Skip to main content

Subscription

Struct Subscription 

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

A subscription that delivers messages from a Topic to a handler function.

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

Declares a subscription to topic.

This constructor is const so it can initialize a module-scope static. Pass a closure returning a boxed future (|msg| Box::pin(send_welcome(msg))), or annotate the static with #[encore::pubsub::register] to pass send_welcome directly and start the subscription automatically at startup.

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

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

Sets how long a handler has to acknowledge a message before it is redelivered (defaults to 30 seconds).

For pull subscriptions, Encore automatically extends the lease for up to 1 hour (or this duration, if longer) while the handler is running; for push subscriptions (such as GCP push), this is the delivery timeout.

Source

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

Sets how long undelivered messages are retained before expiring (defaults to 7 days).

Source

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

Sets the minimum retry backoff after a failed delivery (defaults to 10 seconds).

Source

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

Sets the maximum retry backoff after a failed delivery (defaults to 10 minutes).

Source

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

Sets the maximum number of times a message is delivered, including the first delivery, before it is moved to the dead-letter queue (defaults to 100). Must be between 1 (no retries) and 100. On GCP, the minimum is 5, so lower values behave as 5.

Source

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

Limits how many messages this subscription processes concurrently in each service instance (defaults to 100). Must be at least 1. On an ordered topic, messages sharing an ordering key are processed one at a time.

It has no effect on GCP Cloud Run for unordered topics, whose messages are pushed with a concurrency GCP adapts.

Source

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

Filters which messages are delivered to this subscription based on their attributes. Unmatched messages are skipped before delivery (defaults to receiving all messages).

Uses a cloud-portable subset of the expr language:

.filter(r#"attrs.region in ["eu-west-1", "eu-north-1"] and not (attrs.kind startsWith "test-")"#)

Supported operations:

  • Comparisons: attrs.region == "eu", !=, in [...], not in [...], and startsWith "eu-" (use attrs["iana.org-lang"] for attribute names containing characters outside letters, digits, and _).
  • Presence checks: "region" in attrs and "region" not in attrs. Positive value checks evaluate to false if the attribute is missing, while negated checks evaluate to true.
  • Boolean logic: and (&&), or (||), not (...) (!(...)), and parentheses.

Must be a string literal so Encore can parse and validate it against cloud provider limits.

Source

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

Assigns this subscription to a named worker declared in the workers section of the service’s encore.service.json.

Only that worker process consumes messages for this subscription. If omitted, the service’s main process consumes it. Must be a string literal.

Source

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

Attaches a key/value tag to the subscription (for example, .tag("data", "pci")) for policy and governance tooling.

Call once per tag with unique keys. Both key and value must be string literals so Encore can parse them statically. Keys must start with a letter, contain only lowercase letters, digits, _, and -, and must not start with encore (reserved for Encore’s own tags). Values use the same character set or may be empty. Each key and value is at most 63 characters, and a subscription may have at most 32 tags.

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

Returns the name this subscription was declared with.

Source§

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

Source

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

Starts consuming messages for this subscription if it has not already been started.

Subscriptions annotated with #[encore::pubsub::register] are started automatically; call this manually for unregistered subscriptions.

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>

§

impl<T> Send for Subscription<T>
where &'static str: Send, Atomic<bool>: Send,

§

impl<T> Sync for Subscription<T>
where Atomic<bool>: Sync,

§

impl<T> Unpin for Subscription<T>
where &'static str: Unpin, Align1<u8>: Unpin,

§

impl<T> UnsafeUnpin for Subscription<T>
where &'static str: UnsafeUnpin, Align1<u8>: UnsafeUnpin,

§

impl<T> UnwindSafe for Subscription<T>
where &'static str: UnwindSafe, Align1<u8>: 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