Skip to main content

Subscription

Struct Subscription 

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

A subscription to a topic: every message published to the topic is handed to its handler.

Implementations§

Source§

impl<T> Subscription<T>

Source

pub const fn new<E: Encoding<T>, O>( topic: &'static Topic<T, E, O>, name: &'static str, handler: Handler<T>, ) -> Self

Declare a subscription to topic. const so it can initialize a module-scope static.

The handler is a function, boxing the future that handles a message: |msg| Box::pin(send_welcome(msg)). Under #[encore::pubsub::register], which also has the subscription started with the app, it can be just send_welcome.

Options follow, each a method of its own:

static WELCOME: Subscription<String> =
    Subscription::new(&SIGNUPS, "send-welcome", |msg| Box::pin(send_welcome(msg)))
        .ack_deadline(Duration::from_secs(60))
        .max_retries(5);
Source

pub const fn ack_deadline(self, _deadline: Duration) -> Self

How long a message is kept for its handler before it is delivered again. While the handler runs, a pulled message’s lease is extended for an hour, or for this long when that is longer; a message pushed to the service (a GCP push subscription) has this long. 30 seconds if not given.

The options are part of the declaration, read by Encore’s parser: each takes the subscription by value, so it cannot be called on the static.

Source

pub const fn message_retention(self, _retention: Duration) -> Self

How long an undelivered message is kept. Seven days if not given.

Source

pub const fn min_backoff(self, _backoff: Duration) -> Self

The shortest wait before a failed message is delivered again. 10 seconds if not given.

Source

pub const fn max_backoff(self, _backoff: Duration) -> Self

The longest wait before a failed message is delivered again. 10 minutes if not given.

Source

pub const fn max_retries(self, _retries: u32) -> Self

How many times a failed message is delivered again before it is dead-lettered. 100 if not given; 0 dead-letters it on the first failure. To never dead-letter it, use retry_forever instead.

Source

pub const fn retry_forever(self) -> Self

Delivers a failed message again for as long as its handler fails: it is never dead-lettered. In place of max_retries.

Source

pub const fn max_concurrency(self, _concurrency: u32) -> Self

How many messages are handled at once. Unlimited if not given.

Source

pub const fn filter(self, _filter: &'static str) -> Self

Which of the topic’s messages the subscription receives: those whose attributes filter matches. Messages it does not match are never delivered to the subscription. All of them if not given.

It is written in a subset of the expr language, which every cloud Encore deploys to can filter on:

.filter(r#"attrs.region in ["eu-west-1", "eu-north-1"] and not (attrs.kind startsWith "test-")"#)
  • attrs.region == "eu", !=, in [...], not in [...], and startsWith "eu-"; attrs["iana.org-lang"] for a name with other characters than letters, digits and _.
  • "region" in attrs and "region" not in attrs: whether a message has the attribute. A test of a value does not hold for a message without the attribute, and a negated one does.
  • and (&&), or (||), not (...) (!(...)) and parentheses.

Encore reads it from the source, so it is a string written out, and checks it fits the clouds’ limits.

Source

pub const fn worker(self, _worker: &'static str) -> Self

The Worker of the service that consumes the subscription: a long-running process of the service that serves no requests, declared in the workers section of its encore.service.json. Only that process takes its messages; another process of the service that declares the subscription takes none. The service’s main process consumes it if not given.

Encore reads it from the source, so it is a string written out.

Source

pub const fn tag(self, _key: &'static str, _value: &'static str) -> Self

Tags the subscription with key and value: key/value classification, such as .tag("data", "pci"), for policies to read. One call per tag, each with its own key. Encore reads them from the source, so both are string literals. A key is lowercase letters, digits, _ and -, starting with a letter and not with encore, which is kept for Encore’s own tags; a value is the same characters, or empty. Each is at most 63 characters, and a subscription has at most 32 tags.

Part of the declaration: it takes the subscription by value, so it cannot be called on the static.

static WELCOME: Subscription<String> =
    Subscription::new(&SIGNUPS, "send-welcome", |msg| Box::pin(send_welcome(msg)))
        .tag("team", "growth");
Source

pub const fn name(&self) -> &'static str

The name the subscription was declared with.

Source§

impl<T: Send + 'static> Subscription<T>

Source

pub fn start(&'static self) -> Result<(), Error>

Starts handling the topic’s messages, if it has not been started.

A subscription under #[encore::pubsub::register] is started with the app; this is for one that is not.

Auto Trait Implementations§

§

impl<T> !Freeze for Subscription<T>

§

impl<T> Forget for Subscription<T>

§

impl<T> Move for Subscription<T>

§

impl<T> RefUnwindSafe for Subscription<T>
where fn(&[u8]) -> Result<T, Error>: RefUnwindSafe, fn(Message<T>) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>: RefUnwindSafe,

§

impl<T> Send for Subscription<T>
where fn(&[u8]) -> Result<T, Error>: Send, fn(Message<T>) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>: Send,

§

impl<T> Sync for Subscription<T>
where fn(&[u8]) -> Result<T, Error>: Sync, fn(Message<T>) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>: Sync,

§

impl<T> Unpin for Subscription<T>
where fn(&[u8]) -> Result<T, Error>: Unpin, fn(Message<T>) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>: Unpin,

§

impl<T> UnsafeUnpin for Subscription<T>
where fn(&[u8]) -> Result<T, Error>: UnsafeUnpin, fn(Message<T>) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>: UnsafeUnpin,

§

impl<T> UnwindSafe for Subscription<T>
where fn(&[u8]) -> Result<T, Error>: UnwindSafe, fn(Message<T>) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>: UnwindSafe,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T, U> ExactFrom<T> for U
where U: TryFrom<T>,

Source§

fn exact_from(value: T) -> U

Source§

impl<T, U> ExactInto<U> for T
where U: ExactFrom<T>,

Source§

fn exact_into(self) -> U

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self> ⓘ

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

impl<T, U> OverflowingInto<U> for T
where U: OverflowingFrom<T>,

Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + 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: Sized + 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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> RoundingInto<U> for T
where U: RoundingFrom<T>,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> SaturatingInto<U> for T
where U: SaturatingFrom<T>,

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

impl<T, U> WrappingInto<U> for T
where U: WrappingFrom<T>,

Source§

fn wrapping_into(self) -> U