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 broadcast::Sender::len #5334

Closed
sfackler opened this issue Jan 3, 2023 · 1 comment · Fixed by #5343
Closed

Add broadcast::Sender::len #5334

sfackler opened this issue Jan 3, 2023 · 1 comment · Fixed by #5343
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-sync Module: tokio/sync

Comments

@sfackler
Copy link
Contributor

sfackler commented Jan 3, 2023

Is your feature request related to a problem? Please describe.
I'm working on a service that's essentially a broadcast proxy - it receives a data stream and rebroadcasts it to all active clients. Tokio's broadcast queue fits in pretty cleanly to the core of the logic. I want to provide some metrics to help detect laggy clients; this could be the number of lagged items or the current queue size for example. I don't care about tracking this per client - just some aggregate value for the entire queue.

Currently I have a metric counting the number of lagged items that incremented when a client's receiver sees a RecvError::Lagged(_) value. However, this only works properly if the client lags and then recovers - a client that gets permanently stuck will never report any of its lost values.

Alternatively, I could report the queue size via Receiver::len, but since the recv method is async and mutably borrows self, I can't cleanly do that e.g. in another task while simultaneously waiting for more data.

Describe the solution you'd like
The simplest solution I can see is to add a Sender::len method returning the total number of queued items. It would be the maximum of the values reported from Receiver::len across all live receivers at that time.

Describe alternatives you've considered
An alternative could be to add a Receiver::poll_recv method which would make it possible to have one task receiving and another tracking the queue length. However, this would be significantly more complicated to implement on my end and probably more complicated to implement in Tokio as well.

I'm happy to make a PR if the Sender::len approach seems reasonable.

@sfackler sfackler added A-tokio Area: The main tokio crate M-sync Module: tokio/sync C-feature-request Category: A feature request. labels Jan 3, 2023
@Darksonn
Copy link
Contributor

Darksonn commented Jan 3, 2023

This seems reasonable enough to me.

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.

2 participants