Skip to content
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

Add WeakSender for tokio::sync::broadcast #7003

Open
Carter12s opened this issue Dec 2, 2024 · 1 comment
Open

Add WeakSender for tokio::sync::broadcast #7003

Carter12s opened this issue Dec 2, 2024 · 1 comment
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-sync Module: tokio/sync

Comments

@Carter12s
Copy link

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?

@Carter12s Carter12s added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. labels Dec 2, 2024
@Darksonn Darksonn added the M-sync Module: tokio/sync label Dec 3, 2024
@Darksonn
Copy link
Contributor

Darksonn commented Dec 3, 2024

You could implement this by holding on to a Receiver and using resubscribe.

Edit: actually no I confused myself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-sync Module: tokio/sync
Projects
None yet
Development

No branches or pull requests

2 participants