Skip to main content

ConnectedSqlxBroker

Struct ConnectedSqlxBroker 

Source
pub struct ConnectedSqlxBroker<DB: Database, D = BuiltIn<DB>> { /* private fields */ }
Expand description

The connected inbox broker: the typed witness that the database answered.

It hands out publishers and opens subscriptions; shutdown stops its claim loops and the extension of its leases, and refuses every handle it handed out. A delivery in work keeps its lease after shutdown and settles as before; its lease is no longer extended. In the advisory lock form shutdown releases the lock of every delivery in work and returns once no lock of the broker is left; such a delivery’s settlement then fails with SqlxBrokerError::Closed.

§Examples

use ruststream::OutgoingMessage;
use ruststream_sqlx::prelude::*;
use serde::Serialize;
use sqlx::{PgConnection, PgPool, Postgres};

#[derive(Inbox, sqlx::FromRow)]
#[inbox(table = "cleanup_jobs")]
pub struct Cleanup {
    #[field(id, generated)]
    id: i64,
    #[field(payload)]
    payload: Vec<u8>,
}

impl Publish<Postgres> for Cleanup {
    async fn publish(
        conn: &mut PgConnection,
        message: &OutgoingMessage<'_>,
    ) -> Result<(), sqlx::Error> {
        sqlx::query("INSERT INTO cleanup_jobs (payload) VALUES ($1)")
            .bind(message.payload())
            .execute(conn)
            .await?;
        Ok(())
    }
}

#[derive(Serialize, Outgoing)]
#[outgoing(name = "cleanup")]
struct Sweep {
    older_than_days: u32,
}

pub fn app(pool: PgPool) -> RustStream {
    RustStream::new(AppInfo::new("maintenance", "0.1.0")).with_broker(SqlxBroker::new(pool), |b| {
        // Once the broker connected, the policy pairs against the `ConnectedSqlxBroker` and the
        // hook schedules the first sweep.
        b.after_startup(Repository::<Cleanup>::default(), async move |cleanups| {
            cleanups.message(&Sweep { older_than_days: 30 }).publish().await
        });
    })
}

Trait Implementations§

Source§

impl<DB: QueueDatabase, D: Dialect + 'static> ConnectedBroker for ConnectedSqlxBroker<DB, D>

Source§

fn shutdown( self, ) -> impl Future<Output = Result<Self::Closed, Self::Error>> + Send

Stops the claim loops and the extension of every lease, and refuses every handle handed out before. A delivery in work in the lease or row lock form settles as before; its lease runs out unless it settles first.

In the advisory lock form it then releases the lock of every delivery in work: each session unlocks its key and goes back to the pool, or closes where the database does not confirm the release. It waits for each session lent to a settlement in flight, and for the sessions still closing after a delivery or a claim was dropped, and returns once no lock of the broker is left. In a service the runtime has stopped its subscriptions by then, and its shutdown timeout bounds the handlers that hold a session.

Source§

type Error = SqlxBrokerError

The error type returned by connected-broker operations.
Source§

type Closed = ClosedSqlxBroker

The terminal witness returned by shutdown. Read more
Source§

impl<DB: Database, D: Dialect> Debug for ConnectedSqlxBroker<DB, D>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<DB: QueueDatabase, D: Dialect + 'static> DefaultPublish for ConnectedSqlxBroker<DB, D>

Source§

type Policy = Routed

The broker’s plain publish policy, constructible with its defaults.
Source§

impl<DB, D, Row> PublishPolicy<ConnectedSqlxBroker<DB, D>> for Repository<Row>
where DB: QueueDatabase, D: Dialect + 'static, Row: Publish<DB> + Events<DB>,

Source§

type Live = RepositoryPublisher<DB, Row>

The live form this policy pairs into: a Publisher for a leaf policy, or the live wiring form for a combinator stack (a typed publisher over a policy pairs into the same typed publisher over the live leaf).
Source§

fn pair( self, connected: &ConnectedSqlxBroker<DB, D>, ) -> impl Future<Output = Result<Self::Live, PairError>> + Send

Pairs the policy with a connected broker, producing the live publisher. Read more
Source§

fn channel_bindings(&self, channel: &str) -> Bindings

What a publish through this policy adds to its channel in the generated AsyncAPI document. Read more
Source§

fn operation_bindings(&self, channel: &str) -> Bindings

What a publish through this policy adds to its send operation in the document. Read more
Source§

fn message_bindings(&self, channel: &str) -> Bindings

What a publish through this policy adds to the messages that leave through it. Read more
Source§

fn reply_address_location(&self) -> Option<&'static str>

Where a client finds the address of a reply this policy publishes, as a runtime expression. Read more
Source§

impl<DB: QueueDatabase, D: Dialect + 'static> PublishPolicy<ConnectedSqlxBroker<DB, D>> for Routed

Source§

type Live = RoutedPublisher<DB>

The live form this policy pairs into: a Publisher for a leaf policy, or the live wiring form for a combinator stack (a typed publisher over a policy pairs into the same typed publisher over the live leaf).
Source§

fn pair( self, connected: &ConnectedSqlxBroker<DB, D>, ) -> impl Future<Output = Result<Self::Live, PairError>> + Send

Pairs the policy with a connected broker, producing the live publisher. Read more
Source§

fn channel_bindings(&self, channel: &str) -> Bindings

What a publish through this policy adds to its channel in the generated AsyncAPI document. Read more
Source§

fn operation_bindings(&self, channel: &str) -> Bindings

What a publish through this policy adds to its send operation in the document. Read more
Source§

fn message_bindings(&self, channel: &str) -> Bindings

What a publish through this policy adds to the messages that leave through it. Read more
Source§

fn reply_address_location(&self) -> Option<&'static str>

Where a client finds the address of a reply this policy publishes, as a runtime expression. Read more
Source§

impl<DB, D> Subscribe for ConnectedSqlxBroker<DB, D>
where DB: QueueDatabase + RoleColumns, D: ByName<DB> + 'static,

Source§

type Subscriber = NamedSubscriber<DB, D>

The subscriber type opened by a by-name subscription.
Source§

type Copies = BrokerMoves

Who publishes the copies a retry of a by-name subscription is made of, and who names where they go. This is what the Name source reports, so it decides how #[subscriber("orders")] retries on this broker. Read more
Source§

async fn subscribe( &self, name: &str, ) -> Result<Self::Subscriber, SqlxBrokerError>

Opens a subscription to name, producing this broker’s Subscriber. Read more
Source§

fn declare_retry( &self, name: &str, declaration: &RetryDeclaration, ) -> Result<(), DeclareRetryError>

Takes what a registration mounted by a bare name declared about its retries, before the subscription opens. Read more
Source§

impl<DB, D, Row, Mode> SubscriptionSource<ConnectedSqlxBroker<DB, D>> for InboxQueue<Row, Mode>
where DB: QueueDatabase, D: Dialect + Opens<Row::Opening> + 'static, Row: InboxRow + Events<DB>, Row::Form: FormOn<D>, Mode: InboxMode,

Source§

type Subscriber = InboxSubscriber<DB, Row, Mode>

The subscriber type this source opens.
Source§

type Copies = BrokerMoves

Who publishes the copies this subscription’s retries are made of, and who names where they go: AddressedCopies, NamedCopies or BrokerMoves. Read more
Source§

fn name(&self) -> &str

The name (subject / channel) this subscription binds to. Read more
Source§

async fn subscribe( self, connected: &ConnectedSqlxBroker<DB, D>, ) -> Result<Self::Subscriber, SqlxBrokerError>

Opens the subscription against the connected broker. Called once at startup. Read more
Source§

fn declare_retry(self, declaration: &RetryDeclaration) -> Self

Takes the registration’s retry declaration into the descriptor, before the subscription opens. Read more
Source§

fn channel_bindings(&self) -> Bindings

What this subscription adds to its channel in the generated AsyncAPI document. Read more
Source§

fn declare_retry_on( &self, _connected: &ConnectedSqlxBroker<DB, D>, declaration: &RetryDeclaration, ) -> Result<(), DeclareRetryError>

Carries the registration’s declaration to the broker, at the same point and before the subscription opens. Read more
Source§

fn operation_bindings(&self) -> Bindings

What this subscription adds to its receive operation in the document. Read more
Source§

fn message_bindings(&self) -> Bindings

What this subscription adds to the messages that arrive on it. Read more
Source§

impl<DB: QueueDatabase, D: Dialect + 'static> TestableBroker for ConnectedSqlxBroker<DB, D>

Source§

fn install_coordinator(&self, coordinator: Coordinator)

Installs the harness coordinator into this broker’s bus for a test run. Idempotent: a second install on the same broker is ignored.
Source§

fn inject(&self, message: OutgoingMessage<'_>)

Injects a message onto the bus as an external producer would, synchronously (no awaiting). Routes through the broker’s normal fanout, so it is recorded and counted like any publish. Read more
Source§

fn published(&self, name: &str) -> Vec<RawMessage>

Returns every message published to name on this broker, in publish order. Backs the harness’s published::<T>(name) assertions and expect_published.
Source§

fn backlog(&self) -> Backlog

What a subscription opened by name receives of the messages published to that name before it opened, as the real broker answers it: Backlog::Missed for publish/subscribe, Backlog::Delivered for a queue or a log that keeps them. Read more
Source§

fn routes(&self, destination: &str, subscriptions: &[&str]) -> Vec<usize>

Which subscriptions of this broker a message published to destination is delivered to, as the broker’s own routing picks them: the positions in subscriptions (the names the app subscribed under on this broker) of the ones it delivers to. Read more

Auto Trait Implementations§

§

impl<DB, D = BuiltIn<DB>> !RefUnwindSafe for ConnectedSqlxBroker<DB, D>

§

impl<DB, D = BuiltIn<DB>> !UnwindSafe for ConnectedSqlxBroker<DB, D>

§

impl<DB, D> Freeze for ConnectedSqlxBroker<DB, D>
where Arc<Shared<DB>>: Freeze, Arc<D>: Freeze,

§

impl<DB, D> Send for ConnectedSqlxBroker<DB, D>
where Arc<Shared<DB>>: Send, Arc<D>: Send,

§

impl<DB, D> Sync for ConnectedSqlxBroker<DB, D>
where Arc<Shared<DB>>: Sync, Arc<D>: Sync,

§

impl<DB, D> Unpin for ConnectedSqlxBroker<DB, D>
where Arc<Shared<DB>>: Unpin, Arc<D>: Unpin,

§

impl<DB, D> UnsafeUnpin for ConnectedSqlxBroker<DB, D>
where Arc<Shared<DB>>: UnsafeUnpin, Arc<D>: UnsafeUnpin,

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, W> HasTypeWitness<W> for T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

Source§

const WITNESS: W = W::MAKE

A constant of the type witness
Source§

impl<T> Identity for T
where T: ?Sized,

Source§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = TypeEq::NEW

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
Source§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
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> PublishedThrough<DefaultSlot> for T

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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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