You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

subscriber_client.go 27 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. // Copyright 2019 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. // Code generated by gapic-generator. DO NOT EDIT.
  15. package pubsub
  16. import (
  17. "context"
  18. "fmt"
  19. "math"
  20. "net/url"
  21. "time"
  22. "github.com/golang/protobuf/proto"
  23. gax "github.com/googleapis/gax-go/v2"
  24. "google.golang.org/api/iterator"
  25. "google.golang.org/api/option"
  26. "google.golang.org/api/transport"
  27. pubsubpb "google.golang.org/genproto/googleapis/pubsub/v1"
  28. "google.golang.org/grpc"
  29. "google.golang.org/grpc/codes"
  30. "google.golang.org/grpc/metadata"
  31. )
  32. // SubscriberCallOptions contains the retry settings for each method of SubscriberClient.
  33. type SubscriberCallOptions struct {
  34. CreateSubscription []gax.CallOption
  35. GetSubscription []gax.CallOption
  36. UpdateSubscription []gax.CallOption
  37. ListSubscriptions []gax.CallOption
  38. DeleteSubscription []gax.CallOption
  39. ModifyAckDeadline []gax.CallOption
  40. Acknowledge []gax.CallOption
  41. Pull []gax.CallOption
  42. StreamingPull []gax.CallOption
  43. ModifyPushConfig []gax.CallOption
  44. ListSnapshots []gax.CallOption
  45. CreateSnapshot []gax.CallOption
  46. UpdateSnapshot []gax.CallOption
  47. DeleteSnapshot []gax.CallOption
  48. Seek []gax.CallOption
  49. }
  50. func defaultSubscriberClientOptions() []option.ClientOption {
  51. return []option.ClientOption{
  52. option.WithEndpoint("pubsub.googleapis.com:443"),
  53. option.WithScopes(DefaultAuthScopes()...),
  54. option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
  55. grpc.MaxCallRecvMsgSize(math.MaxInt32))),
  56. }
  57. }
  58. func defaultSubscriberCallOptions() *SubscriberCallOptions {
  59. retry := map[[2]string][]gax.CallOption{
  60. {"default", "idempotent"}: {
  61. gax.WithRetry(func() gax.Retryer {
  62. return gax.OnCodes([]codes.Code{
  63. codes.Aborted,
  64. codes.Unavailable,
  65. codes.Unknown,
  66. }, gax.Backoff{
  67. Initial: 100 * time.Millisecond,
  68. Max: 60000 * time.Millisecond,
  69. Multiplier: 1.3,
  70. })
  71. }),
  72. },
  73. {"default", "non_idempotent"}: {
  74. gax.WithRetry(func() gax.Retryer {
  75. return gax.OnCodes([]codes.Code{
  76. codes.Unavailable,
  77. }, gax.Backoff{
  78. Initial: 100 * time.Millisecond,
  79. Max: 60000 * time.Millisecond,
  80. Multiplier: 1.3,
  81. })
  82. }),
  83. },
  84. {"messaging", "idempotent"}: {
  85. gax.WithRetry(func() gax.Retryer {
  86. return gax.OnCodes([]codes.Code{
  87. codes.Aborted,
  88. codes.Unavailable,
  89. codes.Unknown,
  90. }, gax.Backoff{
  91. Initial: 100 * time.Millisecond,
  92. Max: 60000 * time.Millisecond,
  93. Multiplier: 1.3,
  94. })
  95. }),
  96. },
  97. {"messaging", "non_idempotent"}: {
  98. gax.WithRetry(func() gax.Retryer {
  99. return gax.OnCodes([]codes.Code{
  100. codes.Unavailable,
  101. }, gax.Backoff{
  102. Initial: 100 * time.Millisecond,
  103. Max: 60000 * time.Millisecond,
  104. Multiplier: 1.3,
  105. })
  106. }),
  107. },
  108. }
  109. return &SubscriberCallOptions{
  110. CreateSubscription: retry[[2]string{"default", "idempotent"}],
  111. GetSubscription: retry[[2]string{"default", "idempotent"}],
  112. UpdateSubscription: retry[[2]string{"default", "non_idempotent"}],
  113. ListSubscriptions: retry[[2]string{"default", "idempotent"}],
  114. DeleteSubscription: retry[[2]string{"default", "non_idempotent"}],
  115. ModifyAckDeadline: retry[[2]string{"default", "non_idempotent"}],
  116. Acknowledge: retry[[2]string{"messaging", "non_idempotent"}],
  117. Pull: retry[[2]string{"messaging", "idempotent"}],
  118. StreamingPull: retry[[2]string{"streaming_messaging", "none"}],
  119. ModifyPushConfig: retry[[2]string{"default", "non_idempotent"}],
  120. ListSnapshots: retry[[2]string{"default", "idempotent"}],
  121. CreateSnapshot: retry[[2]string{"default", "non_idempotent"}],
  122. UpdateSnapshot: retry[[2]string{"default", "non_idempotent"}],
  123. DeleteSnapshot: retry[[2]string{"default", "non_idempotent"}],
  124. Seek: retry[[2]string{"default", "idempotent"}],
  125. }
  126. }
  127. // SubscriberClient is a client for interacting with Google Cloud Pub/Sub API.
  128. //
  129. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  130. type SubscriberClient struct {
  131. // The connection to the service.
  132. conn *grpc.ClientConn
  133. // The gRPC API client.
  134. subscriberClient pubsubpb.SubscriberClient
  135. // The call options for this service.
  136. CallOptions *SubscriberCallOptions
  137. // The x-goog-* metadata to be sent with each request.
  138. xGoogMetadata metadata.MD
  139. }
  140. // NewSubscriberClient creates a new subscriber client.
  141. //
  142. // The service that an application uses to manipulate subscriptions and to
  143. // consume messages from a subscription via the Pull method or by
  144. // establishing a bi-directional stream using the StreamingPull method.
  145. func NewSubscriberClient(ctx context.Context, opts ...option.ClientOption) (*SubscriberClient, error) {
  146. conn, err := transport.DialGRPC(ctx, append(defaultSubscriberClientOptions(), opts...)...)
  147. if err != nil {
  148. return nil, err
  149. }
  150. c := &SubscriberClient{
  151. conn: conn,
  152. CallOptions: defaultSubscriberCallOptions(),
  153. subscriberClient: pubsubpb.NewSubscriberClient(conn),
  154. }
  155. c.SetGoogleClientInfo()
  156. return c, nil
  157. }
  158. // Connection returns the client's connection to the API service.
  159. func (c *SubscriberClient) Connection() *grpc.ClientConn {
  160. return c.conn
  161. }
  162. // Close closes the connection to the API service. The user should invoke this when
  163. // the client is no longer required.
  164. func (c *SubscriberClient) Close() error {
  165. return c.conn.Close()
  166. }
  167. // SetGoogleClientInfo sets the name and version of the application in
  168. // the `x-goog-api-client` header passed on each request. Intended for
  169. // use by Google-written clients.
  170. func (c *SubscriberClient) SetGoogleClientInfo(keyval ...string) {
  171. kv := append([]string{"gl-go", versionGo()}, keyval...)
  172. kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
  173. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  174. }
  175. // CreateSubscription creates a subscription to a given topic. See the
  176. // <a href="https://cloud.google.com/pubsub/docs/admin#resource_names">
  177. // resource name rules</a>.
  178. // If the subscription already exists, returns ALREADY_EXISTS.
  179. // If the corresponding topic doesn't exist, returns NOT_FOUND.
  180. //
  181. // If the name is not provided in the request, the server will assign a random
  182. // name for this subscription on the same project as the topic, conforming
  183. // to the
  184. // resource name
  185. // format (at https://cloud.google.com/pubsub/docs/admin#resource_names). The
  186. // generated name is populated in the returned Subscription object. Note that
  187. // for REST API requests, you must specify a name in the request.
  188. func (c *SubscriberClient) CreateSubscription(ctx context.Context, req *pubsubpb.Subscription, opts ...gax.CallOption) (*pubsubpb.Subscription, error) {
  189. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
  190. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  191. opts = append(c.CallOptions.CreateSubscription[0:len(c.CallOptions.CreateSubscription):len(c.CallOptions.CreateSubscription)], opts...)
  192. var resp *pubsubpb.Subscription
  193. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  194. var err error
  195. resp, err = c.subscriberClient.CreateSubscription(ctx, req, settings.GRPC...)
  196. return err
  197. }, opts...)
  198. if err != nil {
  199. return nil, err
  200. }
  201. return resp, nil
  202. }
  203. // GetSubscription gets the configuration details of a subscription.
  204. func (c *SubscriberClient) GetSubscription(ctx context.Context, req *pubsubpb.GetSubscriptionRequest, opts ...gax.CallOption) (*pubsubpb.Subscription, error) {
  205. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "subscription", url.QueryEscape(req.GetSubscription())))
  206. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  207. opts = append(c.CallOptions.GetSubscription[0:len(c.CallOptions.GetSubscription):len(c.CallOptions.GetSubscription)], opts...)
  208. var resp *pubsubpb.Subscription
  209. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  210. var err error
  211. resp, err = c.subscriberClient.GetSubscription(ctx, req, settings.GRPC...)
  212. return err
  213. }, opts...)
  214. if err != nil {
  215. return nil, err
  216. }
  217. return resp, nil
  218. }
  219. // UpdateSubscription updates an existing subscription. Note that certain properties of a
  220. // subscription, such as its topic, are not modifiable.
  221. func (c *SubscriberClient) UpdateSubscription(ctx context.Context, req *pubsubpb.UpdateSubscriptionRequest, opts ...gax.CallOption) (*pubsubpb.Subscription, error) {
  222. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "subscription.name", url.QueryEscape(req.GetSubscription().GetName())))
  223. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  224. opts = append(c.CallOptions.UpdateSubscription[0:len(c.CallOptions.UpdateSubscription):len(c.CallOptions.UpdateSubscription)], opts...)
  225. var resp *pubsubpb.Subscription
  226. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  227. var err error
  228. resp, err = c.subscriberClient.UpdateSubscription(ctx, req, settings.GRPC...)
  229. return err
  230. }, opts...)
  231. if err != nil {
  232. return nil, err
  233. }
  234. return resp, nil
  235. }
  236. // ListSubscriptions lists matching subscriptions.
  237. func (c *SubscriberClient) ListSubscriptions(ctx context.Context, req *pubsubpb.ListSubscriptionsRequest, opts ...gax.CallOption) *SubscriptionIterator {
  238. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "project", url.QueryEscape(req.GetProject())))
  239. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  240. opts = append(c.CallOptions.ListSubscriptions[0:len(c.CallOptions.ListSubscriptions):len(c.CallOptions.ListSubscriptions)], opts...)
  241. it := &SubscriptionIterator{}
  242. req = proto.Clone(req).(*pubsubpb.ListSubscriptionsRequest)
  243. it.InternalFetch = func(pageSize int, pageToken string) ([]*pubsubpb.Subscription, string, error) {
  244. var resp *pubsubpb.ListSubscriptionsResponse
  245. req.PageToken = pageToken
  246. if pageSize > math.MaxInt32 {
  247. req.PageSize = math.MaxInt32
  248. } else {
  249. req.PageSize = int32(pageSize)
  250. }
  251. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  252. var err error
  253. resp, err = c.subscriberClient.ListSubscriptions(ctx, req, settings.GRPC...)
  254. return err
  255. }, opts...)
  256. if err != nil {
  257. return nil, "", err
  258. }
  259. return resp.Subscriptions, resp.NextPageToken, nil
  260. }
  261. fetch := func(pageSize int, pageToken string) (string, error) {
  262. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  263. if err != nil {
  264. return "", err
  265. }
  266. it.items = append(it.items, items...)
  267. return nextPageToken, nil
  268. }
  269. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  270. it.pageInfo.MaxSize = int(req.PageSize)
  271. it.pageInfo.Token = req.PageToken
  272. return it
  273. }
  274. // DeleteSubscription deletes an existing subscription. All messages retained in the subscription
  275. // are immediately dropped. Calls to Pull after deletion will return
  276. // NOT_FOUND. After a subscription is deleted, a new one may be created with
  277. // the same name, but the new one has no association with the old
  278. // subscription or its topic unless the same topic is specified.
  279. func (c *SubscriberClient) DeleteSubscription(ctx context.Context, req *pubsubpb.DeleteSubscriptionRequest, opts ...gax.CallOption) error {
  280. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "subscription", url.QueryEscape(req.GetSubscription())))
  281. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  282. opts = append(c.CallOptions.DeleteSubscription[0:len(c.CallOptions.DeleteSubscription):len(c.CallOptions.DeleteSubscription)], opts...)
  283. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  284. var err error
  285. _, err = c.subscriberClient.DeleteSubscription(ctx, req, settings.GRPC...)
  286. return err
  287. }, opts...)
  288. return err
  289. }
  290. // ModifyAckDeadline modifies the ack deadline for a specific message. This method is useful
  291. // to indicate that more time is needed to process a message by the
  292. // subscriber, or to make the message available for redelivery if the
  293. // processing was interrupted. Note that this does not modify the
  294. // subscription-level ackDeadlineSeconds used for subsequent messages.
  295. func (c *SubscriberClient) ModifyAckDeadline(ctx context.Context, req *pubsubpb.ModifyAckDeadlineRequest, opts ...gax.CallOption) error {
  296. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "subscription", url.QueryEscape(req.GetSubscription())))
  297. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  298. opts = append(c.CallOptions.ModifyAckDeadline[0:len(c.CallOptions.ModifyAckDeadline):len(c.CallOptions.ModifyAckDeadline)], opts...)
  299. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  300. var err error
  301. _, err = c.subscriberClient.ModifyAckDeadline(ctx, req, settings.GRPC...)
  302. return err
  303. }, opts...)
  304. return err
  305. }
  306. // Acknowledge acknowledges the messages associated with the ack_ids in the
  307. // AcknowledgeRequest. The Pub/Sub system can remove the relevant messages
  308. // from the subscription.
  309. //
  310. // Acknowledging a message whose ack deadline has expired may succeed,
  311. // but such a message may be redelivered later. Acknowledging a message more
  312. // than once will not result in an error.
  313. func (c *SubscriberClient) Acknowledge(ctx context.Context, req *pubsubpb.AcknowledgeRequest, opts ...gax.CallOption) error {
  314. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "subscription", url.QueryEscape(req.GetSubscription())))
  315. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  316. opts = append(c.CallOptions.Acknowledge[0:len(c.CallOptions.Acknowledge):len(c.CallOptions.Acknowledge)], opts...)
  317. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  318. var err error
  319. _, err = c.subscriberClient.Acknowledge(ctx, req, settings.GRPC...)
  320. return err
  321. }, opts...)
  322. return err
  323. }
  324. // Pull pulls messages from the server. The server may return UNAVAILABLE if
  325. // there are too many concurrent pull requests pending for the given
  326. // subscription.
  327. func (c *SubscriberClient) Pull(ctx context.Context, req *pubsubpb.PullRequest, opts ...gax.CallOption) (*pubsubpb.PullResponse, error) {
  328. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "subscription", url.QueryEscape(req.GetSubscription())))
  329. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  330. opts = append(c.CallOptions.Pull[0:len(c.CallOptions.Pull):len(c.CallOptions.Pull)], opts...)
  331. var resp *pubsubpb.PullResponse
  332. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  333. var err error
  334. resp, err = c.subscriberClient.Pull(ctx, req, settings.GRPC...)
  335. return err
  336. }, opts...)
  337. if err != nil {
  338. return nil, err
  339. }
  340. return resp, nil
  341. }
  342. // StreamingPull establishes a stream with the server, which sends messages down to the
  343. // client. The client streams acknowledgements and ack deadline modifications
  344. // back to the server. The server will close the stream and return the status
  345. // on any error. The server may close the stream with status UNAVAILABLE to
  346. // reassign server-side resources, in which case, the client should
  347. // re-establish the stream. Flow control can be achieved by configuring the
  348. // underlying RPC channel.
  349. func (c *SubscriberClient) StreamingPull(ctx context.Context, opts ...gax.CallOption) (pubsubpb.Subscriber_StreamingPullClient, error) {
  350. ctx = insertMetadata(ctx, c.xGoogMetadata)
  351. opts = append(c.CallOptions.StreamingPull[0:len(c.CallOptions.StreamingPull):len(c.CallOptions.StreamingPull)], opts...)
  352. var resp pubsubpb.Subscriber_StreamingPullClient
  353. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  354. var err error
  355. resp, err = c.subscriberClient.StreamingPull(ctx, settings.GRPC...)
  356. return err
  357. }, opts...)
  358. if err != nil {
  359. return nil, err
  360. }
  361. return resp, nil
  362. }
  363. // ModifyPushConfig modifies the PushConfig for a specified subscription.
  364. //
  365. // This may be used to change a push subscription to a pull one (signified by
  366. // an empty PushConfig) or vice versa, or change the endpoint URL and other
  367. // attributes of a push subscription. Messages will accumulate for delivery
  368. // continuously through the call regardless of changes to the PushConfig.
  369. func (c *SubscriberClient) ModifyPushConfig(ctx context.Context, req *pubsubpb.ModifyPushConfigRequest, opts ...gax.CallOption) error {
  370. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "subscription", url.QueryEscape(req.GetSubscription())))
  371. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  372. opts = append(c.CallOptions.ModifyPushConfig[0:len(c.CallOptions.ModifyPushConfig):len(c.CallOptions.ModifyPushConfig)], opts...)
  373. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  374. var err error
  375. _, err = c.subscriberClient.ModifyPushConfig(ctx, req, settings.GRPC...)
  376. return err
  377. }, opts...)
  378. return err
  379. }
  380. // ListSnapshots lists the existing snapshots. Snapshots are used in
  381. // <a href="https://cloud.google.com/pubsub/docs/replay-overview">Seek</a>
  382. // operations, which allow
  383. // you to manage message acknowledgments in bulk. That is, you can set the
  384. // acknowledgment state of messages in an existing subscription to the state
  385. // captured by a snapshot.
  386. func (c *SubscriberClient) ListSnapshots(ctx context.Context, req *pubsubpb.ListSnapshotsRequest, opts ...gax.CallOption) *SnapshotIterator {
  387. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "project", url.QueryEscape(req.GetProject())))
  388. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  389. opts = append(c.CallOptions.ListSnapshots[0:len(c.CallOptions.ListSnapshots):len(c.CallOptions.ListSnapshots)], opts...)
  390. it := &SnapshotIterator{}
  391. req = proto.Clone(req).(*pubsubpb.ListSnapshotsRequest)
  392. it.InternalFetch = func(pageSize int, pageToken string) ([]*pubsubpb.Snapshot, string, error) {
  393. var resp *pubsubpb.ListSnapshotsResponse
  394. req.PageToken = pageToken
  395. if pageSize > math.MaxInt32 {
  396. req.PageSize = math.MaxInt32
  397. } else {
  398. req.PageSize = int32(pageSize)
  399. }
  400. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  401. var err error
  402. resp, err = c.subscriberClient.ListSnapshots(ctx, req, settings.GRPC...)
  403. return err
  404. }, opts...)
  405. if err != nil {
  406. return nil, "", err
  407. }
  408. return resp.Snapshots, resp.NextPageToken, nil
  409. }
  410. fetch := func(pageSize int, pageToken string) (string, error) {
  411. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  412. if err != nil {
  413. return "", err
  414. }
  415. it.items = append(it.items, items...)
  416. return nextPageToken, nil
  417. }
  418. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  419. it.pageInfo.MaxSize = int(req.PageSize)
  420. it.pageInfo.Token = req.PageToken
  421. return it
  422. }
  423. // CreateSnapshot creates a snapshot from the requested subscription. Snapshots are used in
  424. // <a href="https://cloud.google.com/pubsub/docs/replay-overview">Seek</a>
  425. // operations, which allow
  426. // you to manage message acknowledgments in bulk. That is, you can set the
  427. // acknowledgment state of messages in an existing subscription to the state
  428. // captured by a snapshot.
  429. // <br><br>If the snapshot already exists, returns ALREADY_EXISTS.
  430. // If the requested subscription doesn't exist, returns NOT_FOUND.
  431. // If the backlog in the subscription is too old -- and the resulting snapshot
  432. // would expire in less than 1 hour -- then FAILED_PRECONDITION is returned.
  433. // See also the Snapshot.expire_time field. If the name is not provided in
  434. // the request, the server will assign a random
  435. // name for this snapshot on the same project as the subscription, conforming
  436. // to the
  437. // resource name
  438. // format (at https://cloud.google.com/pubsub/docs/admin#resource_names). The
  439. // generated name is populated in the returned Snapshot object. Note that for
  440. // REST API requests, you must specify a name in the request.
  441. func (c *SubscriberClient) CreateSnapshot(ctx context.Context, req *pubsubpb.CreateSnapshotRequest, opts ...gax.CallOption) (*pubsubpb.Snapshot, error) {
  442. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
  443. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  444. opts = append(c.CallOptions.CreateSnapshot[0:len(c.CallOptions.CreateSnapshot):len(c.CallOptions.CreateSnapshot)], opts...)
  445. var resp *pubsubpb.Snapshot
  446. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  447. var err error
  448. resp, err = c.subscriberClient.CreateSnapshot(ctx, req, settings.GRPC...)
  449. return err
  450. }, opts...)
  451. if err != nil {
  452. return nil, err
  453. }
  454. return resp, nil
  455. }
  456. // UpdateSnapshot updates an existing snapshot. Snapshots are used in
  457. // <a href="https://cloud.google.com/pubsub/docs/replay-overview">Seek</a>
  458. // operations, which allow
  459. // you to manage message acknowledgments in bulk. That is, you can set the
  460. // acknowledgment state of messages in an existing subscription to the state
  461. // captured by a snapshot.
  462. func (c *SubscriberClient) UpdateSnapshot(ctx context.Context, req *pubsubpb.UpdateSnapshotRequest, opts ...gax.CallOption) (*pubsubpb.Snapshot, error) {
  463. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "snapshot.name", url.QueryEscape(req.GetSnapshot().GetName())))
  464. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  465. opts = append(c.CallOptions.UpdateSnapshot[0:len(c.CallOptions.UpdateSnapshot):len(c.CallOptions.UpdateSnapshot)], opts...)
  466. var resp *pubsubpb.Snapshot
  467. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  468. var err error
  469. resp, err = c.subscriberClient.UpdateSnapshot(ctx, req, settings.GRPC...)
  470. return err
  471. }, opts...)
  472. if err != nil {
  473. return nil, err
  474. }
  475. return resp, nil
  476. }
  477. // DeleteSnapshot removes an existing snapshot. Snapshots are used in
  478. // <a href="https://cloud.google.com/pubsub/docs/replay-overview">Seek</a>
  479. // operations, which allow
  480. // you to manage message acknowledgments in bulk. That is, you can set the
  481. // acknowledgment state of messages in an existing subscription to the state
  482. // captured by a snapshot.<br><br>
  483. // When the snapshot is deleted, all messages retained in the snapshot
  484. // are immediately dropped. After a snapshot is deleted, a new one may be
  485. // created with the same name, but the new one has no association with the old
  486. // snapshot or its subscription, unless the same subscription is specified.
  487. func (c *SubscriberClient) DeleteSnapshot(ctx context.Context, req *pubsubpb.DeleteSnapshotRequest, opts ...gax.CallOption) error {
  488. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "snapshot", url.QueryEscape(req.GetSnapshot())))
  489. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  490. opts = append(c.CallOptions.DeleteSnapshot[0:len(c.CallOptions.DeleteSnapshot):len(c.CallOptions.DeleteSnapshot)], opts...)
  491. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  492. var err error
  493. _, err = c.subscriberClient.DeleteSnapshot(ctx, req, settings.GRPC...)
  494. return err
  495. }, opts...)
  496. return err
  497. }
  498. // Seek seeks an existing subscription to a point in time or to a given snapshot,
  499. // whichever is provided in the request. Snapshots are used in
  500. // <a href="https://cloud.google.com/pubsub/docs/replay-overview">Seek</a>
  501. // operations, which allow
  502. // you to manage message acknowledgments in bulk. That is, you can set the
  503. // acknowledgment state of messages in an existing subscription to the state
  504. // captured by a snapshot. Note that both the subscription and the snapshot
  505. // must be on the same topic.
  506. func (c *SubscriberClient) Seek(ctx context.Context, req *pubsubpb.SeekRequest, opts ...gax.CallOption) (*pubsubpb.SeekResponse, error) {
  507. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "subscription", url.QueryEscape(req.GetSubscription())))
  508. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  509. opts = append(c.CallOptions.Seek[0:len(c.CallOptions.Seek):len(c.CallOptions.Seek)], opts...)
  510. var resp *pubsubpb.SeekResponse
  511. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  512. var err error
  513. resp, err = c.subscriberClient.Seek(ctx, req, settings.GRPC...)
  514. return err
  515. }, opts...)
  516. if err != nil {
  517. return nil, err
  518. }
  519. return resp, nil
  520. }
  521. // SnapshotIterator manages a stream of *pubsubpb.Snapshot.
  522. type SnapshotIterator struct {
  523. items []*pubsubpb.Snapshot
  524. pageInfo *iterator.PageInfo
  525. nextFunc func() error
  526. // InternalFetch is for use by the Google Cloud Libraries only.
  527. // It is not part of the stable interface of this package.
  528. //
  529. // InternalFetch returns results from a single call to the underlying RPC.
  530. // The number of results is no greater than pageSize.
  531. // If there are no more results, nextPageToken is empty and err is nil.
  532. InternalFetch func(pageSize int, pageToken string) (results []*pubsubpb.Snapshot, nextPageToken string, err error)
  533. }
  534. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  535. func (it *SnapshotIterator) PageInfo() *iterator.PageInfo {
  536. return it.pageInfo
  537. }
  538. // Next returns the next result. Its second return value is iterator.Done if there are no more
  539. // results. Once Next returns Done, all subsequent calls will return Done.
  540. func (it *SnapshotIterator) Next() (*pubsubpb.Snapshot, error) {
  541. var item *pubsubpb.Snapshot
  542. if err := it.nextFunc(); err != nil {
  543. return item, err
  544. }
  545. item = it.items[0]
  546. it.items = it.items[1:]
  547. return item, nil
  548. }
  549. func (it *SnapshotIterator) bufLen() int {
  550. return len(it.items)
  551. }
  552. func (it *SnapshotIterator) takeBuf() interface{} {
  553. b := it.items
  554. it.items = nil
  555. return b
  556. }
  557. // SubscriptionIterator manages a stream of *pubsubpb.Subscription.
  558. type SubscriptionIterator struct {
  559. items []*pubsubpb.Subscription
  560. pageInfo *iterator.PageInfo
  561. nextFunc func() error
  562. // InternalFetch is for use by the Google Cloud Libraries only.
  563. // It is not part of the stable interface of this package.
  564. //
  565. // InternalFetch returns results from a single call to the underlying RPC.
  566. // The number of results is no greater than pageSize.
  567. // If there are no more results, nextPageToken is empty and err is nil.
  568. InternalFetch func(pageSize int, pageToken string) (results []*pubsubpb.Subscription, nextPageToken string, err error)
  569. }
  570. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  571. func (it *SubscriptionIterator) PageInfo() *iterator.PageInfo {
  572. return it.pageInfo
  573. }
  574. // Next returns the next result. Its second return value is iterator.Done if there are no more
  575. // results. Once Next returns Done, all subsequent calls will return Done.
  576. func (it *SubscriptionIterator) Next() (*pubsubpb.Subscription, error) {
  577. var item *pubsubpb.Subscription
  578. if err := it.nextFunc(); err != nil {
  579. return item, err
  580. }
  581. item = it.items[0]
  582. it.items = it.items[1:]
  583. return item, nil
  584. }
  585. func (it *SubscriptionIterator) bufLen() int {
  586. return len(it.items)
  587. }
  588. func (it *SubscriptionIterator) takeBuf() interface{} {
  589. b := it.items
  590. it.items = nil
  591. return b
  592. }