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 Sender::len to mpsc channel #6314

Open
barafael opened this issue Jan 30, 2024 · 7 comments · Fixed by #6348
Open

Add Sender::len to mpsc channel #6314

barafael opened this issue Jan 30, 2024 · 7 comments · Fixed by #6348
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-sync Module: tokio/sync

Comments

@barafael
Copy link
Contributor

barafael commented Jan 30, 2024

I'm usually choosing channel sizes intuitively. I frequently miss having empirical data on how full the channels in my system are during normal operation or more exotic load situations.

I'd like to either have public functions on the mpsc::Sender that allow me to see how many elements are in there, like crossbeam::channel::Sender::len.

It would also be fine to emit the channel length as a metric of sorts, like attempted in this PR.

@barafael barafael added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. labels Jan 30, 2024
@Darksonn Darksonn added M-sync Module: tokio/sync M-tracing Tracing support in Tokio labels Jan 31, 2024
@Darksonn
Copy link
Contributor

Adding a Sender::len method seems reasonable enough. More complex solutions with tracing would need more details on exactly what is being proposed.

@barafael
Copy link
Contributor Author

barafael commented Feb 12, 2024

The Sender::len would be completely sufficient for me, it seems like a worthwhile API addition. Should also be available on WeakSender probably. I'll take a shot at it, would you mind pointing me in a direction?

@Darksonn
Copy link
Contributor

You should probably add a method here, and use it to provide a len method on the Rx/Tx types in chan.rs. Then you can add methods on the user-facing channel types that call these methods.

@barafael
Copy link
Contributor Author

Actually, I think simply tx.max_capacity() - tx.capacity() does exactly what I need. I'm not sure this warrants an extra method?

@Darksonn
Copy link
Contributor

Hmm, there's no equivalent to that for the unbounded channel, so I am still okay with adding a len method.

@dan-da
Copy link

dan-da commented Nov 4, 2024

I don't understand why this issue was closed since it explicitly requests a len() method on mpsc::Sender and references crossbeam::channel::Sender::len(). The merged PR #6348 adds methods to the receiver not the sender.

The lack of a sender len method is presently causing me difficulties as I attempt to migrate from another crates's channel impl. I was hoping to remove a dep, but may not be able to until this is implemented.

request reopen.

@Darksonn Darksonn changed the title tracing: mpsc channel size metrics Add Sender::len to mpsc channel Nov 5, 2024
@Darksonn Darksonn removed the M-tracing Tracing support in Tokio label Nov 5, 2024
@Darksonn Darksonn reopened this Nov 5, 2024
@Darksonn
Copy link
Contributor

Darksonn commented Nov 5, 2024

I suppose that is fair enough. That said, it's probably somewhat tricky to implement. You need information from fields that are currently only accessible to the receiver.

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.

3 participants