Skip to main content

PgOutboxWorkerBuilder

Struct PgOutboxWorkerBuilder 

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

Fluent builder for an OutboxWorker backed by PgOutboxStore.

§Pool sizing and acquire timeout

The worker and every concurrent publisher draw connections from the same pool. To avoid indefinite blocking under pressure, configure an acquire timeout on the PgPool before passing it here:

use sqlx::postgres::PgPoolOptions;
use std::time::Duration;

let pool = PgPoolOptions::new()
    // 1 connection for the claim cycle + 1 per concurrent publisher + 2 headroom
    .max_connections(batch_size + num_publishers + 2)
    // surface PoolTimeout instead of blocking indefinitely
    .acquire_timeout(Duration::from_secs(5))
    .connect("postgres://...")
    .await?;

let worker = PgOutboxWorkerBuilder::new(pool)
    .batch_size(batch_size)
    .build()?;

When acquire_timeout expires, OutboxStore::acquire returns OutboxError::PoolTimeout instead of hanging. The worker logs the error and retries after OutboxWorkerConfig::poll_interval.

Implementations§

Source§

impl PgOutboxWorkerBuilder

Source

pub fn new(pool: PgPool) -> Self

Start a new builder for the given pool.

Source

pub fn table_name(self, name: impl Into<String>) -> Self

Override the outbox table name (default "audit_outbox").

Source

pub fn dead_letter_table(self, name: impl Into<String>) -> Self

Enable dead-letter persistence for poison messages.

Envelopes that exhaust their retry budget are atomically moved to dlq_table (INSERT + DELETE in the same transaction). When not set, exhausted envelopes are logged via tracing::error! but not moved.

Source

pub fn register_handler<E, H>(self, handler: H) -> Self
where E: Event, H: Handler<E>,

Register a typed handler for the event type E.

Registering twice for the same event type silently replaces the previous handler.

Source

pub fn shared_handler<E, H>(self, handler: Arc<H>) -> Self
where E: Event, H: Handler<E>,

Register a handler already shared behind an Arc.

Source

pub fn poll_interval(self, d: Duration) -> Self

Override the poll interval (default 100 ms).

Source

pub fn batch_size(self, n: usize) -> Self

Override the batch size per poll (default 10).

Source

pub fn max_attempts(self, n: u32) -> Self

Override the maximum number of attempts per envelope (default 5).

Source

pub fn retry_base_delay(self, d: Duration) -> Self

Override the base delay for exponential backoff (default 1 s).

The actual delay before attempt n is min(retry_max_delay, base × 2^n), optionally jittered. See OutboxWorkerConfig::retry_base_delay.

Source

pub fn retry_max_delay(self, d: Duration) -> Self

Override the maximum backoff delay (default 5 min).

Caps retry_base_delay × 2^n regardless of the attempt count.

Source

pub fn jitter(self, enabled: bool) -> Self

Enable or disable full jitter on the backoff delay (default true).

When true the worker draws a uniform random value in [0, computed_delay] instead of using the deterministic exponential.

Source

pub fn dispatch_timeout(self, d: Duration) -> Self

Override the per-envelope handler deadline and soft-lease unit (default 30 s).

Each handler invocation is wrapped in a hard tokio timeout of this duration; the batch lease is sized as batch_size x dispatch_timeout internally. Set it to the worst-case duration of a single handler.

Source

pub fn build(self) -> Result<OutboxWorker<PgOutboxStore>, OutboxError>

Consume the builder and produce an OutboxWorker ready to spawn.

§Errors

Returns OutboxError::Internal if the configured table_name is not a valid identifier.

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: 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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