pub struct MessageStream { /* private fields */ }Expand description
Represents an open subscribe stream.
This is a stream-like struct for serving messages to an application.
§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();
}Implementations§
Source§impl MessageStream
impl MessageStream
Sourcepub async fn next(&mut self) -> Option<Result<(Message, Handler)>>
pub async fn next(&mut self) -> Option<Result<(Message, Handler)>>
Returns the next message received on this subscription.
§Example
while let Some((m, h)) = stream.next().await.transpose()? {
println!("Received message m={m:?}");
h.ack();
}Returns the message data along with a Handler to acknowledge (ack) the message.
If the underlying stream encounters a permanent error, an Error is
returned instead.
None represents the end of a stream, but in practice, the stream stays
open until it is cancelled or encounters a permanent error.
Sourcepub fn into_stream(
self,
) -> impl Stream<Item = Result<(Message, Handler)>> + Unpin
Available on crate feature unstable-stream only.
pub fn into_stream( self, ) -> impl Stream<Item = Result<(Message, Handler)>> + Unpin
unstable-stream only.Converts the MessageStream to a futures::Stream.
§Example
use futures::TryStreamExt;
let mut stream = stream.into_stream();
while let Some((m, h)) = stream.try_next().await? { /* ... */ }Sourcepub fn shutdown_token(&self) -> ShutdownToken
pub fn shutdown_token(&self) -> ShutdownToken
Returns a shutdown token for the stream.
§Example
// Get a shutdown token for the stream.
let shutdown_token = stream.shutdown_token();
// Signal and await a shutdown of the stream.
shutdown_token.shutdown().await;
// The stream stops yielding messages after a cancel.
assert!(stream.next().await.is_none());Use this token to signal and/or await shutdown of the stream.
Awaiting a stream shutdown gives the subscriber time to flush its pending acknowledgements, and schedule other messages for redelivery to another client as soon as possible.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for MessageStream
impl !RefUnwindSafe for MessageStream
impl Send for MessageStream
impl Sync for MessageStream
impl Unpin for MessageStream
impl UnsafeUnpin for MessageStream
impl !UnwindSafe for MessageStream
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