Repository navigation
feat(broadcast): add MPMC receiver state queries, bounded reservation, and trait assertions - #452
Merged
Merged
Conversation
Add `is_disconnected` and `is_empty` to `broadcast::mpmc::BoundedReceiver` and `UnboundedReceiver` for endpoint-state parity with `mpsc`, `mpmc`, and `spmc`: - `is_disconnected` loads the atomic sender count with `Acquire` ordering without locking the channel, reporting `true` as soon as the final sender is dropped even while unread buffered messages remain available. - `is_empty` checks whether the receiver's own cursor has no unread messages, ignoring messages retained only for slower subscriptions. Signed-off-by: tison <[email protected]>
Add cancellation-safe two-phase reservation (`reserve`, `try_reserve`, and `broadcast::mpmc::Permit<'_, T>`) to `broadcast::mpmc::BoundedSender`: - Track held reservations in `SendQueue::reserved` alongside retained messages (`retained + reserved <= capacity`) without pre-assigning broadcast sequence numbers, so dropping a permit or cancelling a pending reservation never creates gaps in committed message order. - Keep direct `send` and `try_send` non-blocking when no receivers are active while bounding concurrent `Permit` reservations to `capacity`, and distinguish slot-backed vs zero-receiver notifications in `SendWaiter` so dropping the last receiver wakes blocked `send` futures without spuriously waking `reserve` waiters beyond remaining capacity. - Release the reserved slot and wake a waiting producer before running the rejected payload's destructor if `Permit::send` panics on version overflow. Signed-off-by: tison <[email protected]>
…angelog Assert `Send`, `Sync`, `Unpin`, `UnwindSafe`, and `RefUnwindSafe` for `broadcast::mpmc` endpoints, `Permit`, and futures in `traits_test`. Add `try_reserve_and_send_and_try_recv` and `cancel_blocked_reserve` to `primitives::broadcast::mpmc::bounded`, and document the new receiver state queries and bounded reservation APIs in `CHANGELOG.md`. Signed-off-by: tison <[email protected]>
8 tasks done
… unwind Tighten bounded broadcast wakeup accounting and cleanup unused internal helpers: - Track detached `SendWaiter::NotifiedSlot` nodes in `SendQueue::notified_slots` so dropping the last receiver or cancelling/completing a zero-receiver send only notifies `reserve` waiters for unclaimed free slots rather than re-waking for slots that already have a notification in flight. - Retire a pending `send` waiter only after `Backlog::publish` succeeds so a version-overflow unwind forwards its slot notification to the next waiter before dropping the unsent payload outside the channel lock. - Clarify `bounded` constructor docs on direct-send vs reservation waiting conditions, and remove unused batch-notification helpers from `RetryWaiter`. Signed-off-by: tison <[email protected]>
Replace the custom `SendWaiter` / `notified_slots` state machine and `WaitList::unlink_if` helper with the existing `WaitList<RetryWaiter>` queue: - Bounded broadcast producers already re-check capacity and re-queue on wakeup whenever a racing `try_send` or `try_reserve` claims a freed slot, so `notify_all` on the final receiver drop needs no separate per-waiter slot ledger or `internal/` changes. - Deduplicate the version-overflow unwind tests onto `assert_overflow_drops_unlocked` and consolidate the zero-receiver reservation integration checks. Signed-off-by: tison <[email protected]>
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
Part of #445 (Wave 2).
is_disconnectedand per-cursoris_emptytobroadcast::mpmc::{BoundedReceiver, UnboundedReceiver}so callers can inspect sender liveness and unread backlog without receiving.reserveandtry_reservereturning a borrowedbroadcast::mpmc::Permit<'_, T>onbroadcast::mpmc::BoundedSender, allowing producers to wait for capacity inselect!branches before constructing a message without pre-assigning sequence numbers.traits_testto assertSend,Sync,Unpin,UnwindSafe, andRefUnwindSafeacrossbroadcast::mpmcendpoints,Permit, and futures, and addtry_reserve_and_send_and_try_recvandcancel_blocked_reservebenchmarks toprimitives::broadcast::mpmc::bounded.Design Notes
PermitincrementsSendQueue::reserved(retained + reserved <= capacity) rather than claiming a version slot inBacklog. Message sequence numbers are assigned only whenPermit::sendpublishes, so dropping an unusedPermitor cancelling a pendingreservenever leaves a gap in committed broadcast order.RetryWaiter: BecauseBoundedSender::subscribeandPermit::sendare both synchronous and infallible, concurrentPermitreservations stay bounded bycapacityeven while no receivers are active (preventing a latesubscribe()followed byPermit::sendcalls from exceeding the fixed backlog capacity), whereas directsendandtry_sendcontinue to discard immediately without waiting when no receivers exist.SendQueuereusesWaitList<RetryWaiter>directly without touchinginternal/.Permit::sendandBoundedSender::sendretire their reservation or waiter only afterBacklog::publishsucceeds, ensuring a version-overflow panic unlocks the channel, releases or forwards the notification, and wakes the next waiting producer before running the payload destructor.Benchmarks
Apple M4 Max (
cargo bench -p benchmarks --bench primitives -- broadcast::mpmc::bounded, median of 5 runs):main(d8157bc)try_send_and_try_recvtry_reserve_and_send_and_try_recvtry_send_and_drain_fanout/1try_send_and_drain_fanout/4try_send_and_drain_fanout/16full_channel_sender_handoff_cycle/1full_channel_sender_handoff_cycle/4deliver_to_waiting_receiversend_without_receiverstry_send_when_fullcancel_blocked_sendcancel_blocked_reservedrop_lagging_receiver_wakes_senders/32