Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pubsub] Cancellable MessageStream #271

Merged
merged 7 commits into from
Jun 2, 2024
Merged

Conversation

yoshidan
Copy link
Owner

@yoshidan yoshidan commented May 25, 2024

Added MessageStream#cancellable() to finish subscribe messages gracefully.

#266

use std::time::Duration;
use futures_util::StreamExt;
use google_cloud_pubsub::client::{Client, ClientConfig};
use google_cloud_googleapis::pubsub::v1::PubsubMessage;
use google_cloud_pubsub::subscription::{SubscribeConfig, SubscriptionConfig};
use google_cloud_gax::grpc::Status;

async fn run(config: ClientConfig) -> Result<(), Status> {
     // Creating Client, Topic and Subscription...
     let client = Client::new(config).await.unwrap();
     let subscription = client.subscription("test-subscription");

     // Read the messages as a stream
     let mut stream = subscription.subscribe(None).await.unwrap();
     let cancellable = stream.cancellable();
     let task = tokio::spawn(async move {
         // None if the stream is cancelled
         while let Some(message) = stream.next().await {
             message.ack().await.unwrap();
         }
     });
     tokio::time::sleep(Duration::from_secs(60)).await;
     cancellable.cancel();
     let _ = task.await;
     Ok(())
}

@yoshidan yoshidan added the safe to test safe to test label May 25, 2024
@yoshidan yoshidan marked this pull request as draft May 25, 2024 10:33
@yoshidan yoshidan changed the title [pubsub] Add read() method to nack automatically [pubsub] Cancellable MessageStream May 30, 2024
@yoshidan yoshidan added safe to test safe to test and removed safe to test safe to test labels May 30, 2024
@yoshidan yoshidan marked this pull request as ready for review May 30, 2024 04:47
@yoshidan yoshidan merged commit e4c7391 into main Jun 2, 2024
7 checks passed
@yoshidan yoshidan deleted the feature/change_message_stream_api branch June 2, 2024 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
safe to test safe to test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant