Skip to main content

PgNotifyBus

Struct PgNotifyBus 

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

Process-wide PostgreSQL LISTEN multiplexer.

Create one at runtime startup, register all channels up front, then call run in a background task. Subsystems obtain a broadcast::Receiver<String> via subscribe.

Implementations§

Source§

impl PgNotifyBus

Source

pub fn new(pool: PgPool, channels: &[&str]) -> Self

Create a new notify bus for the given channels.

Each channel gets a broadcast::channel(256) so subscribers can lag slightly without losing messages. The bus does not start listening until run is called.

Source

pub fn subscribe(&self, channel: &str) -> Option<Receiver<String>>

Subscribe to notifications on channel.

Returns None if channel was not registered at construction time. The returned receiver yields the raw NOTIFY payload string.

Source

pub fn channels(&self) -> Vec<&str>

Returns the set of channel names this bus is configured for.

Source

pub fn subscribe_reconnects(&self) -> Receiver<u64>

Subscribe to reconnect events.

The returned receiver’s value is bumped once per successful (re)connect of the underlying PgListener. The initial connect publishes 1; subsequent reconnects publish 2, 3, etc. Subscribers that want to trigger gap recovery on reconnect should:

  1. Call subscribe_reconnects() and snapshot the current generation via *rx.borrow().
  2. In their main loop, select! on rx.changed() alongside their payload recv().
  3. On a change, compare *rx.borrow() to the snapshot and only treat it as a reconnect if it is strictly greater than the snapshot — this filters the first-boot connect for subscribers that attach before run() succeeds.
Source

pub async fn run(&self, shutdown: Receiver<bool>)

Run the listener loop until shutdown fires.

This must be spawned as a background task. It owns the single PgListener connection, reconnects on failure, and fans out every received notification to the matching broadcast channel.

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. 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