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 .len() method to UnboundedSender #7077

Open
tqwewe opened this issue Jan 8, 2025 · 3 comments · May be fixed by #7103
Open

Add .len() method to UnboundedSender #7077

tqwewe opened this issue Jan 8, 2025 · 3 comments · May be fixed by #7103
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-sync Module: tokio/sync

Comments

@tqwewe
Copy link

tqwewe commented Jan 8, 2025

Is your feature request related to a problem? Please describe.
The number of buffered messages can be derived from a bounded mpsc channel sender with len = tx.max_capacity() - tx.capacity(). However there's no methods available to get the number of buffered messages from an unbounded mpsc channel sender.

Describe the solution you'd like
A UnboundedSender::len() method which returns how many messages are buffered in the channel and pending to be received.

// Currently possible
let (boundex_tx, bounded_rx) = mpsc::channel(10);
let bounded_len = bounded_tx.max_capacity() - bounded.capacity();

// Proposed `len` method
let (unbounded_tx, unbounded_rx) = mpsc::unbounded_channel();
let unbounded_len = unbounded_tx.len();
@tqwewe tqwewe added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. labels Jan 8, 2025
@conradludgate
Copy link
Contributor

For unbounded channels, the semaphore used counts up by 2 for each message sent, and subtracts 2 for each message received. So half the semaphore count could be used for such an implementation.

I do wonder what the usecase is for knowing the length from the sender. There's not a lot you can safely do with it (while avoiding race conditions)

@tqwewe
Copy link
Author

tqwewe commented Jan 8, 2025

Another related issue is #6314.

I do wonder what the usecase is for knowing the length from the sender.

In my case, I'm implementing a least-loaded kind of scheduler for a pool of actors. The pool stores the sender of each actor, and in order to forward a message to the actor with the least amount of load I could pick the actor in the pool with the smallest .len() value.

The alternative of course would be that the actor pool keeps a counter for each actor, but this brings some restrictions, particularly when the worker actors are generic actors defined by the user.

@Archibajl
Copy link

So I tried something here #7092
Let me know if this is what you want, if this fulfils your needs, or if it's even an acceptable update for this repo.

I'm still not confident in the repo and rust but as far as I can tell this should work.
it should be able to return a deeper method/function that holds the number of messages in que to be sent,
(the first message says "messages to be received" but this is a sender so I assume it meant messages to be received from our sender.

@tqwewe tqwewe linked a pull request Jan 15, 2025 that will close this issue
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

Successfully merging a pull request may close this issue.

5 participants
@Darksonn @conradludgate @tqwewe @Archibajl and others