pub struct Publisher { /* private fields */ }Expand description
A Publisher client for the Cloud Pub/Sub API.
A Publisher sends messages to a specific topic. It manages message batching
and sending in a background task.
let publisher = Publisher::builder("projects/my-project/topics/my-topic").build().await?;
let message_id_future = publisher.publish(Message::new().set_data("Hello, World"));§Configuration
To configure Publisher 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 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
Publisher holds a connection pool internally, it is advised to
create one and then reuse it. You do not need to wrap Publisher in
an Rc or Arc to reuse it.
Implementations§
Source§impl Publisher
impl Publisher
Sourcepub fn builder<T: Into<String>>(topic: T) -> PublisherBuilder
pub fn builder<T: Into<String>>(topic: T) -> PublisherBuilder
Sourcepub fn publish(&self, msg: Message) -> PublishFuture ⓘ
pub fn publish(&self, msg: Message) -> PublishFuture ⓘ
Publishes a message to the topic.
When this method encounters a non-recoverable error publishing for an ordering key, it will pause publishing on all new messages on that ordering key. Any outstanding messages that have not yet been published may return an error.
let message_id = publisher.publish(Message::new().set_data("Hello, World")).await?;Sourcepub async fn flush(&self)
pub async fn flush(&self)
Flushes all buffered messages across all ordering keys, sending them immediately.
let _handle = publisher.publish(Message::new().set_data("event"));
// Ensures the message above is sent without needing to track its future.
publisher.flush().await;This method bypasses configured batching delays and returns only after all messages buffered at the time of the call have reached a terminal state (success or permanent failure).
§Recommendations
- For most use cases, we recommend you
.awaitthePublishFuturereturned bypublishto retrieve message IDs and handle specific errors. - Use
flush()as a convenience during application shutdown to ensure the client attempts to send all outstanding data.
Sourcepub fn resume_publish<T: Into<String>>(&self, ordering_key: T)
pub fn resume_publish<T: Into<String>>(&self, ordering_key: T)
Resume accepting publish for a paused ordering key.
Publishing using an ordering key might be paused if an error is encountered while publishing, to prevent messages from being published out of order. If the ordering key is not currently paused, this function is a no-op.
§Example
if let Err(_) = publisher.publish(Message::new().set_data("foo").set_ordering_key("bar")).await {
// Error handling code can go here.
publisher.resume_publish("bar");
}Trait Implementations§
Auto Trait Implementations§
impl Freeze for Publisher
impl RefUnwindSafe for Publisher
impl Send for Publisher
impl Sync for Publisher
impl Unpin for Publisher
impl UnsafeUnpin for Publisher
impl UnwindSafe for Publisher
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