Skip to content

Commit

Permalink
pubsub/gcppubsub: Add an example for ModifyAckDeadline
Browse files Browse the repository at this point in the history
  • Loading branch information
vangent committed Nov 22, 2024
1 parent a93c109 commit ce9c96a
Showing 1 changed file with 47 additions and 0 deletions.
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

0 comments on commit ce9c96a

Please sign in to comment.