pub struct Subscriber { /* private fields */ }Expand description
A Subscriber client for the Cloud Pub/Sub API.
Use this client to receive messages from a pull subscription on a topic.
§Example
let client = Subscriber::builder().build().await?;
let mut stream = client
.subscribe("projects/my-project/subscriptions/my-subscription")
.build();
while let Some((m, h)) = stream.next().await.transpose()? {
println!("Received message m={m:?}");
h.ack();
}§Ordered Delivery
The subscriber returns messages in order if ordered delivery is enabled on the subscription. The client provides the same guarantees as the service.
For more details on how the service works, see:
§Exactly-once Delivery
The subscriber supports exactly-once delivery.
If you enable exactly-once delivery for a subscription, your application
can be opinionated about the delivery type, by destructuring the handler
into its Handler::ExactlyOnce branch.
use google_cloud_pubsub::subscriber::MessageStream;
use google_cloud_pubsub::subscriber::handler::Handler;
async fn exactly_once_stream(mut stream: MessageStream) -> anyhow::Result<()> {
while let Some((m, Handler::ExactlyOnce(h))) = stream.next().await.transpose()? {
println!("Received message m={m:?}");
// Await the result of the ack. Typically you would not block the loop
// with an `await` point like this.
h.confirmed_ack().await?;
}
unreachable!("Oops, my subscription must have at-least-once semantics")
}You should not change the delivery type of a subscription midstream. If you do, the subscriber will honor the delivery setting at the time each message was received.
§Configuration
To configure a Subscriber use the with_* methods in the type returned by
builder(). The default configuration should work for
most applications. Common configuration changes include:
- with_endpoint(): by default this client uses the global default endpoint
(
https://pubsub.googleapis.com). Applications using regional endpoints or running in restricted networks (e.g. a network configured with Private Google Access with VPC Service Controls) may want to override this default. - with_credentials(): by default this client uses Application Default Credentials. Applications using custom authentication may need to override this default.
§Pooling and Cloning
Subscriber holds a connection pool internally, it is advised to
create one and then reuse it. You do not need to wrap Subscriber in
an Rc or Arc to reuse it, because it already uses an Arc
internally.
Implementations§
Source§impl Subscriber
impl Subscriber
Sourcepub fn builder() -> ClientBuilder
pub fn builder() -> ClientBuilder
Sourcepub fn subscribe<T>(&self, subscription: T) -> Subscribe
pub fn subscribe<T>(&self, subscription: T) -> Subscribe
Receive messages from a subscription.
The subscription is the full name, in the format of
projects/*/subscriptions/*.
§Example
let mut stream = client
.subscribe("projects/my-project/subscriptions/my-subscription")
.build();
while let Some((m, h)) = stream.next().await.transpose()? {
println!("Received message m={m:?}");
h.ack();
}Trait Implementations§
Source§impl Clone for Subscriber
impl Clone for Subscriber
Source§fn clone(&self) -> Subscriber
fn clone(&self) -> Subscriber
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for Subscriber
impl !RefUnwindSafe for Subscriber
impl Send for Subscriber
impl Sync for Subscriber
impl Unpin for Subscriber
impl UnsafeUnpin for Subscriber
impl !UnwindSafe for Subscriber
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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