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>
impl<T> Subscription<T>
Sourcepub const fn new<E: Encoding<T>, O>(
topic: &'static Topic<T, E, O>,
name: &'static str,
handler: Handler<T>,
) -> Self
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);Sourcepub const fn ack_deadline(self, _deadline: Duration) -> Self
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.
Sourcepub const fn message_retention(self, _retention: Duration) -> Self
pub const fn message_retention(self, _retention: Duration) -> Self
How long an undelivered message is kept. Seven days if not given.
Sourcepub const fn min_backoff(self, _backoff: Duration) -> Self
pub const fn min_backoff(self, _backoff: Duration) -> Self
The shortest wait before a failed message is delivered again. 10 seconds if not given.
Sourcepub const fn max_backoff(self, _backoff: Duration) -> Self
pub const fn max_backoff(self, _backoff: Duration) -> Self
The longest wait before a failed message is delivered again. 10 minutes if not given.
Sourcepub const fn max_retries(self, _retries: u32) -> Self
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.
Sourcepub const fn retry_forever(self) -> Self
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.
Sourcepub const fn max_concurrency(self, _concurrency: u32) -> Self
pub const fn max_concurrency(self, _concurrency: u32) -> Self
How many messages are handled at once. Unlimited if not given.
Sourcepub const fn filter(self, _filter: &'static str) -> Self
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 [...], andstartsWith "eu-";attrs["iana.org-lang"]for a name with other characters than letters, digits and_."region" in attrsand"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.
Sourcepub const fn worker(self, _worker: &'static str) -> Self
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.
Sourcepub const fn tag(self, _key: &'static str, _value: &'static str) -> Self
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§impl<T: Send + 'static> Subscription<T>
impl<T: Send + 'static> Subscription<T>
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>
impl<T> Sync for Subscription<T>
impl<T> Unpin for Subscription<T>
impl<T> UnsafeUnpin for Subscription<T>
impl<T> UnwindSafe for Subscription<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request