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

sync: remove broadcast channel slot level closed flag #4867

Merged
merged 2 commits into from
Aug 10, 2022

Commits on Jul 26, 2022

  1. sync: remove broadcast channel slot level closed flag

    The broadcast channel allows multiple senders to send messages to
    multiple receivers, where each receiver receives messages starting from
    when it subscribes. After all senders are dropped, the receivers will
    continue to receive all waiting messages in the buffer and then receive
    a `Closed` error.
    
    To mark that a channel has closed, it stores two closed flags, one on
    the channel level and another in the buffer slot *after* the last used
    slot (this may also be the earliest entry being kept for lagged
    receivers, see tokio-rs#2425).
    
    However, we don't need both closed flags, keeping the channel level
    closed flag is sufficient.
    
    Without the slot level closed flag, each receiver receives each message
    until it is up to date and for that receiver the channel is empty. Then,
    the actual return message is chosen depending on the channel level
    closed flag; if the channel is NOT closed, then `Empty` is returned, if
    the channel is closed then `Closed` is returned instead.
    
    With the modified logic, there is no longer a need to append a closed
    token to the internal buffer (by setting the slot level closed flag on
    the next slot). This fixes the off by one error described in tokio-rs#4814,
    which caused a receiver which was created after the channel was already
    closed to get `Empty` from `try_recv` (or hang forever when calling
    `recv`) instead of receiving `Closed`.
    
    As a bonus, we save a single `bool` on each buffer slot.
    
    Refs: tokio-rs#4814
    hds committed Jul 26, 2022
    Configuration menu
    Copy the full SHA
    d39626f View commit details
    Browse the repository at this point in the history

Commits on Aug 10, 2022

  1. Update tokio/src/sync/broadcast.rs

    replace unsafe call with `UnsafeCell::new` as the unsafe isn't necessary.
    
    Co-authored-by: Alice Ryhl <aliceryhl@google.com>
    hds and Darksonn authored Aug 10, 2022
    Configuration menu
    Copy the full SHA
    85e5e11 View commit details
    Browse the repository at this point in the history