Add WeakSender for tokio::sync::broadcast #7003
Labels
A-tokio
Area: The main tokio crate
C-feature-request
Category: A feature request.
M-sync
Module: tokio/sync
Is your feature request related to a problem? Please describe.
WeakSender is a useful abstraction which allows a "central spawner" to be able to hand out new Senders when needed, but not have the "central spawner" keep a channel alive after all the actual users shutdown.
This diff: https://github.com/RosLibRust/roslibrust/pull/208/files shows a real world application where I was relying on WeakSender for an mpsc channel, but when refactoring a chunk of code to use broadcast instead of mpsc had to do some gymnastics to work around broadcast lacking a WeakSender.
I also consider it "principle of least surprise" that if WeakSender exists for one channel type it would exist for all channel types.
Describe the solution you'd like
tokio::sync::broadcast to support WeakSender with an identical api to tokio::sync::mpsc
Describe alternatives you've considered
It is likely possible for users to emulate this behavior by passing Arc to the various users of their channel, and having the central spawner hold a std::sync::Weak to that Arc. However it is non-obvious that this is the correct way to do things and refactoring code to hold Arc in place of Sender is sub-optimal in both ergonomics and performance.
I think the biggest issue at play is the surprising difference between mpsc and broadcast. I can see a call for removing WeakSender from mpsc and pushing users for an externalized solution instead.
I'm unaware of the internal implementation details of mpsc vs. broadcast that make a WeakSender implementation applicable to one but not the other?
The text was updated successfully, but these errors were encountered: