pub struct EventBusBuilder { /* private fields */ }Expand description
Builder for constructing an EventBus with custom configuration and
pre-registered handlers.
Obtain an instance via EventBus::builder(). All settings have sensible
defaults (see individual methods), so calling build on a
freshly created builder is valid and yields a ready-to-use bus.
§Handler registration
Use handler and dead_letter to
register handlers that will be subscribed automatically during
build. Dependencies required by those handlers are supplied
via deps.
let bus = EventBus::builder()
.buffer_size(256)
.handler(ProcessPaymentHandler)
.handler(AuditLogHandler)
.dead_letter(LogDeadLetterHandler)
.deps(Deps::new().insert(db).insert(mailer))
.build()
.await?;§Errors
build returns EventBusError::InvalidConfig when:
buffer_sizewas set to0.max_concurrent_asyncwas set to0.
build returns EventBusError::MissingDependency when a
registered handler requires a dependency that was not supplied via
deps.
Implementations§
Source§impl EventBusBuilder
impl EventBusBuilder
Sourcepub fn buffer_size(self, size: usize) -> Self
pub fn buffer_size(self, size: usize) -> Self
Set the internal channel buffer capacity.
This controls the maximum number of in-flight publish permits at any
given time. When the buffer is full, EventBus::publish will wait
until space becomes available, and EventBus::try_publish will
return EventBusError::ChannelFull immediately.
Default: 256.
§Errors
build will return an error if size is 0.
Sourcepub fn handler_timeout(self, timeout: Duration) -> Self
pub fn handler_timeout(self, timeout: Duration) -> Self
Set a per-invocation timeout for async handler tasks.
If an async handler does not complete within this duration it is
cancelled and treated as a failure (subject to the listener’s
SubscriptionPolicy). Sync handlers are not affected.
Default: no timeout (handlers may run indefinitely).
Sourcepub fn max_concurrent_async(self, max: usize) -> Self
pub fn max_concurrent_async(self, max: usize) -> Self
Sourcepub fn default_subscription_policy(self, policy: SubscriptionPolicy) -> Self
pub fn default_subscription_policy(self, policy: SubscriptionPolicy) -> Self
Set the fallback SubscriptionPolicy applied to every new subscription
that does not specify its own policy.
This policy is overridden on a per-subscription basis by
EventBus::subscribe_with_policy and friends.
Default: SubscriptionPolicy::default() — priority 0, no
retries, dead-letter enabled.
Sourcepub fn shutdown_timeout(self, timeout: Duration) -> Self
pub fn shutdown_timeout(self, timeout: Duration) -> Self
Set a deadline for draining in-flight async tasks during shutdown.
If tasks do not complete within this duration after EventBus::shutdown
is called they are aborted and shutdown returns
EventBusError::ShutdownTimeout.
Default: no timeout (shutdown waits indefinitely for tasks to finish).
Sourcepub fn handler(self, handler: impl HandlerDescriptor) -> Self
pub fn handler(self, handler: impl HandlerDescriptor) -> Self
Register a handler to be subscribed during build.
The handler’s HandlerDescriptor::register method is called once with
the newly-created bus and the Deps container provided via
deps. Any required dependencies must be present in Deps
by the time build is called or it will return
EventBusError::MissingDependency.
Lifetime: The handler’s subscription is held by the bus for its
entire lifetime. Builder-registered handlers cannot be individually
unsubscribed after build returns.
Note: This method accepts impl HandlerDescriptor. Passing a
dead-letter handler (which only implements DeadLetterDescriptor) is a
compile-time error — use dead_letter instead.
Sourcepub fn dead_letter(self, handler: impl DeadLetterDescriptor) -> Self
pub fn dead_letter(self, handler: impl DeadLetterDescriptor) -> Self
Register a dead-letter handler to be subscribed during build.
The handler’s DeadLetterDescriptor::register_dead_letter method is
called once. Dead-letter handlers must implement
SyncEventHandler<DeadLetter> — passing an
async handler is a compile-time error.
Lifetime: The handler’s subscription is held by the bus for its
entire lifetime. Builder-registered dead-letter handlers cannot be
individually unsubscribed after build returns.
Note: Passing a regular handler that only implements
HandlerDescriptor (not DeadLetterDescriptor) is a compile-time
error — use handler instead.
Sourcepub fn deps(self, deps: Deps) -> Self
pub fn deps(self, deps: Deps) -> Self
Supply the dependency container used to inject dependencies into registered handlers.
Call Deps::new() and chain insert calls to build
the container:
.deps(Deps::new().insert(db_pool).insert(mailer))Default: an empty Deps container (suitable when no handlers
require dependencies).
Sourcepub async fn build(self) -> Result<EventBus, EventBusError>
pub async fn build(self) -> Result<EventBus, EventBusError>
Consume the builder, construct the EventBus, and register all
handlers.
This method:
- Validates configuration (returns
EventBusError::InvalidConfigon invalid settings). - Creates the
EventBusruntime. - Calls
HandlerDescriptor::registerfor each handler added viahandler, in registration order. - Calls
DeadLetterDescriptor::register_dead_letterfor each handler added viadead_letter, in registration order.
§Errors
EventBusError::InvalidConfig—buffer_sizeormax_concurrent_asyncis0.EventBusError::MissingDependency— a registered handler requires a dependency that was not supplied viadeps.EventBusError::Stopped— should not occur during build, but propagated from subscription calls for completeness.