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/gcppubsub: Add an example showing how to call ModifyAckDeadline #3510

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions pubsub/gcppubsub/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
"gocloud.dev/gcp"
"gocloud.dev/pubsub"
"gocloud.dev/pubsub/gcppubsub"

raw "cloud.google.com/go/pubsub/apiv1"
pb "cloud.google.com/go/pubsub/apiv1/pubsubpb"
)

func ExampleOpenTopic() {
Expand Down Expand Up @@ -106,6 +109,50 @@ func ExampleOpenSubscription() {
defer subscription.Shutdown(ctx)
}

func ExampleExtendingAckDeadline() {
ctx := context.Background()

// Construct a *pubsub.Subscription, in this example using a URL.
const subName = "projects/myprojectID/subscriptions/example-subscription"
subscription, err := pubsub.OpenSubscription(ctx, "gcppubsub://"+subName)
if err != nil {
log.Fatal(err)
}
defer subscription.Shutdown(ctx)

// Get the underlying SubscriberClient. If you used the constructor to create
// the subscription (e.g., gcppubsub.OpenSubscriptionByPath), you may already
// have the client.
var client *raw.SubscriberClient
if !subscription.As(&client) {
log.Fatal("Couldn't get SubscriberClient using As")
}

// Now assume you've got a message, and processing is going to take a long time;
// you want to extend the default Ack deadline.
msg, err := subscription.Receive(ctx)
if err != nil {
log.Fatalf("Failed to receive message: %v", err)
}

// Get the underlying ReceivedMessage.
var rm *pb.ReceivedMessage
if !msg.As(&rm) {
log.Fatal("Couldn't get ReceivedMessage using As")
}

// Call ModifyAckDeadline.
if err := client.ModifyAckDeadline(ctx, &pb.ModifyAckDeadlineRequest{
Subscription: subName,
AckIds: []string{rm.AckId},
AckDeadlineSeconds: 30 * 60, // 30m, or whatever you need
}); err != nil {
log.Fatalf("Failed to ModifyAckDeadline: %v", err)
}
// ... eventually Ack the message.
msg.Ack()
}

func Example_openSubscriptionFromURL() {
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/gcppubsub"
Expand Down
Loading