| 1 |
// Copyright 2018 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package gcppubsub_test |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"log" |
| 20 |
|
| 21 |
"gocloud.dev/gcp" |
| 22 |
"gocloud.dev/pubsub" |
| 23 |
"gocloud.dev/pubsub/gcppubsub" |
| 24 |
|
| 25 |
raw "cloud.google.com/go/pubsub/apiv1" |
| 26 |
pb "cloud.google.com/go/pubsub/apiv1/pubsubpb" |
| 27 |
) |
| 28 |
|
| 29 |
func ExampleOpenTopic() { |
| 30 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 31 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 32 |
ctx := context.Background() |
| 33 |
|
| 34 |
// Your GCP credentials. |
| 35 |
// See https://cloud.google.com/docs/authentication/production |
| 36 |
// for more info on alternatives. |
| 37 |
creds, err := gcp.DefaultCredentials(ctx) |
| 38 |
if err != nil { |
| 39 |
log.Fatal(err) |
| 40 |
} |
| 41 |
// Open a gRPC connection to the GCP Pub/Sub API. |
| 42 |
conn, cleanup, err := gcppubsub.Dial(ctx, creds.TokenSource) |
| 43 |
if err != nil { |
| 44 |
log.Fatal(err) |
| 45 |
} |
| 46 |
defer cleanup() |
| 47 |
|
| 48 |
// Construct a PublisherClient using the connection. |
| 49 |
pubClient, err := gcppubsub.PublisherClient(ctx, conn) |
| 50 |
if err != nil { |
| 51 |
log.Fatal(err) |
| 52 |
} |
| 53 |
defer pubClient.Close() |
| 54 |
|
| 55 |
// Construct a *pubsub.Topic. |
| 56 |
topic, err := gcppubsub.OpenTopicByPath(pubClient, "projects/myprojectID/topics/example-topic", nil) |
| 57 |
if err != nil { |
| 58 |
log.Fatal(err) |
| 59 |
} |
| 60 |
defer topic.Shutdown(ctx) |
| 61 |
} |
| 62 |
|
| 63 |
func Example_openTopicFromURL() { |
| 64 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 65 |
// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/gcppubsub" |
| 66 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 67 |
ctx := context.Background() |
| 68 |
|
| 69 |
topic, err := pubsub.OpenTopic(ctx, "gcppubsub://projects/myproject/topics/mytopic") |
| 70 |
if err != nil { |
| 71 |
log.Fatal(err) |
| 72 |
} |
| 73 |
defer topic.Shutdown(ctx) |
| 74 |
} |
| 75 |
|
| 76 |
func ExampleOpenSubscription() { |
| 77 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 78 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 79 |
ctx := context.Background() |
| 80 |
|
| 81 |
// Your GCP credentials. |
| 82 |
// See https://cloud.google.com/docs/authentication/production |
| 83 |
// for more info on alternatives. |
| 84 |
creds, err := gcp.DefaultCredentials(ctx) |
| 85 |
if err != nil { |
| 86 |
log.Fatal(err) |
| 87 |
} |
| 88 |
|
| 89 |
// Open a gRPC connection to the GCP Pub/Sub API. |
| 90 |
conn, cleanup, err := gcppubsub.Dial(ctx, creds.TokenSource) |
| 91 |
if err != nil { |
| 92 |
log.Fatal(err) |
| 93 |
} |
| 94 |
defer cleanup() |
| 95 |
|
| 96 |
// Construct a SubscriberClient using the connection. |
| 97 |
subClient, err := gcppubsub.SubscriberClient(ctx, conn) |
| 98 |
if err != nil { |
| 99 |
log.Fatal(err) |
| 100 |
} |
| 101 |
defer subClient.Close() |
| 102 |
|
| 103 |
// Construct a *pubsub.Subscription. |
| 104 |
subscription, err := gcppubsub.OpenSubscriptionByPath( |
| 105 |
subClient, "projects/myprojectID/subscriptions/example-subscription", nil) |
| 106 |
if err != nil { |
| 107 |
log.Fatal(err) |
| 108 |
} |
| 109 |
defer subscription.Shutdown(ctx) |
| 110 |
} |
| 111 |
|
| 112 |
func Example_extendingAckDeadline() { |
| 113 |
ctx := context.Background() |
| 114 |
|
| 115 |
// Construct a *pubsub.Subscription, in this example using a URL. |
| 116 |
const subName = "projects/myprojectID/subscriptions/example-subscription" |
| 117 |
subscription, err := pubsub.OpenSubscription(ctx, "gcppubsub://"+subName) |
| 118 |
if err != nil { |
| 119 |
log.Fatal(err) |
| 120 |
} |
| 121 |
defer subscription.Shutdown(ctx) |
| 122 |
|
| 123 |
// Get the underlying SubscriberClient. If you used the constructor to create |
| 124 |
// the subscription (e.g., gcppubsub.OpenSubscriptionByPath), you may already |
| 125 |
// have the client. |
| 126 |
var client *raw.SubscriberClient |
| 127 |
if !subscription.As(&client) { |
| 128 |
log.Fatal("Couldn't get SubscriberClient using As") |
| 129 |
} |
| 130 |
|
| 131 |
// Now assume you've got a message, and processing is going to take a long time; |
| 132 |
// you want to extend the default Ack deadline. |
| 133 |
msg, err := subscription.Receive(ctx) |
| 134 |
if err != nil { |
| 135 |
log.Fatalf("Failed to receive message: %v", err) |
| 136 |
} |
| 137 |
|
| 138 |
// Get the underlying ReceivedMessage. |
| 139 |
var rm *pb.ReceivedMessage |
| 140 |
if !msg.As(&rm) { |
| 141 |
log.Fatal("Couldn't get ReceivedMessage using As") |
| 142 |
} |
| 143 |
|
| 144 |
// Call ModifyAckDeadline. |
| 145 |
if err := client.ModifyAckDeadline(ctx, &pb.ModifyAckDeadlineRequest{ |
| 146 |
Subscription: subName, |
| 147 |
AckIds: []string{rm.AckId}, |
| 148 |
AckDeadlineSeconds: 30 * 60, // 30m, or whatever you need |
| 149 |
}); err != nil { |
| 150 |
log.Fatalf("Failed to ModifyAckDeadline: %v", err) |
| 151 |
} |
| 152 |
// ... eventually Ack the message. |
| 153 |
msg.Ack() |
| 154 |
} |
| 155 |
|
| 156 |
func Example_openSubscriptionFromURL() { |
| 157 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 158 |
// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/gcppubsub" |
| 159 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 160 |
ctx := context.Background() |
| 161 |
|
| 162 |
subscription, err := pubsub.OpenSubscription(ctx, |
| 163 |
"gcppubsub://projects/my-project/subscriptions/my-subscription") |
| 164 |
if err != nil { |
| 165 |
log.Fatal(err) |
| 166 |
} |
| 167 |
defer subscription.Shutdown(ctx) |
| 168 |
} |