Crates.io | relaxed_channel |
lib.rs | relaxed_channel |
version | 0.2.0 |
source | src |
created_at | 2023-11-08 18:28:46.818259 |
updated_at | 2024-03-17 14:57:00.068414 |
description | Wrapper around async-channel, more performant on heavy pipelines |
homepage | |
repository | https://github.com/Ten0/relaxed_channel |
max_upload_size | |
id | 1029228 |
size | 24,630 |
Wrapper around async-channel, more performant on heavy pipelines
When readers (holding the Receiver
) are faster than writers (holding the Sender
), the channel's length will always be low. Normally, after reading a message, a reader task would encounter an empty channel when attempting to read the next message, and immediately set itself waiting for a wake up when the next message is sent. This implies that upon sending the next message, the writer has to dedicate a significant portion of its time marking the receiving tasks for wake up in the tokio runtime.
(The portion of time is significant when the pipeline gets heavy, around millions of messages per second.)
The effect increases as the number of readers increases (because there are more readers to wake up), so the more parallelism is required on the receiving end, the slower the writer(s) get.
When reading a message and encountering an empty channel:
Receiver::recv()
to check for a message, and only now mark ourselves for wake up if there is still no message available.This implies that each reader will only have to be woken up by a writer at most once every 100ms.
This crate provides RelaxedReceiver
, wrapping the regular async_channel::Receiver
to add this behavior.
Note that this design is such that all readers may be sleeping at the same time for 100ms, so in order for this to not slow the reader down, the channel's capacity must be large enough to hold more messages than may arrive during that time.