pub struct EventRing<T> { /* private fields */ }Expand description
A bounded, replayable buffer of stream events.
Generic over the payload because the ring is transport machinery: it must not
name an MCP type, and a serde_json::Value in a domain signature is exactly
the kind of raw JSON the layer rules keep out of here. The adapter supplies
whatever its wire type is.
Implementations§
Source§impl<T> EventRing<T>
impl<T> EventRing<T>
Sourcepub fn new(capacity: NonZeroUsize) -> Self
pub fn new(capacity: NonZeroUsize) -> Self
A ring retaining at most capacity events.
A capacity of zero is refused at the type level rather than tolerated: such a ring would accept a push, drop it, and report success, which is the eviction-as-success bug with no eviction to notice.
Sourcepub fn with_recommended_capacity() -> Self
pub fn with_recommended_capacity() -> Self
Retains RECOMMENDED_CAPACITY events.
Sourcepub const fn last(&self) -> EventId
pub const fn last(&self) -> EventId
The newest id ever issued, or the watermark if nothing has been.
Sourcepub const fn evicted_through(&self) -> Option<EventId>
pub const fn evicted_through(&self) -> Option<EventId>
The newest event ever dropped, if the ring has dropped anything.
A client at or after this id can still be served contiguously; a client before it cannot.
Sourcepub fn push(&mut self, payload: T) -> EventId
pub fn push(&mut self, payload: T) -> EventId
Appends an event, dropping the oldest if the ring is full.
Returns the id assigned, which is the value a client will quote to resume after this event.
Sourcepub fn replay_from(&self, after: Option<EventId>) -> Replay<'_, T>
pub fn replay_from(&self, after: Option<EventId>) -> Replay<'_, T>
Answers a client that says where it stopped.
after is the client’s Last-Event-ID, or None for a client that has
never seen this stream and wants everything. The four outcomes are
described on Replay; the short version is that a request is only
Replay::is_complete when the ring is still holding everything after
the watermark.
Takes &self and hands back a borrow, so an adapter can answer from a
shared reference without taking the stream’s lock exclusively.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Empties the ring, keeping the id sequence going.
A new stream, not a continuing one: ids stay monotonic so a client that
reconnects across the reset is refused with
Replay::Unknown rather than being served events
from a stream that no longer exists.