pub struct EventStream { /* private fields */ }Expand description
In-memory event stream buffer that supports multiple consumers.
Events are stored in a bounded ring buffer. Each consumer tracks its own cursor independently. When the buffer is full, the oldest events are evicted (consumers that fall too far behind will miss events).
Implementations§
Source§impl EventStream
impl EventStream
Sourcepub fn new(buffer_size: usize) -> Self
pub fn new(buffer_size: usize) -> Self
Create a new event stream with the given buffer capacity.
§Arguments
buffer_size— Maximum number of events to retain. When the buffer is full, the oldest events are evicted on push.
Sourcepub fn push(&mut self, event: DecodedEvent)
pub fn push(&mut self, event: DecodedEvent)
Push a new event into the stream.
If the buffer is at capacity, the oldest event is evicted first.
Sourcepub fn next_batch(
&self,
cursor: &StreamCursor,
limit: usize,
) -> Result<EventBatch, IndexerError>
pub fn next_batch( &self, cursor: &StreamCursor, limit: usize, ) -> Result<EventBatch, IndexerError>
Fetch the next batch of events for a consumer, starting after cursor.
Returns events that come after the cursor position, up to limit events.
The returned EventBatch contains a new cursor pointing to the end of
the batch (use it for the next call).
If the cursor version does not match the current stream version (due to
a reorg), an error is returned. The consumer should re-register or use
StreamCursor::initial().
Sourcepub fn register_consumer(&mut self, id: impl Into<String>)
pub fn register_consumer(&mut self, id: impl Into<String>)
Register a new consumer with the given ID.
The consumer starts at the initial cursor position (beginning of stream). If a consumer with this ID already exists, its cursor is reset.
Sourcepub fn get_consumer_cursor(&self, id: &str) -> Option<&StreamCursor>
pub fn get_consumer_cursor(&self, id: &str) -> Option<&StreamCursor>
Get the current cursor for a registered consumer.
Returns None if the consumer is not registered.
Sourcepub fn update_consumer_cursor(&mut self, id: &str, cursor: StreamCursor)
pub fn update_consumer_cursor(&mut self, id: &str, cursor: StreamCursor)
Update a consumer’s cursor (e.g., after processing a batch).
Sourcepub fn invalidate_after(&mut self, block_number: u64)
pub fn invalidate_after(&mut self, block_number: u64)
Invalidate all events at or above block_number (reorg handling).
Removes affected events from the buffer and increments the stream version, which invalidates all outstanding cursors.