pub struct LocalEventBus { /* private fields */ }Expand description
Thread-safe in-process event bus.
This backend stores subscriptions in memory and dispatches subscriber
handlers on background threads. Publishing schedules work and returns after
dispatch, while wait_for_idle can be used by tests
to wait for all handler work for a topic.
Implementations§
Source§impl LocalEventBus
impl LocalEventBus
Sourcepub fn started() -> EventBusResult<Self>
pub fn started() -> EventBusResult<Self>
Sourcepub fn start(&self) -> EventBusResult<bool>
pub fn start(&self) -> EventBusResult<bool>
Sourcepub fn shutdown(&self) -> bool
pub fn shutdown(&self) -> bool
Shuts down the event bus.
The method waits for currently scheduled handlers to finish and then clears all subscriptions.
§Returns
true when this call changed the bus from started to stopped.
§Panics
Panics when called from one of this bus’s subscriber worker threads. A
subscriber worker cannot wait for itself to finish. Use
shutdown_nonblocking or
shutdown_with_timeout from subscriber
handlers.
Sourcepub fn shutdown_nonblocking(&self) -> bool
pub fn shutdown_nonblocking(&self) -> bool
Requests shutdown without waiting for subscriber work to finish.
The bus stops accepting publish and subscribe operations, asks the handler executor to shut down, deactivates subscriptions, and returns immediately. Already running handler code is not interrupted.
§Returns
true when this call changed the bus from started to stopped.
Sourcepub fn shutdown_with_timeout(&self, timeout: Duration) -> EventBusResult<bool>
pub fn shutdown_with_timeout(&self, timeout: Duration) -> EventBusResult<bool>
Shuts down the event bus with a maximum wait duration.
The bus stops accepting new publish and subscribe operations immediately, then waits for scheduled subscriber work and executor workers to finish. If the timeout elapses, subscriptions are deactivated before the timeout error is returned.
§Parameters
timeout: Maximum duration to wait for graceful shutdown.
§Returns
Ok(true) when this call changed the bus from started to stopped and
shutdown completed within the timeout. Ok(false) means the bus was
already stopped.
§Errors
Returns EventBusError::ShutdownTimedOut if subscriber work or executor
workers do not finish before timeout.
Sourcepub fn add_error_observer<F>(&self, observer: F) -> EventBusResult<()>
pub fn add_error_observer<F>(&self, observer: F) -> EventBusResult<()>
Sourcepub fn publish<T>(&self, topic: &Topic<T>, payload: T) -> EventBusResult<()>
pub fn publish<T>(&self, topic: &Topic<T>, payload: T) -> EventBusResult<()>
Publishes a payload to a topic.
§Parameters
topic: Target topic.payload: Event payload.
§Returns
Ok(()) after subscriber work has been scheduled.
Local dispatch is non-transactional across matching subscribers. If scheduling fails for a later subscriber, earlier subscriber work may already have been accepted.
§Errors
Returns EventBusError::NotStarted if the bus is stopped.
Sourcepub fn publish_with_options<T>(
&self,
topic: &Topic<T>,
payload: T,
options: PublishOptions<T>,
) -> EventBusResult<()>
pub fn publish_with_options<T>( &self, topic: &Topic<T>, payload: T, options: PublishOptions<T>, ) -> EventBusResult<()>
Publishes a payload to a topic with explicit options.
§Parameters
topic: Target topic.payload: Event payload.options: Publish options merged with factory defaults.
§Returns
Ok(()) after subscriber work has been scheduled.
§Errors
Returns EventBusError::NotStarted if the bus is stopped.
Sourcepub fn publish_envelope<T>(
&self,
envelope: EventEnvelope<T>,
) -> EventBusResult<()>
pub fn publish_envelope<T>( &self, envelope: EventEnvelope<T>, ) -> EventBusResult<()>
Publishes an existing envelope.
§Parameters
envelope: Event envelope to dispatch.
§Returns
Ok(()) after subscriber work has been scheduled.
§Errors
Returns EventBusError::NotStarted if the bus is stopped.
Sourcepub fn publish_envelope_with_options<T>(
&self,
envelope: EventEnvelope<T>,
options: PublishOptions<T>,
) -> EventBusResult<()>
pub fn publish_envelope_with_options<T>( &self, envelope: EventEnvelope<T>, options: PublishOptions<T>, ) -> EventBusResult<()>
Publishes an existing envelope with options.
§Parameters
envelope: Event envelope to dispatch.options: Publish options.
§Returns
Ok(()) after subscriber work has been scheduled.
§Errors
Returns EventBusError::NotStarted if the bus is stopped.
Sourcepub fn publish_all<T>(
&self,
envelopes: Vec<EventEnvelope<T>>,
) -> EventBusResult<BatchPublishResult>
pub fn publish_all<T>( &self, envelopes: Vec<EventEnvelope<T>>, ) -> EventBusResult<BatchPublishResult>
Publishes multiple envelopes by submitting each envelope in input order.
This method preserves submission order only. Handler execution order is backend-specific because local handlers can run on multiple worker threads.
§Parameters
envelopes: Envelopes to submit in order.
§Returns
Summary containing per-envelope successes and failures.
§Errors
Returns lifecycle or option validation errors before the batch starts.
Sourcepub fn publish_all_with_options<T>(
&self,
envelopes: Vec<EventEnvelope<T>>,
options: PublishOptions<T>,
) -> EventBusResult<BatchPublishResult>
pub fn publish_all_with_options<T>( &self, envelopes: Vec<EventEnvelope<T>>, options: PublishOptions<T>, ) -> EventBusResult<BatchPublishResult>
Publishes multiple envelopes with explicit publish options.
§Parameters
envelopes: Envelopes to submit in order.options: Publish options cloned for each envelope.
§Returns
Summary containing per-envelope successes and failures.
§Errors
Returns lifecycle or option validation errors before the batch starts.
Sourcepub fn subscribe<T, S, F, R>(
&self,
subscriber_id: S,
topic: &Topic<T>,
handler: F,
) -> EventBusResult<Subscription<T>>where
T: Clone + Send + Sync + 'static,
S: Into<String>,
F: Fn(EventEnvelope<T>) -> R + Send + Sync + 'static,
R: IntoEventBusResult + 'static,
pub fn subscribe<T, S, F, R>(
&self,
subscriber_id: S,
topic: &Topic<T>,
handler: F,
) -> EventBusResult<Subscription<T>>where
T: Clone + Send + Sync + 'static,
S: Into<String>,
F: Fn(EventEnvelope<T>) -> R + Send + Sync + 'static,
R: IntoEventBusResult + 'static,
Sourcepub fn subscribe_with_options<T, S, F, R>(
&self,
subscriber_id: S,
topic: &Topic<T>,
handler: F,
options: SubscribeOptions<T>,
) -> EventBusResult<Subscription<T>>where
T: Clone + Send + Sync + 'static,
S: Into<String>,
F: Fn(EventEnvelope<T>) -> R + Send + Sync + 'static,
R: IntoEventBusResult + 'static,
pub fn subscribe_with_options<T, S, F, R>(
&self,
subscriber_id: S,
topic: &Topic<T>,
handler: F,
options: SubscribeOptions<T>,
) -> EventBusResult<Subscription<T>>where
T: Clone + Send + Sync + 'static,
S: Into<String>,
F: Fn(EventEnvelope<T>) -> R + Send + Sync + 'static,
R: IntoEventBusResult + 'static,
Subscribes a handler using explicit options.
§Parameters
subscriber_id: Subscriber identifier.topic: Topic to subscribe.handler: Handler invoked for matching events.options: Subscription processing options.
§Returns
Subscription handle.
§Errors
Returns an error when the bus is stopped, the subscriber ID is blank, or shared state is unavailable.
Sourcepub fn add_dead_letter_handler<F, R>(
&self,
dead_letter_topic: &Topic<DeadLetterPayload>,
handler: F,
options: SubscribeOptions<DeadLetterPayload>,
) -> EventBusResult<Subscription<DeadLetterPayload>>where
F: Fn(EventEnvelope<DeadLetterPayload>) -> R + Send + Sync + 'static,
R: IntoEventBusResult + 'static,
pub fn add_dead_letter_handler<F, R>(
&self,
dead_letter_topic: &Topic<DeadLetterPayload>,
handler: F,
options: SubscribeOptions<DeadLetterPayload>,
) -> EventBusResult<Subscription<DeadLetterPayload>>where
F: Fn(EventEnvelope<DeadLetterPayload>) -> R + Send + Sync + 'static,
R: IntoEventBusResult + 'static,
Subscribes a handler to a dead-letter topic.
Dead-letter payloads are type-erased, so callers can inspect the
original topic, error metadata, and original payload through
DeadLetterPayload.
§Parameters
dead_letter_topic: Topic carrying dead-letter records.handler: Handler invoked for dead-letter events.options: Subscription options merged with factory defaults.
§Returns
Subscription handle for the dead-letter topic.
§Errors
Returns an error when the bus is stopped, the generated subscriber ID is invalid, or shared state is unavailable.
Sourcepub fn wait_for_idle<T>(&self, topic: &Topic<T>) -> EventBusResult<()>where
T: 'static,
pub fn wait_for_idle<T>(&self, topic: &Topic<T>) -> EventBusResult<()>where
T: 'static,
Sourcepub fn wait_for_idle_timeout<T>(
&self,
topic: &Topic<T>,
timeout: Duration,
) -> EventBusResult<bool>where
T: 'static,
pub fn wait_for_idle_timeout<T>(
&self,
topic: &Topic<T>,
timeout: Duration,
) -> EventBusResult<bool>where
T: 'static,
Waits until all work for a topic is idle or the timeout elapses.
§Parameters
topic: Topic to wait for.timeout: Maximum duration to wait.
§Returns
Ok(true) once the topic has no active handler work, or Ok(false) when
the timeout elapses first.
§Errors
Returns a lock-poisoning error if tracker state is unavailable.
Trait Implementations§
Source§impl Clone for LocalEventBus
impl Clone for LocalEventBus
Source§fn clone(&self) -> LocalEventBus
fn clone(&self) -> LocalEventBus
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl EventBus for LocalEventBus
impl EventBus for LocalEventBus
Source§fn start(&self) -> EventBusResult<bool>
fn start(&self) -> EventBusResult<bool>
Starts the local event bus.
Source§fn publish<T>(&self, topic: &Topic<T>, payload: T) -> EventBusResult<()>
fn publish<T>(&self, topic: &Topic<T>, payload: T) -> EventBusResult<()>
Publishes a payload using the local backend.
Source§fn publish_with_options<T>(
&self,
topic: &Topic<T>,
payload: T,
options: PublishOptions<T>,
) -> EventBusResult<()>
fn publish_with_options<T>( &self, topic: &Topic<T>, payload: T, options: PublishOptions<T>, ) -> EventBusResult<()>
Publishes a payload with options using the local backend.
Source§fn publish_envelope<T>(&self, envelope: EventEnvelope<T>) -> EventBusResult<()>
fn publish_envelope<T>(&self, envelope: EventEnvelope<T>) -> EventBusResult<()>
Publishes an envelope using the local backend.
Source§fn publish_envelope_with_options<T>(
&self,
envelope: EventEnvelope<T>,
options: PublishOptions<T>,
) -> EventBusResult<()>
fn publish_envelope_with_options<T>( &self, envelope: EventEnvelope<T>, options: PublishOptions<T>, ) -> EventBusResult<()>
Publishes an envelope with options using the local backend.
Source§fn publish_all<T>(
&self,
envelopes: Vec<EventEnvelope<T>>,
) -> EventBusResult<BatchPublishResult>
fn publish_all<T>( &self, envelopes: Vec<EventEnvelope<T>>, ) -> EventBusResult<BatchPublishResult>
Publishes a batch using the local backend.
Source§fn publish_all_with_options<T>(
&self,
envelopes: Vec<EventEnvelope<T>>,
options: PublishOptions<T>,
) -> EventBusResult<BatchPublishResult>
fn publish_all_with_options<T>( &self, envelopes: Vec<EventEnvelope<T>>, options: PublishOptions<T>, ) -> EventBusResult<BatchPublishResult>
Publishes a batch with options using the local backend.
Source§fn subscribe<T, S, F, R>(
&self,
subscriber_id: S,
topic: &Topic<T>,
handler: F,
) -> EventBusResult<Subscription<T>>where
T: Clone + Send + Sync + 'static,
S: Into<String>,
F: Fn(EventEnvelope<T>) -> R + Send + Sync + 'static,
R: IntoEventBusResult + 'static,
fn subscribe<T, S, F, R>(
&self,
subscriber_id: S,
topic: &Topic<T>,
handler: F,
) -> EventBusResult<Subscription<T>>where
T: Clone + Send + Sync + 'static,
S: Into<String>,
F: Fn(EventEnvelope<T>) -> R + Send + Sync + 'static,
R: IntoEventBusResult + 'static,
Subscribes a handler using local backend defaults.
Source§fn subscribe_with_options<T, S, F, R>(
&self,
subscriber_id: S,
topic: &Topic<T>,
handler: F,
options: SubscribeOptions<T>,
) -> EventBusResult<Subscription<T>>where
T: Clone + Send + Sync + 'static,
S: Into<String>,
F: Fn(EventEnvelope<T>) -> R + Send + Sync + 'static,
R: IntoEventBusResult + 'static,
fn subscribe_with_options<T, S, F, R>(
&self,
subscriber_id: S,
topic: &Topic<T>,
handler: F,
options: SubscribeOptions<T>,
) -> EventBusResult<Subscription<T>>where
T: Clone + Send + Sync + 'static,
S: Into<String>,
F: Fn(EventEnvelope<T>) -> R + Send + Sync + 'static,
R: IntoEventBusResult + 'static,
Subscribes a handler with options using the local backend.
Source§fn wait_for_idle<T>(&self, topic: &Topic<T>) -> EventBusResult<()>where
T: 'static,
fn wait_for_idle<T>(&self, topic: &Topic<T>) -> EventBusResult<()>where
T: 'static,
Waits until local topic work is idle.
Source§fn wait_for_idle_timeout<T>(
&self,
topic: &Topic<T>,
timeout: Duration,
) -> EventBusResult<bool>where
T: 'static,
fn wait_for_idle_timeout<T>(
&self,
topic: &Topic<T>,
timeout: Duration,
) -> EventBusResult<bool>where
T: 'static,
Waits until local topic work is idle or the timeout elapses.