feat(watch): add a latest-state channel - #233
Open
QwQBiG wants to merge 1 commit into
Open
Conversation
Author
Author
|
All 9 CI checks passed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
asyncband::watchlatest-state channel with cloneable senders and independently versioned receiversborrownon-consuming, and drain a final unseen value before reporting disconnectionchangedcancellation safe and release replaced values, retired wakers, and wake callbacks outside the internal lockCloses #215.
Design Notes
The public API returns owning
Arc<T>snapshots fromborrow,borrow_and_update, andchanged. Unlike Tokio's lock-backedRef<T>andchanged() -> Result<(), _>API, these snapshots never retain the channel's internal lock and may be stored or moved independently. The ecosystem benchmark documents this semantic difference and immediately reads the currentusizevalue on both sides.The implementation retains an
Arc<T>, a monotonically checked version, endpoint counts, and aWaitSetunder one private mutex. Each receiver stores its own observed version. New subscriptions begin at the current version, while receiver clones inherit the source receiver's observed version and then advance independently.The internal
arena,mutex, andwaitsetfeature gates includewatchbecause of thewatch -> WaitSet -> Arenadependency chain and watch's direct use of the internal mutex. These additions are additive and do not change any existing feature combination or public behavior.WaitSet::is_empty()lets the watch send and final-sender-drop paths avoid taking an empty waker set. Besides avoiding unnecessary work, this skips the internal epoch advance performed bytake_wakers()when no waiter exists.The benchmarks cover current-value borrowing, send-and-borrow paths, ready changes, pending notification, and 1/2/4/8/32-receiver fanout. No measured watch path showed a sustained regression above 3x relative to Tokio on the development machine.
Validation included
cargo x build --locked,cargo x check,cargo x test --no-capture,cargo x bench, Rust 1.86 watch-only all-target checks, nightly Clippy and rustfmt, typos, Hawkeye license checks, and all-features rustdoc with warnings denied.