-
Notifications
You must be signed in to change notification settings - Fork 342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API: channel::send_all? #486
Comments
I think it would be surprising to have broadcast and unicast channels be the same |
For cases where I don't care about the source, I can always use |
@dignifiedquire Can you share more why you'd want to distinguish between the two? To share some more of my thinking here: MPMC channels (like the ones in this crate) already support having multiple senders and multiple receivers. The proposal here would be to add a single method so that a message can be sent to all active receivers (rather than only a single one at the time). From an API perspective this feels like a relatively natural extension. We can actually look at the broader ecosystem to see how alternative models would play out. Tokio recently introduced
Because Because the way async-std's channels have been designed we can already fast-path simpler topologies, so there's not even a performance argument to add different channel kinds. All we're missing is a way to send a message to all active receivers, which is what this issue proposes we add. |
Isn't there enough differences and restrictions to group in the broadcast ability with the async-std channel? It would require For example the case: use async_std::sync::channel;
let (s, r1) = channel(1);
// Clone the channel and send an event to both channels
let r2 = r1.clone();
s.send_all(String::from("chashu")).await;
assert_eq!(r1.recv().await, "chashu");
// This will block since r2 channel is full and has not polled "chashu" even though r1 could receive this event
s.send(String::from("test")).await; The tokio broadcast channel errors only on the receiver that is lagging, but also allows it to poll most recent events after polling the error. Is it intended that this would block the sender on a full channel buffer (blocked by the slowest receiver) or this behaviour? |
Absolutely; if you don't want to be blocked on the slowest reader, either using |
I'm having a little trouble thinking about how that could be used, because then the receive function would
Hmm yeah my intuition would be that unbounded receivers doesn't really seem that practical for this, and timeouts I would think would add unnecessary overhead. I am also biased by a specific use case and don't have extensive understanding of how the internals of this channel works. |
In #212 (comment) I mentioned it'd be interesting to investigate broadcasting channels. I initially thought we might want to create a new type for this, possibly in a separate library, but I think at least API-wise we could integrate this into the existing channels module.
The way we could do this is by introducing a new API:
Sender::send_all
. When an item is passed intoSender::send_all
it's cloned and sent to every availableReceiver
.Now a huge caveat here is that this may not at all be performant, and/or complicate the channels design to such a degree that a separate type is warranted. But I figured at least sharing what seems to be an ideal solution from an end-user point of view would provide us with a starting point to assess this further.
Related #436, cc/ @stjepang
API
Example
The text was updated successfully, but these errors were encountered: