Skip to main content

EventBusBuilder

Struct EventBusBuilder 

Source
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_size was set to 0.
  • max_concurrent_async was set to 0.

build returns EventBusError::MissingDependency when a registered handler requires a dependency that was not supplied via deps.

Implementations§

Source§

impl EventBusBuilder

Source

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.

Source

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).

Source

pub fn max_concurrent_async(self, max: usize) -> Self

Set the maximum number of async handler tasks that may run concurrently.

When this limit is reached, new async dispatches wait until a running task finishes. Sync handlers are not counted toward this limit.

Default: unlimited (None).

§Errors

build will return an error if max is 0.

Source

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.

Source

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).

Source

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.

Source

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.

Source

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).

Source

pub async fn build(self) -> Result<EventBus, EventBusError>

Consume the builder, construct the EventBus, and register all handlers.

This method:

  1. Validates configuration (returns EventBusError::InvalidConfig on invalid settings).
  2. Creates the EventBus runtime.
  3. Calls HandlerDescriptor::register for each handler added via handler, in registration order.
  4. Calls DeadLetterDescriptor::register_dead_letter for each handler added via dead_letter, in registration order.
§Errors

Trait Implementations§

Source§

impl Debug for EventBusBuilder

Source§

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

Formats the value using the given formatter. 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, 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, 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> Event for T
where T: Send + Sync + 'static,