Skip to main content

Topic

Struct Topic 

Source
pub struct Topic<T> { /* private fields */ }
Expand description

One topic under one key. Cheap to build; hold none. subscribe and channel are the [TopicHandle]’s; publish is this type’s own, so a topic bound to an @isolation attempt rides it.

Implementations§

Source§

impl<T> Topic<T>
where T: Serialize + DeserializeOwned + Send + 'static,

Source

pub fn new( backplane: Arc<dyn SubscriptionBackplane>, topic: &str, key: impl Display, ) -> Self

topic is the declared name (RoomMessage), key the key’s value.

Source

pub async fn publish(&self, message: &T) -> Result<(), CratestackError>

Delivers message to every current subscriber, on every replica. From an @isolation procedure’s handle it is delivered only if the attempt commits, never for one that rolls back or retries: with PgNotifyBackplane as Self::publish_in_tx in a savepoint of the attempt, with any other backplane queued and sent after the commit. Inside db.transaction(..) there, use publish_in_tx(tx, ..).

Source

pub async fn publish_in_tx( &self, tx: &mut Transaction<'_, Postgres>, message: &T, ) -> Result<(), CratestackError>

Delivers message only if tx commits, when it commits. On a topic from an @isolation procedure’s handle, a backplane the transaction cannot notify gets it queued until the attempt commits (dropped if this db.transaction(..) rolls back). Otherwise such a backplane (InProcessBackplane) is refused: call Self::publish after the commit instead.

Methods from Deref<Target = TopicHandle<T>>§

pub fn channel(&self) -> &str

The backplane channel: cratestack.topic.<Topic>.<key>.

pub fn backplane(&self) -> &Arc<dyn SubscriptionBackplane>

The backplane the topic publishes on.

pub fn encode(&self, message: &T) -> Result<String, CratestackError>

message as the JSON payload the backplane carries.

pub async fn publish(&self, message: &T) -> Result<(), CratestackError>

Delivers message to every current subscriber, on every replica.

pub async fn subscribe( &self, ) -> Result<Pin<Box<dyn Stream<Item = Result<T, CratestackError>> + Send>>, CratestackError>

Every message published from now on. Resolves once the subscription is live. Ends with unavailable when it falls behind ("subscription lagged") or the backplane may have lost messages ("subscription interrupted"): resubscribe and refetch.

Trait Implementations§

Source§

impl<T> Deref for Topic<T>

Source§

type Target = TopicHandle<T>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Topic<T>

§

impl<T> !UnwindSafe for Topic<T>

§

impl<T> Freeze for Topic<T>

§

impl<T> Send for Topic<T>

§

impl<T> Sync for Topic<T>

§

impl<T> Unpin for Topic<T>

§

impl<T> UnsafeUnpin for Topic<T>

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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.
§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more