1// Copyright 2021 Google LLC 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// Code generated by protoc-gen-go_gapic. DO NOT EDIT. 16 17package pubsub 18 19import ( 20 "context" 21 "fmt" 22 "math" 23 "net/url" 24 "time" 25 26 "github.com/golang/protobuf/proto" 27 gax "github.com/googleapis/gax-go/v2" 28 "google.golang.org/api/iterator" 29 "google.golang.org/api/option" 30 "google.golang.org/api/option/internaloption" 31 gtransport "google.golang.org/api/transport/grpc" 32 pubsubpb "google.golang.org/genproto/googleapis/pubsub/v1" 33 "google.golang.org/grpc" 34 "google.golang.org/grpc/codes" 35 "google.golang.org/grpc/metadata" 36) 37 38var newSubscriberClientHook clientHook 39 40// SubscriberCallOptions contains the retry settings for each method of SubscriberClient. 41type SubscriberCallOptions struct { 42 CreateSubscription []gax.CallOption 43 GetSubscription []gax.CallOption 44 UpdateSubscription []gax.CallOption 45 ListSubscriptions []gax.CallOption 46 DeleteSubscription []gax.CallOption 47 ModifyAckDeadline []gax.CallOption 48 Acknowledge []gax.CallOption 49 Pull []gax.CallOption 50 StreamingPull []gax.CallOption 51 ModifyPushConfig []gax.CallOption 52 GetSnapshot []gax.CallOption 53 ListSnapshots []gax.CallOption 54 CreateSnapshot []gax.CallOption 55 UpdateSnapshot []gax.CallOption 56 DeleteSnapshot []gax.CallOption 57 Seek []gax.CallOption 58} 59 60func defaultSubscriberClientOptions() []option.ClientOption { 61 return []option.ClientOption{ 62 internaloption.WithDefaultEndpoint("pubsub.googleapis.com:443"), 63 internaloption.WithDefaultMTLSEndpoint("pubsub.mtls.googleapis.com:443"), 64 internaloption.WithDefaultAudience("https://pubsub.googleapis.com/"), 65 internaloption.WithDefaultScopes(DefaultAuthScopes()...), 66 option.WithGRPCDialOption(grpc.WithDisableServiceConfig()), 67 option.WithGRPCDialOption(grpc.WithDefaultCallOptions( 68 grpc.MaxCallRecvMsgSize(math.MaxInt32))), 69 } 70} 71 72func defaultSubscriberCallOptions() *SubscriberCallOptions { 73 return &SubscriberCallOptions{ 74 CreateSubscription: []gax.CallOption{ 75 gax.WithRetry(func() gax.Retryer { 76 return gax.OnCodes([]codes.Code{ 77 codes.Unknown, 78 codes.Aborted, 79 codes.Unavailable, 80 }, gax.Backoff{ 81 Initial: 100 * time.Millisecond, 82 Max: 60000 * time.Millisecond, 83 Multiplier: 1.30, 84 }) 85 }), 86 }, 87 GetSubscription: []gax.CallOption{ 88 gax.WithRetry(func() gax.Retryer { 89 return gax.OnCodes([]codes.Code{ 90 codes.Unknown, 91 codes.Aborted, 92 codes.Unavailable, 93 }, gax.Backoff{ 94 Initial: 100 * time.Millisecond, 95 Max: 60000 * time.Millisecond, 96 Multiplier: 1.30, 97 }) 98 }), 99 }, 100 UpdateSubscription: []gax.CallOption{ 101 gax.WithRetry(func() gax.Retryer { 102 return gax.OnCodes([]codes.Code{ 103 codes.Unavailable, 104 }, gax.Backoff{ 105 Initial: 100 * time.Millisecond, 106 Max: 60000 * time.Millisecond, 107 Multiplier: 1.30, 108 }) 109 }), 110 }, 111 ListSubscriptions: []gax.CallOption{ 112 gax.WithRetry(func() gax.Retryer { 113 return gax.OnCodes([]codes.Code{ 114 codes.Unknown, 115 codes.Aborted, 116 codes.Unavailable, 117 }, gax.Backoff{ 118 Initial: 100 * time.Millisecond, 119 Max: 60000 * time.Millisecond, 120 Multiplier: 1.30, 121 }) 122 }), 123 }, 124 DeleteSubscription: []gax.CallOption{ 125 gax.WithRetry(func() gax.Retryer { 126 return gax.OnCodes([]codes.Code{ 127 codes.Unavailable, 128 }, gax.Backoff{ 129 Initial: 100 * time.Millisecond, 130 Max: 60000 * time.Millisecond, 131 Multiplier: 1.30, 132 }) 133 }), 134 }, 135 ModifyAckDeadline: []gax.CallOption{ 136 gax.WithRetry(func() gax.Retryer { 137 return gax.OnCodes([]codes.Code{ 138 codes.Unavailable, 139 }, gax.Backoff{ 140 Initial: 100 * time.Millisecond, 141 Max: 60000 * time.Millisecond, 142 Multiplier: 1.30, 143 }) 144 }), 145 }, 146 Acknowledge: []gax.CallOption{ 147 gax.WithRetry(func() gax.Retryer { 148 return gax.OnCodes([]codes.Code{ 149 codes.Unavailable, 150 }, gax.Backoff{ 151 Initial: 100 * time.Millisecond, 152 Max: 60000 * time.Millisecond, 153 Multiplier: 1.30, 154 }) 155 }), 156 }, 157 Pull: []gax.CallOption{ 158 gax.WithRetry(func() gax.Retryer { 159 return gax.OnCodes([]codes.Code{ 160 codes.Unknown, 161 codes.Aborted, 162 codes.Unavailable, 163 }, gax.Backoff{ 164 Initial: 100 * time.Millisecond, 165 Max: 60000 * time.Millisecond, 166 Multiplier: 1.30, 167 }) 168 }), 169 }, 170 StreamingPull: []gax.CallOption{ 171 gax.WithRetry(func() gax.Retryer { 172 return gax.OnCodes([]codes.Code{ 173 codes.DeadlineExceeded, 174 codes.ResourceExhausted, 175 codes.Aborted, 176 codes.Internal, 177 codes.Unavailable, 178 }, gax.Backoff{ 179 Initial: 100 * time.Millisecond, 180 Max: 60000 * time.Millisecond, 181 Multiplier: 1.30, 182 }) 183 }), 184 }, 185 ModifyPushConfig: []gax.CallOption{ 186 gax.WithRetry(func() gax.Retryer { 187 return gax.OnCodes([]codes.Code{ 188 codes.Unavailable, 189 }, gax.Backoff{ 190 Initial: 100 * time.Millisecond, 191 Max: 60000 * time.Millisecond, 192 Multiplier: 1.30, 193 }) 194 }), 195 }, 196 GetSnapshot: []gax.CallOption{ 197 gax.WithRetry(func() gax.Retryer { 198 return gax.OnCodes([]codes.Code{ 199 codes.Unknown, 200 codes.Aborted, 201 codes.Unavailable, 202 }, gax.Backoff{ 203 Initial: 100 * time.Millisecond, 204 Max: 60000 * time.Millisecond, 205 Multiplier: 1.30, 206 }) 207 }), 208 }, 209 ListSnapshots: []gax.CallOption{ 210 gax.WithRetry(func() gax.Retryer { 211 return gax.OnCodes([]codes.Code{ 212 codes.Unknown, 213 codes.Aborted, 214 codes.Unavailable, 215 }, gax.Backoff{ 216 Initial: 100 * time.Millisecond, 217 Max: 60000 * time.Millisecond, 218 Multiplier: 1.30, 219 }) 220 }), 221 }, 222 CreateSnapshot: []gax.CallOption{ 223 gax.WithRetry(func() gax.Retryer { 224 return gax.OnCodes([]codes.Code{ 225 codes.Unavailable, 226 }, gax.Backoff{ 227 Initial: 100 * time.Millisecond, 228 Max: 60000 * time.Millisecond, 229 Multiplier: 1.30, 230 }) 231 }), 232 }, 233 UpdateSnapshot: []gax.CallOption{ 234 gax.WithRetry(func() gax.Retryer { 235 return gax.OnCodes([]codes.Code{ 236 codes.Unavailable, 237 }, gax.Backoff{ 238 Initial: 100 * time.Millisecond, 239 Max: 60000 * time.Millisecond, 240 Multiplier: 1.30, 241 }) 242 }), 243 }, 244 DeleteSnapshot: []gax.CallOption{ 245 gax.WithRetry(func() gax.Retryer { 246 return gax.OnCodes([]codes.Code{ 247 codes.Unavailable, 248 }, gax.Backoff{ 249 Initial: 100 * time.Millisecond, 250 Max: 60000 * time.Millisecond, 251 Multiplier: 1.30, 252 }) 253 }), 254 }, 255 Seek: []gax.CallOption{ 256 gax.WithRetry(func() gax.Retryer { 257 return gax.OnCodes([]codes.Code{ 258 codes.Unknown, 259 codes.Aborted, 260 codes.Unavailable, 261 }, gax.Backoff{ 262 Initial: 100 * time.Millisecond, 263 Max: 60000 * time.Millisecond, 264 Multiplier: 1.30, 265 }) 266 }), 267 }, 268 } 269} 270 271// SubscriberClient is a client for interacting with Cloud Pub/Sub API. 272// 273// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 274type SubscriberClient struct { 275 // Connection pool of gRPC connections to the service. 276 connPool gtransport.ConnPool 277 278 // flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE 279 disableDeadlines bool 280 281 // The gRPC API client. 282 subscriberClient pubsubpb.SubscriberClient 283 284 // The call options for this service. 285 CallOptions *SubscriberCallOptions 286 287 // The x-goog-* metadata to be sent with each request. 288 xGoogMetadata metadata.MD 289} 290 291// NewSubscriberClient creates a new subscriber client. 292// 293// The service that an application uses to manipulate subscriptions and to 294// consume messages from a subscription via the Pull method or by 295// establishing a bi-directional stream using the StreamingPull method. 296func NewSubscriberClient(ctx context.Context, opts ...option.ClientOption) (*SubscriberClient, error) { 297 clientOpts := defaultSubscriberClientOptions() 298 299 if newSubscriberClientHook != nil { 300 hookOpts, err := newSubscriberClientHook(ctx, clientHookParams{}) 301 if err != nil { 302 return nil, err 303 } 304 clientOpts = append(clientOpts, hookOpts...) 305 } 306 307 disableDeadlines, err := checkDisableDeadlines() 308 if err != nil { 309 return nil, err 310 } 311 312 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) 313 if err != nil { 314 return nil, err 315 } 316 c := &SubscriberClient{ 317 connPool: connPool, 318 disableDeadlines: disableDeadlines, 319 CallOptions: defaultSubscriberCallOptions(), 320 321 subscriberClient: pubsubpb.NewSubscriberClient(connPool), 322 } 323 c.setGoogleClientInfo() 324 325 return c, nil 326} 327 328// Connection returns a connection to the API service. 329// 330// Deprecated. 331func (c *SubscriberClient) Connection() *grpc.ClientConn { 332 return c.connPool.Conn() 333} 334 335// Close closes the connection to the API service. The user should invoke this when 336// the client is no longer required. 337func (c *SubscriberClient) Close() error { 338 return c.connPool.Close() 339} 340 341// setGoogleClientInfo sets the name and version of the application in 342// the `x-goog-api-client` header passed on each request. Intended for 343// use by Google-written clients. 344func (c *SubscriberClient) setGoogleClientInfo(keyval ...string) { 345 kv := append([]string{"gl-go", versionGo()}, keyval...) 346 kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) 347 c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) 348} 349 350// CreateSubscription creates a subscription to a given topic. See the [resource name rules] 351// (https://cloud.google.com/pubsub/docs/admin#resource_names (at https://cloud.google.com/pubsub/docs/admin#resource_names)). 352// If the subscription already exists, returns ALREADY_EXISTS. 353// If the corresponding topic doesn’t exist, returns NOT_FOUND. 354// 355// If the name is not provided in the request, the server will assign a random 356// name for this subscription on the same project as the topic, conforming 357// to the [resource name format] 358// (https://cloud.google.com/pubsub/docs/admin#resource_names (at https://cloud.google.com/pubsub/docs/admin#resource_names)). The generated 359// name is populated in the returned Subscription object. Note that for REST 360// API requests, you must specify a name in the request. 361func (c *SubscriberClient) CreateSubscription(ctx context.Context, req *pubsubpb.Subscription, opts ...gax.CallOption) (*pubsubpb.Subscription, error) { 362 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 363 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 364 defer cancel() 365 ctx = cctx 366 } 367 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 368 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 369 opts = append(c.CallOptions.CreateSubscription[0:len(c.CallOptions.CreateSubscription):len(c.CallOptions.CreateSubscription)], opts...) 370 var resp *pubsubpb.Subscription 371 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 372 var err error 373 resp, err = c.subscriberClient.CreateSubscription(ctx, req, settings.GRPC...) 374 return err 375 }, opts...) 376 if err != nil { 377 return nil, err 378 } 379 return resp, nil 380} 381 382// GetSubscription gets the configuration details of a subscription. 383func (c *SubscriberClient) GetSubscription(ctx context.Context, req *pubsubpb.GetSubscriptionRequest, opts ...gax.CallOption) (*pubsubpb.Subscription, error) { 384 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 385 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 386 defer cancel() 387 ctx = cctx 388 } 389 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "subscription", url.QueryEscape(req.GetSubscription()))) 390 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 391 opts = append(c.CallOptions.GetSubscription[0:len(c.CallOptions.GetSubscription):len(c.CallOptions.GetSubscription)], opts...) 392 var resp *pubsubpb.Subscription 393 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 394 var err error 395 resp, err = c.subscriberClient.GetSubscription(ctx, req, settings.GRPC...) 396 return err 397 }, opts...) 398 if err != nil { 399 return nil, err 400 } 401 return resp, nil 402} 403 404// UpdateSubscription updates an existing subscription. Note that certain properties of a 405// subscription, such as its topic, are not modifiable. 406func (c *SubscriberClient) UpdateSubscription(ctx context.Context, req *pubsubpb.UpdateSubscriptionRequest, opts ...gax.CallOption) (*pubsubpb.Subscription, error) { 407 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 408 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 409 defer cancel() 410 ctx = cctx 411 } 412 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "subscription.name", url.QueryEscape(req.GetSubscription().GetName()))) 413 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 414 opts = append(c.CallOptions.UpdateSubscription[0:len(c.CallOptions.UpdateSubscription):len(c.CallOptions.UpdateSubscription)], opts...) 415 var resp *pubsubpb.Subscription 416 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 417 var err error 418 resp, err = c.subscriberClient.UpdateSubscription(ctx, req, settings.GRPC...) 419 return err 420 }, opts...) 421 if err != nil { 422 return nil, err 423 } 424 return resp, nil 425} 426 427// ListSubscriptions lists matching subscriptions. 428func (c *SubscriberClient) ListSubscriptions(ctx context.Context, req *pubsubpb.ListSubscriptionsRequest, opts ...gax.CallOption) *SubscriptionIterator { 429 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "project", url.QueryEscape(req.GetProject()))) 430 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 431 opts = append(c.CallOptions.ListSubscriptions[0:len(c.CallOptions.ListSubscriptions):len(c.CallOptions.ListSubscriptions)], opts...) 432 it := &SubscriptionIterator{} 433 req = proto.Clone(req).(*pubsubpb.ListSubscriptionsRequest) 434 it.InternalFetch = func(pageSize int, pageToken string) ([]*pubsubpb.Subscription, string, error) { 435 var resp *pubsubpb.ListSubscriptionsResponse 436 req.PageToken = pageToken 437 if pageSize > math.MaxInt32 { 438 req.PageSize = math.MaxInt32 439 } else { 440 req.PageSize = int32(pageSize) 441 } 442 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 443 var err error 444 resp, err = c.subscriberClient.ListSubscriptions(ctx, req, settings.GRPC...) 445 return err 446 }, opts...) 447 if err != nil { 448 return nil, "", err 449 } 450 451 it.Response = resp 452 return resp.GetSubscriptions(), resp.GetNextPageToken(), nil 453 } 454 fetch := func(pageSize int, pageToken string) (string, error) { 455 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 456 if err != nil { 457 return "", err 458 } 459 it.items = append(it.items, items...) 460 return nextPageToken, nil 461 } 462 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 463 it.pageInfo.MaxSize = int(req.GetPageSize()) 464 it.pageInfo.Token = req.GetPageToken() 465 return it 466} 467 468// DeleteSubscription deletes an existing subscription. All messages retained in the subscription 469// are immediately dropped. Calls to Pull after deletion will return 470// NOT_FOUND. After a subscription is deleted, a new one may be created with 471// the same name, but the new one has no association with the old 472// subscription or its topic unless the same topic is specified. 473func (c *SubscriberClient) DeleteSubscription(ctx context.Context, req *pubsubpb.DeleteSubscriptionRequest, opts ...gax.CallOption) error { 474 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 475 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 476 defer cancel() 477 ctx = cctx 478 } 479 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "subscription", url.QueryEscape(req.GetSubscription()))) 480 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 481 opts = append(c.CallOptions.DeleteSubscription[0:len(c.CallOptions.DeleteSubscription):len(c.CallOptions.DeleteSubscription)], opts...) 482 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 483 var err error 484 _, err = c.subscriberClient.DeleteSubscription(ctx, req, settings.GRPC...) 485 return err 486 }, opts...) 487 return err 488} 489 490// ModifyAckDeadline modifies the ack deadline for a specific message. This method is useful 491// to indicate that more time is needed to process a message by the 492// subscriber, or to make the message available for redelivery if the 493// processing was interrupted. Note that this does not modify the 494// subscription-level ackDeadlineSeconds used for subsequent messages. 495func (c *SubscriberClient) ModifyAckDeadline(ctx context.Context, req *pubsubpb.ModifyAckDeadlineRequest, opts ...gax.CallOption) error { 496 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 497 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 498 defer cancel() 499 ctx = cctx 500 } 501 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "subscription", url.QueryEscape(req.GetSubscription()))) 502 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 503 opts = append(c.CallOptions.ModifyAckDeadline[0:len(c.CallOptions.ModifyAckDeadline):len(c.CallOptions.ModifyAckDeadline)], opts...) 504 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 505 var err error 506 _, err = c.subscriberClient.ModifyAckDeadline(ctx, req, settings.GRPC...) 507 return err 508 }, opts...) 509 return err 510} 511 512// Acknowledge acknowledges the messages associated with the ack_ids in the 513// AcknowledgeRequest. The Pub/Sub system can remove the relevant messages 514// from the subscription. 515// 516// Acknowledging a message whose ack deadline has expired may succeed, 517// but such a message may be redelivered later. Acknowledging a message more 518// than once will not result in an error. 519func (c *SubscriberClient) Acknowledge(ctx context.Context, req *pubsubpb.AcknowledgeRequest, opts ...gax.CallOption) error { 520 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 521 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 522 defer cancel() 523 ctx = cctx 524 } 525 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "subscription", url.QueryEscape(req.GetSubscription()))) 526 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 527 opts = append(c.CallOptions.Acknowledge[0:len(c.CallOptions.Acknowledge):len(c.CallOptions.Acknowledge)], opts...) 528 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 529 var err error 530 _, err = c.subscriberClient.Acknowledge(ctx, req, settings.GRPC...) 531 return err 532 }, opts...) 533 return err 534} 535 536// Pull pulls messages from the server. The server may return UNAVAILABLE if 537// there are too many concurrent pull requests pending for the given 538// subscription. 539func (c *SubscriberClient) Pull(ctx context.Context, req *pubsubpb.PullRequest, opts ...gax.CallOption) (*pubsubpb.PullResponse, error) { 540 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 541 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 542 defer cancel() 543 ctx = cctx 544 } 545 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "subscription", url.QueryEscape(req.GetSubscription()))) 546 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 547 opts = append(c.CallOptions.Pull[0:len(c.CallOptions.Pull):len(c.CallOptions.Pull)], opts...) 548 var resp *pubsubpb.PullResponse 549 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 550 var err error 551 resp, err = c.subscriberClient.Pull(ctx, req, settings.GRPC...) 552 return err 553 }, opts...) 554 if err != nil { 555 return nil, err 556 } 557 return resp, nil 558} 559 560// StreamingPull establishes a stream with the server, which sends messages down to the 561// client. The client streams acknowledgements and ack deadline modifications 562// back to the server. The server will close the stream and return the status 563// on any error. The server may close the stream with status UNAVAILABLE to 564// reassign server-side resources, in which case, the client should 565// re-establish the stream. Flow control can be achieved by configuring the 566// underlying RPC channel. 567func (c *SubscriberClient) StreamingPull(ctx context.Context, opts ...gax.CallOption) (pubsubpb.Subscriber_StreamingPullClient, error) { 568 ctx = insertMetadata(ctx, c.xGoogMetadata) 569 opts = append(c.CallOptions.StreamingPull[0:len(c.CallOptions.StreamingPull):len(c.CallOptions.StreamingPull)], opts...) 570 var resp pubsubpb.Subscriber_StreamingPullClient 571 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 572 var err error 573 resp, err = c.subscriberClient.StreamingPull(ctx, settings.GRPC...) 574 return err 575 }, opts...) 576 if err != nil { 577 return nil, err 578 } 579 return resp, nil 580} 581 582// ModifyPushConfig modifies the PushConfig for a specified subscription. 583// 584// This may be used to change a push subscription to a pull one (signified by 585// an empty PushConfig) or vice versa, or change the endpoint URL and other 586// attributes of a push subscription. Messages will accumulate for delivery 587// continuously through the call regardless of changes to the PushConfig. 588func (c *SubscriberClient) ModifyPushConfig(ctx context.Context, req *pubsubpb.ModifyPushConfigRequest, opts ...gax.CallOption) error { 589 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 590 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 591 defer cancel() 592 ctx = cctx 593 } 594 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "subscription", url.QueryEscape(req.GetSubscription()))) 595 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 596 opts = append(c.CallOptions.ModifyPushConfig[0:len(c.CallOptions.ModifyPushConfig):len(c.CallOptions.ModifyPushConfig)], opts...) 597 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 598 var err error 599 _, err = c.subscriberClient.ModifyPushConfig(ctx, req, settings.GRPC...) 600 return err 601 }, opts...) 602 return err 603} 604 605// GetSnapshot gets the configuration details of a snapshot. Snapshots are used in 606// Seek (at https://cloud.google.com/pubsub/docs/replay-overview) 607// operations, which allow you to manage message acknowledgments in bulk. That 608// is, you can set the acknowledgment state of messages in an existing 609// subscription to the state captured by a snapshot. 610func (c *SubscriberClient) GetSnapshot(ctx context.Context, req *pubsubpb.GetSnapshotRequest, opts ...gax.CallOption) (*pubsubpb.Snapshot, error) { 611 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 612 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 613 defer cancel() 614 ctx = cctx 615 } 616 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "snapshot", url.QueryEscape(req.GetSnapshot()))) 617 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 618 opts = append(c.CallOptions.GetSnapshot[0:len(c.CallOptions.GetSnapshot):len(c.CallOptions.GetSnapshot)], opts...) 619 var resp *pubsubpb.Snapshot 620 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 621 var err error 622 resp, err = c.subscriberClient.GetSnapshot(ctx, req, settings.GRPC...) 623 return err 624 }, opts...) 625 if err != nil { 626 return nil, err 627 } 628 return resp, nil 629} 630 631// ListSnapshots lists the existing snapshots. Snapshots are used in Seek (at https://cloud.google.com/pubsub/docs/replay-overview) operations, which 632// allow you to manage message acknowledgments in bulk. That is, you can set 633// the acknowledgment state of messages in an existing subscription to the 634// state captured by a snapshot. 635func (c *SubscriberClient) ListSnapshots(ctx context.Context, req *pubsubpb.ListSnapshotsRequest, opts ...gax.CallOption) *SnapshotIterator { 636 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "project", url.QueryEscape(req.GetProject()))) 637 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 638 opts = append(c.CallOptions.ListSnapshots[0:len(c.CallOptions.ListSnapshots):len(c.CallOptions.ListSnapshots)], opts...) 639 it := &SnapshotIterator{} 640 req = proto.Clone(req).(*pubsubpb.ListSnapshotsRequest) 641 it.InternalFetch = func(pageSize int, pageToken string) ([]*pubsubpb.Snapshot, string, error) { 642 var resp *pubsubpb.ListSnapshotsResponse 643 req.PageToken = pageToken 644 if pageSize > math.MaxInt32 { 645 req.PageSize = math.MaxInt32 646 } else { 647 req.PageSize = int32(pageSize) 648 } 649 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 650 var err error 651 resp, err = c.subscriberClient.ListSnapshots(ctx, req, settings.GRPC...) 652 return err 653 }, opts...) 654 if err != nil { 655 return nil, "", err 656 } 657 658 it.Response = resp 659 return resp.GetSnapshots(), resp.GetNextPageToken(), nil 660 } 661 fetch := func(pageSize int, pageToken string) (string, error) { 662 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 663 if err != nil { 664 return "", err 665 } 666 it.items = append(it.items, items...) 667 return nextPageToken, nil 668 } 669 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 670 it.pageInfo.MaxSize = int(req.GetPageSize()) 671 it.pageInfo.Token = req.GetPageToken() 672 return it 673} 674 675// CreateSnapshot creates a snapshot from the requested subscription. Snapshots are used in 676// Seek (at https://cloud.google.com/pubsub/docs/replay-overview) operations, 677// which allow you to manage message acknowledgments in bulk. That is, you can 678// set the acknowledgment state of messages in an existing subscription to the 679// state captured by a snapshot. 680// If the snapshot already exists, returns ALREADY_EXISTS. 681// If the requested subscription doesn’t exist, returns NOT_FOUND. 682// If the backlog in the subscription is too old – and the resulting snapshot 683// would expire in less than 1 hour – then FAILED_PRECONDITION is returned. 684// See also the Snapshot.expire_time field. If the name is not provided in 685// the request, the server will assign a random 686// name for this snapshot on the same project as the subscription, conforming 687// to the [resource name format] 688// (https://cloud.google.com/pubsub/docs/admin#resource_names (at https://cloud.google.com/pubsub/docs/admin#resource_names)). The 689// generated name is populated in the returned Snapshot object. Note that for 690// REST API requests, you must specify a name in the request. 691func (c *SubscriberClient) CreateSnapshot(ctx context.Context, req *pubsubpb.CreateSnapshotRequest, opts ...gax.CallOption) (*pubsubpb.Snapshot, error) { 692 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 693 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 694 defer cancel() 695 ctx = cctx 696 } 697 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 698 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 699 opts = append(c.CallOptions.CreateSnapshot[0:len(c.CallOptions.CreateSnapshot):len(c.CallOptions.CreateSnapshot)], opts...) 700 var resp *pubsubpb.Snapshot 701 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 702 var err error 703 resp, err = c.subscriberClient.CreateSnapshot(ctx, req, settings.GRPC...) 704 return err 705 }, opts...) 706 if err != nil { 707 return nil, err 708 } 709 return resp, nil 710} 711 712// UpdateSnapshot updates an existing snapshot. Snapshots are used in 713// Seek (at https://cloud.google.com/pubsub/docs/replay-overview) 714// operations, which allow 715// you to manage message acknowledgments in bulk. That is, you can set the 716// acknowledgment state of messages in an existing subscription to the state 717// captured by a snapshot. 718func (c *SubscriberClient) UpdateSnapshot(ctx context.Context, req *pubsubpb.UpdateSnapshotRequest, opts ...gax.CallOption) (*pubsubpb.Snapshot, error) { 719 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 720 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 721 defer cancel() 722 ctx = cctx 723 } 724 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "snapshot.name", url.QueryEscape(req.GetSnapshot().GetName()))) 725 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 726 opts = append(c.CallOptions.UpdateSnapshot[0:len(c.CallOptions.UpdateSnapshot):len(c.CallOptions.UpdateSnapshot)], opts...) 727 var resp *pubsubpb.Snapshot 728 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 729 var err error 730 resp, err = c.subscriberClient.UpdateSnapshot(ctx, req, settings.GRPC...) 731 return err 732 }, opts...) 733 if err != nil { 734 return nil, err 735 } 736 return resp, nil 737} 738 739// DeleteSnapshot removes an existing snapshot. Snapshots are used in [Seek] 740// (https://cloud.google.com/pubsub/docs/replay-overview (at https://cloud.google.com/pubsub/docs/replay-overview)) operations, which 741// allow you to manage message acknowledgments in bulk. That is, you can set 742// the acknowledgment state of messages in an existing subscription to the 743// state captured by a snapshot. 744// When the snapshot is deleted, all messages retained in the snapshot 745// are immediately dropped. After a snapshot is deleted, a new one may be 746// created with the same name, but the new one has no association with the old 747// snapshot or its subscription, unless the same subscription is specified. 748func (c *SubscriberClient) DeleteSnapshot(ctx context.Context, req *pubsubpb.DeleteSnapshotRequest, opts ...gax.CallOption) error { 749 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 750 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 751 defer cancel() 752 ctx = cctx 753 } 754 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "snapshot", url.QueryEscape(req.GetSnapshot()))) 755 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 756 opts = append(c.CallOptions.DeleteSnapshot[0:len(c.CallOptions.DeleteSnapshot):len(c.CallOptions.DeleteSnapshot)], opts...) 757 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 758 var err error 759 _, err = c.subscriberClient.DeleteSnapshot(ctx, req, settings.GRPC...) 760 return err 761 }, opts...) 762 return err 763} 764 765// Seek seeks an existing subscription to a point in time or to a given snapshot, 766// whichever is provided in the request. Snapshots are used in [Seek] 767// (https://cloud.google.com/pubsub/docs/replay-overview (at https://cloud.google.com/pubsub/docs/replay-overview)) operations, which 768// allow you to manage message acknowledgments in bulk. That is, you can set 769// the acknowledgment state of messages in an existing subscription to the 770// state captured by a snapshot. Note that both the subscription and the 771// snapshot must be on the same topic. 772func (c *SubscriberClient) Seek(ctx context.Context, req *pubsubpb.SeekRequest, opts ...gax.CallOption) (*pubsubpb.SeekResponse, error) { 773 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 774 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 775 defer cancel() 776 ctx = cctx 777 } 778 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "subscription", url.QueryEscape(req.GetSubscription()))) 779 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 780 opts = append(c.CallOptions.Seek[0:len(c.CallOptions.Seek):len(c.CallOptions.Seek)], opts...) 781 var resp *pubsubpb.SeekResponse 782 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 783 var err error 784 resp, err = c.subscriberClient.Seek(ctx, req, settings.GRPC...) 785 return err 786 }, opts...) 787 if err != nil { 788 return nil, err 789 } 790 return resp, nil 791} 792 793// SnapshotIterator manages a stream of *pubsubpb.Snapshot. 794type SnapshotIterator struct { 795 items []*pubsubpb.Snapshot 796 pageInfo *iterator.PageInfo 797 nextFunc func() error 798 799 // Response is the raw response for the current page. 800 // It must be cast to the RPC response type. 801 // Calling Next() or InternalFetch() updates this value. 802 Response interface{} 803 804 // InternalFetch is for use by the Google Cloud Libraries only. 805 // It is not part of the stable interface of this package. 806 // 807 // InternalFetch returns results from a single call to the underlying RPC. 808 // The number of results is no greater than pageSize. 809 // If there are no more results, nextPageToken is empty and err is nil. 810 InternalFetch func(pageSize int, pageToken string) (results []*pubsubpb.Snapshot, nextPageToken string, err error) 811} 812 813// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 814func (it *SnapshotIterator) PageInfo() *iterator.PageInfo { 815 return it.pageInfo 816} 817 818// Next returns the next result. Its second return value is iterator.Done if there are no more 819// results. Once Next returns Done, all subsequent calls will return Done. 820func (it *SnapshotIterator) Next() (*pubsubpb.Snapshot, error) { 821 var item *pubsubpb.Snapshot 822 if err := it.nextFunc(); err != nil { 823 return item, err 824 } 825 item = it.items[0] 826 it.items = it.items[1:] 827 return item, nil 828} 829 830func (it *SnapshotIterator) bufLen() int { 831 return len(it.items) 832} 833 834func (it *SnapshotIterator) takeBuf() interface{} { 835 b := it.items 836 it.items = nil 837 return b 838} 839 840// SubscriptionIterator manages a stream of *pubsubpb.Subscription. 841type SubscriptionIterator struct { 842 items []*pubsubpb.Subscription 843 pageInfo *iterator.PageInfo 844 nextFunc func() error 845 846 // Response is the raw response for the current page. 847 // It must be cast to the RPC response type. 848 // Calling Next() or InternalFetch() updates this value. 849 Response interface{} 850 851 // InternalFetch is for use by the Google Cloud Libraries only. 852 // It is not part of the stable interface of this package. 853 // 854 // InternalFetch returns results from a single call to the underlying RPC. 855 // The number of results is no greater than pageSize. 856 // If there are no more results, nextPageToken is empty and err is nil. 857 InternalFetch func(pageSize int, pageToken string) (results []*pubsubpb.Subscription, nextPageToken string, err error) 858} 859 860// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 861func (it *SubscriptionIterator) PageInfo() *iterator.PageInfo { 862 return it.pageInfo 863} 864 865// Next returns the next result. Its second return value is iterator.Done if there are no more 866// results. Once Next returns Done, all subsequent calls will return Done. 867func (it *SubscriptionIterator) Next() (*pubsubpb.Subscription, error) { 868 var item *pubsubpb.Subscription 869 if err := it.nextFunc(); err != nil { 870 return item, err 871 } 872 item = it.items[0] 873 it.items = it.items[1:] 874 return item, nil 875} 876 877func (it *SubscriptionIterator) bufLen() int { 878 return len(it.items) 879} 880 881func (it *SubscriptionIterator) takeBuf() interface{} { 882 b := it.items 883 it.items = nil 884 return b 885} 886