Expand description
PubSubRing: one-producer many-subscriber broadcast primitive
with per-subscriber positions.
Where a regular ring (SpscRingCore) has one consumer position
(the tail), PubSubRing exposes the producer’s monotonic head
as the absolute position and lets each subscriber walk
positions independently. Subscriber positions are tracked
externally via SubscriberPosition, so they can survive a
subscriber restart.
§Slot layout
Each slot carries a sequence: AtomicU64 + 56-byte payload.
On a successful publish(payload), the producer:
- Writes the payload into slot[head % capacity].
- Releases the new sequence = head + 1.
- Releases head + 1 into the header.
On read_at(position), a subscriber:
- Reads the slot’s sequence with Acquire.
- Validates
sequence == position + 1(matches expected slot). Ifsequence > position + 1, the slot has been overwritten (wraparound); subscriber returnsPubSubReadError::Lost. Ifsequence < position + 1, the slot is not yet published; subscriber returnsPubSubReadError::Pending. - On match: copies the payload to the out buffer.
§KeepAll vs KeepLastN policy
The primitive itself is KeepLastN-shaped: producer never blocks on subscribers; wraparound happens at capacity. Callers that want KeepAll semantics check the minimum subscriber position before publishing and back off when the ring is about to wrap past it. Helpers for that pattern can layer on top.
Structs§
- PubSub
Ring - One-producer many-subscriber broadcast ring with per-subscriber positions.
- PubSub
Subscriber - Subscriber-side helper that holds a
SubscriberPositionand pulls items from aPubSubRingin order.
Enums§
- PubSub
Read Error - Errors a subscriber read can return.
Constants§
- PUBSUB_
PAYLOAD_ BYTES - Payload bytes per slot. Matches the Vyukov-side payload size to keep the substrate’s per-slot byte layout consistent across primitives.