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 method to watch::Receiver #6252

Merged
merged 4 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tokio/src/sync/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,22 @@ impl<T> Receiver<T> {
self.version.decrement();
}

/// Marks the state as unchanged.
///
/// The current value can will be considered seen by the receiver.
PaulOlteanu marked this conversation as resolved.
Show resolved Hide resolved
///
/// After invoking this method [`has_changed()`](Self::has_changed)
/// *MAY* return `false` and [`changed()`](Self::changed) *MAY* not return
/// immediately. This is because a new value could be sent before
/// this method returns.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only the case if a value could be sent concurrently with this call. If there are no concurrent sends, then this does not apply.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'll explain that better. Should I also provide a warning that the value being marked as "seen" could change if used concurrently as well? e.g.

  1. receiver version is 2, shared version is 4. mark_unchanged is called
  2. another task sends a message so shared version becomes 6, mark_unchanged sets receiver version to 6
  3. the caller intended to ignore the value associated with version 4 but instead skips 2 values

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how important the warning really is. It applies to anything that works with concurrency. You could say something along the lines of if another message is sent concurrently with the call to mark_unchanged, then you cannot predict whether the message or the mark_unchanged call comes first.

///
/// This is useful if you are not interested in the current value
/// visible in the receiver.
pub fn mark_unchanged(&mut self) {
let current_version = self.shared.state.load().version();
self.version = current_version;
}

/// Waits for a change notification, then marks the newest value as seen.
///
/// If the newest value in the channel has not yet been marked seen when
Expand Down
33 changes: 33 additions & 0 deletions tokio/tests/sync_watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,39 @@ fn rx_mark_changed() {
assert_eq!(*rx.borrow(), "two");
}

#[test]
fn rx_mark_unchanged() {
let (tx, mut rx) = watch::channel("one");

let mut rx2 = rx.clone();

{
assert!(!rx.has_changed().unwrap());

rx.mark_changed();
assert!(rx.has_changed().unwrap());

rx.mark_unchanged();
assert!(!rx.has_changed().unwrap());

let mut t = spawn(rx.changed());
assert_pending!(t.poll());
}

{
assert!(!rx2.has_changed().unwrap());

tx.send("two").unwrap();
assert!(rx2.has_changed().unwrap());

rx2.mark_unchanged();
assert!(!rx2.has_changed().unwrap());
assert_eq!(*rx2.borrow_and_update(), "two");
}

assert_eq!(*rx.borrow(), "two");
}

#[test]
fn multi_rx() {
let (tx, mut rx1) = watch::channel("one");
Expand Down
Loading