Add broadcast::Sender::len #5334
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.
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 fromReceiver::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.
The text was updated successfully, but these errors were encountered: