pub struct Events { /* private fields */ }Expand description
Server-wide event registry.
Owns the inline sinks and the deferred handlers. Sinks are awaited by
post before it returns; handlers are queued and run one at a time on a
single dispatcher task.
Implementations§
Source§impl Events
impl Events
pub fn new() -> Self
Sourcepub fn add_sink(&self, sink: Arc<dyn EventSink>)
pub fn add_sink(&self, sink: Arc<dyn EventSink>)
Register an inline sink. Sinks are awaited in registration order.
May be called at any time, including from inside another sink’s
call-out; the new sink takes effect from the next post.
Sourcepub fn add_handler(&self, event: impl Into<String>, handler: EventHandler)
pub fn add_handler(&self, event: impl Into<String>, handler: EventHandler)
Register a deferred handler for event.
Handlers for one event run in registration order.
Sourcepub fn dropped_count(&self) -> u64
pub fn dropped_count(&self) -> u64
Number of handler invocations dropped because the queue was full.
Sourcepub fn failed_count(&self) -> u64
pub fn failed_count(&self) -> u64
Number of sink or handler invocations that panicked.
Sourcepub fn start_dispatcher(&self, store: Arc<SimplePvStore>)
pub fn start_dispatcher(&self, store: Arc<SimplePvStore>)
Start the single dispatcher task. Call once, at server start.
Sourcepub async fn post(&self, event: &str)
pub async fn post(&self, event: &str)
Post event: await every sink, then queue handlers and return.
Sinks run in registration order and are each wrapped in
catch_unwind, exactly as handlers are on the dispatcher: a
panicking sink is logged, counted in Self::failed_count, and the
fan-out continues. Without that, one bad sink would unwind out of
post and the remaining sinks and every handler would silently
never see the event.
An unknown event name is a no-op — events are a dynamic namespace.
Sourcepub async fn drain(&self)
pub async fn drain(&self)
Wait until every queued handler has finished. Test helper.
Bounded rather than an unconditional spin: this doubles as a deadlock
detector. If a future change ever breaks the invariant that the
dispatcher always decrements inflight (e.g. removing the
catch_unwind around handler futures), a hung dispatcher must show up
as a clear, named test failure — not an indefinitely hanging CI job.