Skip to main content

EventStore

Struct EventStore 

Source
pub struct EventStore { /* private fields */ }
Expand description

Thread-safe event store for SSE resumability.

Stores events per stream with automatic expiration and size limits. Use this to enable clients to resume SSE streams after disconnection.

§Thread Safety

The EventStore uses RwLock internally and is safe for concurrent access from multiple threads.

Implementations§

Source§

impl EventStore

Source

pub fn new() -> EventStore

Creates a new event store with default configuration.

Source

pub fn with_config(config: EventStoreConfig) -> EventStore

Creates a new event store with custom configuration.

Source

pub fn config(&self) -> &EventStoreConfig

Returns the configuration.

Source

pub fn store_event(&self, stream_id: &str, data: Option<Value>) -> String

Stores an event and returns its ID.

§Arguments
  • stream_id - The stream (session) this event belongs to
  • data - Event data, or None for a priming event
§Returns

The unique event ID that can be used for resumption.

Source

pub fn store_priming_event(&self, stream_id: &str) -> String

Stores a priming event (empty data) for SSE initialization.

Per SSE spec, servers should send an event with just an ID to prime the client’s Last-Event-ID tracking.

Source

pub fn get_events_after( &self, stream_id: &str, after_id: Option<&str>, ) -> Vec<EventEntry>

Gets events after the specified event ID.

§Arguments
  • stream_id - The stream to get events from
  • after_id - Get events after this ID (exclusive). None returns all events.
§Returns

Vector of events in chronological order.

Source

pub fn replay_events_after<F>( &self, last_event_id: &str, callback: F, ) -> Option<String>
where F: FnMut(&EventEntry),

Replays events after a specific event ID using a callback.

This is the primary method for SSE resumption. When a client reconnects with a Last-Event-ID, use this to replay missed events.

§Arguments
  • last_event_id - The client’s last received event ID
  • callback - Called for each event to replay
§Returns

The stream ID if the event was found, None otherwise.

Source

pub fn find_stream_for_event(&self, event_id: &str) -> Option<String>

Looks up the stream ID for a given event ID.

§Returns

The stream ID if the event exists, None otherwise.

Source

pub fn clear_stream(&self, stream_id: &str)

Removes all events for a stream.

Call this when a session ends to free memory.

Source

pub fn cleanup_expired(&self)

Removes all expired events across all streams.

This is called automatically during operations, but you can call it manually for cleanup.

Source

pub fn stream_count(&self) -> usize

Returns the number of streams currently stored.

Source

pub fn event_count(&self) -> usize

Returns the total number of events across all streams.

Source

pub fn stats(&self) -> EventStoreStats

Returns statistics about the event store.

Trait Implementations§

Source§

impl Debug for EventStore

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for EventStore

Source§

fn default() -> EventStore

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more