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 mark_unchanged or mark_seen for tokio::sync::watch::Receiver. #6086

Closed
Tnze opened this issue Oct 19, 2023 · 3 comments · Fixed by #6252
Closed

Add mark_unchanged or mark_seen for tokio::sync::watch::Receiver. #6086

Tnze opened this issue Oct 19, 2023 · 3 comments · Fixed by #6252
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-sync Module: tokio/sync

Comments

@Tnze
Copy link

Tnze commented Oct 19, 2023

Is your feature request related to a problem? Please describe.

In developing of a web server, I need to wait a new value when handling every http requests. I create a watch channel and clone the Receiver for every individual request.

What I want is to call .changed().await and wait for a new value. However the receiver I clone from is always outdated. So I have to mark it as seen before waiting new value, otherwise the changed() return immediately.

Currently, the only way to mark the receiver is seen is the borrow_and_update() method. But it not only update the internal version, but also lock the internal RwLock for getting the value, which is unnecessary in this case.

Describe the solution you'd like
I want to add a method called mark_unchanged or mark_seen, which just update the Receiver internal version, without locking the shared RwLock anyway.

Describe alternatives you've considered
drop(borrow_and_update())

Additional context
No additional context.

I can explain more details of how I expect the web server works. When a new GET request incoming, it blocks to wait. In the background, a coroutine is scanning bluetooth devices in an infinite loop. Every time it found one, it sends a value to the channel. And all currently waiting request handlers received the value and return.

I suggest to add this method for avoid an unnecessary read-lock because we can. It's not a critical optimization for me. But it will be useful for someone who is building high-performance services.

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

Considering that we have a mark_changed, it seems okay to also have mark_unchanged.

@Tnze
Copy link
Author

Tnze commented Oct 19, 2023

Additionally:

Unlike mark_changed, the receiver can transform from 'unchanged' to 'changed' internally but not vice versa.

We should remind users in the document that the channel could be changed again even before the mark_unchanged method returned.

@Nerdy5k

This comment was marked as spam.

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