1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package pubsub_test
16
17 import (
18 "context"
19 "fmt"
20 "time"
21
22 "cloud.google.com/go/pubsub"
23 "google.golang.org/api/iterator"
24 )
25
26 func ExampleNewClient() {
27 ctx := context.Background()
28 _, err := pubsub.NewClient(ctx, "project-id")
29 if err != nil {
30
31 }
32
33
34 }
35
36 func ExampleClient_CreateTopic() {
37 ctx := context.Background()
38 client, err := pubsub.NewClient(ctx, "project-id")
39 if err != nil {
40
41 }
42
43
44 topic, err := client.CreateTopic(ctx, "topicName")
45 if err != nil {
46
47 }
48
49 _ = topic
50 }
51
52 func ExampleClient_CreateTopicWithConfig() {
53 ctx := context.Background()
54 client, err := pubsub.NewClient(ctx, "project-id")
55 if err != nil {
56
57 }
58
59
60 topicConfig := &pubsub.TopicConfig{
61 KMSKeyName: "projects/project-id/locations/global/keyRings/my-key-ring/cryptoKeys/my-key",
62 MessageStoragePolicy: pubsub.MessageStoragePolicy{
63 AllowedPersistenceRegions: []string{"us-east1"},
64 },
65 }
66 topic, err := client.CreateTopicWithConfig(ctx, "topicName", topicConfig)
67 if err != nil {
68
69 }
70 _ = topic
71 }
72
73
74
75 func ExampleClient_TopicInProject() {
76 ctx := context.Background()
77 client, err := pubsub.NewClient(ctx, "project-id")
78 if err != nil {
79
80 }
81 topic := client.TopicInProject("topicName", "another-project-id")
82 _ = topic
83 }
84
85 func ExampleClient_CreateSubscription() {
86 ctx := context.Background()
87 client, err := pubsub.NewClient(ctx, "project-id")
88 if err != nil {
89
90 }
91
92
93 topic, err := client.CreateTopic(ctx, "topicName")
94 if err != nil {
95
96 }
97
98
99
100 sub, err := client.CreateSubscription(ctx, "subName", pubsub.SubscriptionConfig{
101 Topic: topic,
102 AckDeadline: 10 * time.Second,
103 ExpirationPolicy: 25 * time.Hour,
104 })
105 if err != nil {
106
107 }
108
109 _ = sub
110 }
111
112 func ExampleClient_CreateSubscription_neverExpire() {
113 ctx := context.Background()
114 client, err := pubsub.NewClient(ctx, "project-id")
115 if err != nil {
116
117 }
118
119
120 topic, err := client.CreateTopic(ctx, "topicName")
121 if err != nil {
122
123 }
124
125
126
127 sub, err := client.CreateSubscription(ctx, "subName", pubsub.SubscriptionConfig{
128 Topic: topic,
129 AckDeadline: 10 * time.Second,
130 ExpirationPolicy: time.Duration(0),
131 })
132 if err != nil {
133
134 }
135 _ = sub
136 }
137
138 func ExampleTopic_Delete() {
139 ctx := context.Background()
140 client, err := pubsub.NewClient(ctx, "project-id")
141 if err != nil {
142
143 }
144
145 topic := client.Topic("topicName")
146 if err := topic.Delete(ctx); err != nil {
147
148 }
149 }
150
151 func ExampleTopic_Exists() {
152 ctx := context.Background()
153 client, err := pubsub.NewClient(ctx, "project-id")
154 if err != nil {
155
156 }
157
158 topic := client.Topic("topicName")
159 ok, err := topic.Exists(ctx)
160 if err != nil {
161
162 }
163 if !ok {
164
165 }
166 }
167
168 func ExampleTopic_Publish() {
169 ctx := context.Background()
170 client, err := pubsub.NewClient(ctx, "project-id")
171 if err != nil {
172
173 }
174
175 topic := client.Topic("topicName")
176 defer topic.Stop()
177 var results []*pubsub.PublishResult
178 r := topic.Publish(ctx, &pubsub.Message{
179 Data: []byte("hello world"),
180 })
181 results = append(results, r)
182
183 for _, r := range results {
184 id, err := r.Get(ctx)
185 if err != nil {
186
187 }
188 fmt.Printf("Published a message with a message ID: %s\n", id)
189 }
190 }
191
192 func ExampleTopic_Subscriptions() {
193 ctx := context.Background()
194 client, err := pubsub.NewClient(ctx, "project-id")
195 if err != nil {
196
197 }
198 topic := client.Topic("topic-name")
199
200 for subs := topic.Subscriptions(ctx); ; {
201 sub, err := subs.Next()
202 if err == iterator.Done {
203 break
204 }
205 if err != nil {
206
207 }
208 _ = sub
209 }
210 }
211
212 func ExampleTopic_Update() {
213 ctx := context.Background()
214 client, err := pubsub.NewClient(ctx, "project-id")
215 if err != nil {
216
217 }
218 topic := client.Topic("topic-name")
219 topicConfig, err := topic.Update(ctx, pubsub.TopicConfigToUpdate{
220 MessageStoragePolicy: &pubsub.MessageStoragePolicy{
221 AllowedPersistenceRegions: []string{
222 "asia-east1", "asia-northeast1", "asia-southeast1", "australia-southeast1",
223 "europe-north1", "europe-west1", "europe-west2", "europe-west3", "europe-west4",
224 "us-central1", "us-central2", "us-east1", "us-east4", "us-west1", "us-west2"},
225 },
226 })
227 if err != nil {
228
229 }
230 _ = topicConfig
231 }
232
233 func ExampleTopic_Update_resetMessageStoragePolicy() {
234 ctx := context.Background()
235 client, err := pubsub.NewClient(ctx, "project-id")
236 if err != nil {
237
238 }
239 topic := client.Topic("topic-name")
240 topicConfig, err := topic.Update(ctx, pubsub.TopicConfigToUpdate{
241
242 MessageStoragePolicy: &pubsub.MessageStoragePolicy{},
243 })
244 if err != nil {
245
246 }
247 _ = topicConfig
248 }
249
250 func ExampleSubscription_Delete() {
251 ctx := context.Background()
252 client, err := pubsub.NewClient(ctx, "project-id")
253 if err != nil {
254
255 }
256
257 sub := client.Subscription("subName")
258 if err := sub.Delete(ctx); err != nil {
259
260 }
261 }
262
263 func ExampleSubscription_Exists() {
264 ctx := context.Background()
265 client, err := pubsub.NewClient(ctx, "project-id")
266 if err != nil {
267
268 }
269
270 sub := client.Subscription("subName")
271 ok, err := sub.Exists(ctx)
272 if err != nil {
273
274 }
275 if !ok {
276
277 }
278 }
279
280 func ExampleSubscription_Config() {
281 ctx := context.Background()
282 client, err := pubsub.NewClient(ctx, "project-id")
283 if err != nil {
284
285 }
286 sub := client.Subscription("subName")
287 config, err := sub.Config(ctx)
288 if err != nil {
289
290 }
291 fmt.Println(config)
292 }
293
294 func ExampleSubscription_Receive() {
295 ctx := context.Background()
296 client, err := pubsub.NewClient(ctx, "project-id")
297 if err != nil {
298
299 }
300 sub := client.Subscription("subName")
301 err = sub.Receive(ctx, func(ctx context.Context, m *pubsub.Message) {
302
303
304 m.Ack()
305 })
306 if err != nil && err != context.Canceled {
307
308 }
309 }
310
311
312
313 func ExampleSubscription_Receive_maxExtension() {
314 ctx := context.Background()
315 client, err := pubsub.NewClient(ctx, "project-id")
316 if err != nil {
317
318 }
319 sub := client.Subscription("subName")
320
321
322 sub.ReceiveSettings.MaxExtension = 30 * time.Second
323 err = sub.Receive(ctx, func(ctx context.Context, m *pubsub.Message) {
324
325 m.Ack()
326 })
327 if err != nil && err != context.Canceled {
328
329 }
330 }
331
332
333
334
335 func ExampleSubscription_Receive_maxOutstanding() {
336 ctx := context.Background()
337 client, err := pubsub.NewClient(ctx, "project-id")
338 if err != nil {
339
340 }
341 sub := client.Subscription("subName")
342 sub.ReceiveSettings.MaxOutstandingMessages = 5
343 sub.ReceiveSettings.MaxOutstandingBytes = 10e6
344 err = sub.Receive(ctx, func(ctx context.Context, m *pubsub.Message) {
345
346 m.Ack()
347 })
348 if err != nil && err != context.Canceled {
349
350 }
351 }
352
353 func ExampleSubscription_Update() {
354 ctx := context.Background()
355 client, err := pubsub.NewClient(ctx, "project-id")
356 if err != nil {
357
358 }
359 sub := client.Subscription("subName")
360 subConfig, err := sub.Update(ctx, pubsub.SubscriptionConfigToUpdate{
361 PushConfig: &pubsub.PushConfig{Endpoint: "https://example.com/push"},
362
363 ExpirationPolicy: time.Duration(0),
364 })
365 if err != nil {
366
367 }
368 _ = subConfig
369 }
370
371 func ExampleSubscription_Update_pushConfigAuthenticationMethod() {
372 ctx := context.Background()
373 client, err := pubsub.NewClient(ctx, "project-id")
374 if err != nil {
375
376 }
377 sub := client.Subscription("subName")
378 subConfig, err := sub.Update(ctx, pubsub.SubscriptionConfigToUpdate{
379 PushConfig: &pubsub.PushConfig{
380 Endpoint: "https://example.com/push",
381 AuthenticationMethod: &pubsub.OIDCToken{
382 ServiceAccountEmail: "service-account-email",
383 Audience: "client-12345",
384 },
385 },
386 })
387 if err != nil {
388
389 }
390 _ = subConfig
391 }
392
393 func ExampleSubscription_CreateSnapshot() {
394 ctx := context.Background()
395 client, err := pubsub.NewClient(ctx, "project-id")
396 if err != nil {
397
398 }
399 sub := client.Subscription("subName")
400 snapConfig, err := sub.CreateSnapshot(ctx, "snapshotName")
401 if err != nil {
402
403 }
404 _ = snapConfig
405 }
406
407 func ExampleSubscription_SeekToSnapshot() {
408 ctx := context.Background()
409 client, err := pubsub.NewClient(ctx, "project-id")
410 if err != nil {
411
412 }
413 sub := client.Subscription("subName")
414 snap := client.Snapshot("snapshotName")
415 if err := sub.SeekToSnapshot(ctx, snap); err != nil {
416
417 }
418 }
419
420 func ExampleSubscription_SeekToTime() {
421 ctx := context.Background()
422 client, err := pubsub.NewClient(ctx, "project-id")
423 if err != nil {
424
425 }
426 sub := client.Subscription("subName")
427 if err := sub.SeekToTime(ctx, time.Now().Add(-time.Hour)); err != nil {
428
429 }
430 }
431
432 func ExampleSnapshot_Delete() {
433 ctx := context.Background()
434 client, err := pubsub.NewClient(ctx, "project-id")
435 if err != nil {
436
437 }
438
439 snap := client.Snapshot("snapshotName")
440 if err := snap.Delete(ctx); err != nil {
441
442 }
443 }
444
445 func ExampleClient_Snapshots() {
446 ctx := context.Background()
447 client, err := pubsub.NewClient(ctx, "project-id")
448 if err != nil {
449
450 }
451
452 iter := client.Snapshots(ctx)
453 _ = iter
454 }
455
456 func ExampleSnapshotConfigIterator_Next() {
457 ctx := context.Background()
458 client, err := pubsub.NewClient(ctx, "project-id")
459 if err != nil {
460
461 }
462
463 iter := client.Snapshots(ctx)
464 for {
465 snapConfig, err := iter.Next()
466 if err == iterator.Done {
467 break
468 }
469 if err != nil {
470
471 }
472 _ = snapConfig
473 }
474 }
475
476
477
478
479
480
View as plain text