#[non_exhaustive]pub enum Handler {
AtLeastOnce(AtLeastOnce),
ExactlyOnce(ExactlyOnce),
}Expand description
A handler for acknowledging or rejecting messages.
§Example
use google_cloud_pubsub::model::Message;
fn on_message(m: Message, h: Handler) {
match process(m) {
Ok(_) => h.ack(),
Err(e) => {
println!("failed to process message: {e:?}");
h.nack();
}
}
}
fn process(m: Message) -> anyhow::Result<()> {
// some business logic here...
}To acknowledge (ack) a message, you call Handler::ack().
To reject (nack) a message, you call Handler::nack(). The
service will redeliver the message.
§Delivery attempts
If the subscription has a dead-letter topic configured, you can retrieve the delivery attempt count.
fn on_message(h: Handler) {
match h.delivery_attempt() {
Some(i) => println!("Delivery attempt: {i}"),
None => println!("Delivery attempt: unknown"),
}
}§Exactly-once delivery
If your subscription has exactly-once delivery enabled, you should
destructure this enum into its Handler::ExactlyOnce branch.
Only when ExactlyOnce::confirmed_ack() returns Ok can you be certain
that the message will not be redelivered.
use google_cloud_pubsub::model::Message;
async fn on_message(m: Message, h: Handler) {
let Handler::ExactlyOnce(h) = h else {
panic!("Oops, my subscription does not have exactly-once delivery enabled.")
};
match h.confirmed_ack().await {
Ok(()) => println!("Confirmed ack for message={m:?}. The message will not be redelivered."),
Err(e) => println!("Failed to confirm ack for message={m:?} with error={e:?}"),
}
}Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
AtLeastOnce(AtLeastOnce)
A handler for at-least-once delivery.
The handler type is determined by the subscription configuration.
if let Handler::AtLeastOnce(h) = h {
h.ack();
}ExactlyOnce(ExactlyOnce)
A handler for exactly-once delivery.
The handler type is determined by the subscription configuration.
if let Handler::ExactlyOnce(h) = h {
h.confirmed_ack().await?;
}Implementations§
Source§impl Handler
impl Handler
Sourcepub fn ack(self)
pub fn ack(self)
Acknowledge the message associated with this handler.
§Example
use google_cloud_pubsub::model::Message;
fn on_message(m: Message, h: Handler) {
println!("Received message: {m:?}");
h.ack();
}Note that the acknowledgement is best effort. The message may still be redelivered to this client, or another client, even if exactly-once delivery is enabled on the subscription.
Sourcepub fn nack(self)
pub fn nack(self)
Rejects the message associated with this handler.
§Example
use google_cloud_pubsub::model::Message;
fn on_message(m: Message, h: Handler) {
println!("Received message: {m:?}");
h.nack();
}The message will be removed from this Subscriber’s lease management.
The service will redeliver this message, possibly to another client.
Sourcepub fn delivery_attempt(&self) -> Option<i32>
pub fn delivery_attempt(&self) -> Option<i32>
Returns the delivery attempt count for this message, if available.
§Example
fn on_message(h: Handler) {
match h.delivery_attempt() {
Some(i) => println!("Delivery attempt: {i}"),
None => println!("Delivery attempt: unknown"),
}
}This returns None if dead-letter topics are not configured on the
subscription.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Handler
impl !RefUnwindSafe for Handler
impl Send for Handler
impl Sync for Handler
impl Unpin for Handler
impl UnsafeUnpin for Handler
impl !UnwindSafe for Handler
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
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> 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