smol-rs / async-broadcast

Async broadcast channels
Apache License 2.0
151 stars 26 forks source link

Allow Sender to not await active receivers #35

Closed zeenix closed 1 year ago

zeenix commented 1 year ago

While typically the sender would want to await active receivers, there are some cases where it'd be desirable for sender to not wait but rather not block. One example would be where there are multiple channels to send out to. The use case for using multiple channels would typically be to avoid needlessly waking up tasks that are only interested in the message if it qualifies a certain criteria. Since async-broadcast doesn't provide API for setting up sender-side filters on receivers (with the current implementation, I'm not even sure that's possible), users will have to split up the channels, one for each filter. There may also be other use cases.

Two solutions come to mind:

  1. We extend the scope of overflow mode. When it's set to true, Sender doesn't wait for active receivers and just drops the message.
  2. Introduce a new await-active-receiver mode that when set, means Sender not waiting for active receivers. The question would be if broadcast should just succeed when there are no active receivers of error out, in case sender wanted the message back if nobody could receive it? I'm leaning towards the latter.