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.

publisher_client.go 15 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. // PublisherCallOptions contains the retry settings for each method of PublisherClient.
  33. type PublisherCallOptions struct {
  34. CreateTopic []gax.CallOption
  35. UpdateTopic []gax.CallOption
  36. Publish []gax.CallOption
  37. GetTopic []gax.CallOption
  38. ListTopics []gax.CallOption
  39. ListTopicSubscriptions []gax.CallOption
  40. DeleteTopic []gax.CallOption
  41. }
  42. func defaultPublisherClientOptions() []option.ClientOption {
  43. return []option.ClientOption{
  44. option.WithEndpoint("pubsub.googleapis.com:443"),
  45. option.WithScopes(DefaultAuthScopes()...),
  46. option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
  47. grpc.MaxCallRecvMsgSize(math.MaxInt32))),
  48. }
  49. }
  50. func defaultPublisherCallOptions() *PublisherCallOptions {
  51. retry := map[[2]string][]gax.CallOption{
  52. {"default", "idempotent"}: {
  53. gax.WithRetry(func() gax.Retryer {
  54. return gax.OnCodes([]codes.Code{
  55. codes.Aborted,
  56. codes.Unavailable,
  57. codes.Unknown,
  58. }, gax.Backoff{
  59. Initial: 100 * time.Millisecond,
  60. Max: 60000 * time.Millisecond,
  61. Multiplier: 1.3,
  62. })
  63. }),
  64. },
  65. {"default", "non_idempotent"}: {
  66. gax.WithRetry(func() gax.Retryer {
  67. return gax.OnCodes([]codes.Code{
  68. codes.Unavailable,
  69. }, gax.Backoff{
  70. Initial: 100 * time.Millisecond,
  71. Max: 60000 * time.Millisecond,
  72. Multiplier: 1.3,
  73. })
  74. }),
  75. },
  76. {"messaging", "publish"}: {
  77. gax.WithRetry(func() gax.Retryer {
  78. return gax.OnCodes([]codes.Code{
  79. codes.Aborted,
  80. codes.Canceled,
  81. codes.DeadlineExceeded,
  82. codes.Internal,
  83. codes.ResourceExhausted,
  84. codes.Unavailable,
  85. codes.Unknown,
  86. }, gax.Backoff{
  87. Initial: 100 * time.Millisecond,
  88. Max: 60000 * time.Millisecond,
  89. Multiplier: 1.3,
  90. })
  91. }),
  92. },
  93. }
  94. return &PublisherCallOptions{
  95. CreateTopic: retry[[2]string{"default", "non_idempotent"}],
  96. UpdateTopic: retry[[2]string{"default", "non_idempotent"}],
  97. Publish: retry[[2]string{"messaging", "publish"}],
  98. GetTopic: retry[[2]string{"default", "idempotent"}],
  99. ListTopics: retry[[2]string{"default", "idempotent"}],
  100. ListTopicSubscriptions: retry[[2]string{"default", "idempotent"}],
  101. DeleteTopic: retry[[2]string{"default", "non_idempotent"}],
  102. }
  103. }
  104. // PublisherClient is a client for interacting with Google Cloud Pub/Sub API.
  105. //
  106. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  107. type PublisherClient struct {
  108. // The connection to the service.
  109. conn *grpc.ClientConn
  110. // The gRPC API client.
  111. publisherClient pubsubpb.PublisherClient
  112. // The call options for this service.
  113. CallOptions *PublisherCallOptions
  114. // The x-goog-* metadata to be sent with each request.
  115. xGoogMetadata metadata.MD
  116. }
  117. // NewPublisherClient creates a new publisher client.
  118. //
  119. // The service that an application uses to manipulate topics, and to send
  120. // messages to a topic.
  121. func NewPublisherClient(ctx context.Context, opts ...option.ClientOption) (*PublisherClient, error) {
  122. conn, err := transport.DialGRPC(ctx, append(defaultPublisherClientOptions(), opts...)...)
  123. if err != nil {
  124. return nil, err
  125. }
  126. c := &PublisherClient{
  127. conn: conn,
  128. CallOptions: defaultPublisherCallOptions(),
  129. publisherClient: pubsubpb.NewPublisherClient(conn),
  130. }
  131. c.SetGoogleClientInfo()
  132. return c, nil
  133. }
  134. // Connection returns the client's connection to the API service.
  135. func (c *PublisherClient) Connection() *grpc.ClientConn {
  136. return c.conn
  137. }
  138. // Close closes the connection to the API service. The user should invoke this when
  139. // the client is no longer required.
  140. func (c *PublisherClient) Close() error {
  141. return c.conn.Close()
  142. }
  143. // SetGoogleClientInfo sets the name and version of the application in
  144. // the `x-goog-api-client` header passed on each request. Intended for
  145. // use by Google-written clients.
  146. func (c *PublisherClient) SetGoogleClientInfo(keyval ...string) {
  147. kv := append([]string{"gl-go", versionGo()}, keyval...)
  148. kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
  149. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  150. }
  151. // CreateTopic creates the given topic with the given name. See the
  152. // <a href="https://cloud.google.com/pubsub/docs/admin#resource_names">
  153. // resource name rules</a>.
  154. func (c *PublisherClient) CreateTopic(ctx context.Context, req *pubsubpb.Topic, opts ...gax.CallOption) (*pubsubpb.Topic, error) {
  155. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
  156. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  157. opts = append(c.CallOptions.CreateTopic[0:len(c.CallOptions.CreateTopic):len(c.CallOptions.CreateTopic)], opts...)
  158. var resp *pubsubpb.Topic
  159. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  160. var err error
  161. resp, err = c.publisherClient.CreateTopic(ctx, req, settings.GRPC...)
  162. return err
  163. }, opts...)
  164. if err != nil {
  165. return nil, err
  166. }
  167. return resp, nil
  168. }
  169. // UpdateTopic updates an existing topic. Note that certain properties of a
  170. // topic are not modifiable.
  171. func (c *PublisherClient) UpdateTopic(ctx context.Context, req *pubsubpb.UpdateTopicRequest, opts ...gax.CallOption) (*pubsubpb.Topic, error) {
  172. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "topic.name", url.QueryEscape(req.GetTopic().GetName())))
  173. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  174. opts = append(c.CallOptions.UpdateTopic[0:len(c.CallOptions.UpdateTopic):len(c.CallOptions.UpdateTopic)], opts...)
  175. var resp *pubsubpb.Topic
  176. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  177. var err error
  178. resp, err = c.publisherClient.UpdateTopic(ctx, req, settings.GRPC...)
  179. return err
  180. }, opts...)
  181. if err != nil {
  182. return nil, err
  183. }
  184. return resp, nil
  185. }
  186. // Publish adds one or more messages to the topic. Returns NOT_FOUND if the topic
  187. // does not exist.
  188. func (c *PublisherClient) Publish(ctx context.Context, req *pubsubpb.PublishRequest, opts ...gax.CallOption) (*pubsubpb.PublishResponse, error) {
  189. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "topic", url.QueryEscape(req.GetTopic())))
  190. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  191. opts = append(c.CallOptions.Publish[0:len(c.CallOptions.Publish):len(c.CallOptions.Publish)], opts...)
  192. var resp *pubsubpb.PublishResponse
  193. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  194. var err error
  195. resp, err = c.publisherClient.Publish(ctx, req, settings.GRPC...)
  196. return err
  197. }, opts...)
  198. if err != nil {
  199. return nil, err
  200. }
  201. return resp, nil
  202. }
  203. // GetTopic gets the configuration of a topic.
  204. func (c *PublisherClient) GetTopic(ctx context.Context, req *pubsubpb.GetTopicRequest, opts ...gax.CallOption) (*pubsubpb.Topic, error) {
  205. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "topic", url.QueryEscape(req.GetTopic())))
  206. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  207. opts = append(c.CallOptions.GetTopic[0:len(c.CallOptions.GetTopic):len(c.CallOptions.GetTopic)], opts...)
  208. var resp *pubsubpb.Topic
  209. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  210. var err error
  211. resp, err = c.publisherClient.GetTopic(ctx, req, settings.GRPC...)
  212. return err
  213. }, opts...)
  214. if err != nil {
  215. return nil, err
  216. }
  217. return resp, nil
  218. }
  219. // ListTopics lists matching topics.
  220. func (c *PublisherClient) ListTopics(ctx context.Context, req *pubsubpb.ListTopicsRequest, opts ...gax.CallOption) *TopicIterator {
  221. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "project", url.QueryEscape(req.GetProject())))
  222. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  223. opts = append(c.CallOptions.ListTopics[0:len(c.CallOptions.ListTopics):len(c.CallOptions.ListTopics)], opts...)
  224. it := &TopicIterator{}
  225. req = proto.Clone(req).(*pubsubpb.ListTopicsRequest)
  226. it.InternalFetch = func(pageSize int, pageToken string) ([]*pubsubpb.Topic, string, error) {
  227. var resp *pubsubpb.ListTopicsResponse
  228. req.PageToken = pageToken
  229. if pageSize > math.MaxInt32 {
  230. req.PageSize = math.MaxInt32
  231. } else {
  232. req.PageSize = int32(pageSize)
  233. }
  234. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  235. var err error
  236. resp, err = c.publisherClient.ListTopics(ctx, req, settings.GRPC...)
  237. return err
  238. }, opts...)
  239. if err != nil {
  240. return nil, "", err
  241. }
  242. return resp.Topics, resp.NextPageToken, nil
  243. }
  244. fetch := func(pageSize int, pageToken string) (string, error) {
  245. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  246. if err != nil {
  247. return "", err
  248. }
  249. it.items = append(it.items, items...)
  250. return nextPageToken, nil
  251. }
  252. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  253. it.pageInfo.MaxSize = int(req.PageSize)
  254. it.pageInfo.Token = req.PageToken
  255. return it
  256. }
  257. // ListTopicSubscriptions lists the names of the subscriptions on this topic.
  258. func (c *PublisherClient) ListTopicSubscriptions(ctx context.Context, req *pubsubpb.ListTopicSubscriptionsRequest, opts ...gax.CallOption) *StringIterator {
  259. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "topic", url.QueryEscape(req.GetTopic())))
  260. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  261. opts = append(c.CallOptions.ListTopicSubscriptions[0:len(c.CallOptions.ListTopicSubscriptions):len(c.CallOptions.ListTopicSubscriptions)], opts...)
  262. it := &StringIterator{}
  263. req = proto.Clone(req).(*pubsubpb.ListTopicSubscriptionsRequest)
  264. it.InternalFetch = func(pageSize int, pageToken string) ([]string, string, error) {
  265. var resp *pubsubpb.ListTopicSubscriptionsResponse
  266. req.PageToken = pageToken
  267. if pageSize > math.MaxInt32 {
  268. req.PageSize = math.MaxInt32
  269. } else {
  270. req.PageSize = int32(pageSize)
  271. }
  272. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  273. var err error
  274. resp, err = c.publisherClient.ListTopicSubscriptions(ctx, req, settings.GRPC...)
  275. return err
  276. }, opts...)
  277. if err != nil {
  278. return nil, "", err
  279. }
  280. return resp.Subscriptions, resp.NextPageToken, nil
  281. }
  282. fetch := func(pageSize int, pageToken string) (string, error) {
  283. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  284. if err != nil {
  285. return "", err
  286. }
  287. it.items = append(it.items, items...)
  288. return nextPageToken, nil
  289. }
  290. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  291. it.pageInfo.MaxSize = int(req.PageSize)
  292. it.pageInfo.Token = req.PageToken
  293. return it
  294. }
  295. // DeleteTopic deletes the topic with the given name. Returns NOT_FOUND if the topic
  296. // does not exist. After a topic is deleted, a new topic may be created with
  297. // the same name; this is an entirely new topic with none of the old
  298. // configuration or subscriptions. Existing subscriptions to this topic are
  299. // not deleted, but their topic field is set to _deleted-topic_.
  300. func (c *PublisherClient) DeleteTopic(ctx context.Context, req *pubsubpb.DeleteTopicRequest, opts ...gax.CallOption) error {
  301. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "topic", url.QueryEscape(req.GetTopic())))
  302. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  303. opts = append(c.CallOptions.DeleteTopic[0:len(c.CallOptions.DeleteTopic):len(c.CallOptions.DeleteTopic)], opts...)
  304. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  305. var err error
  306. _, err = c.publisherClient.DeleteTopic(ctx, req, settings.GRPC...)
  307. return err
  308. }, opts...)
  309. return err
  310. }
  311. // StringIterator manages a stream of string.
  312. type StringIterator struct {
  313. items []string
  314. pageInfo *iterator.PageInfo
  315. nextFunc func() error
  316. // InternalFetch is for use by the Google Cloud Libraries only.
  317. // It is not part of the stable interface of this package.
  318. //
  319. // InternalFetch returns results from a single call to the underlying RPC.
  320. // The number of results is no greater than pageSize.
  321. // If there are no more results, nextPageToken is empty and err is nil.
  322. InternalFetch func(pageSize int, pageToken string) (results []string, nextPageToken string, err error)
  323. }
  324. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  325. func (it *StringIterator) PageInfo() *iterator.PageInfo {
  326. return it.pageInfo
  327. }
  328. // Next returns the next result. Its second return value is iterator.Done if there are no more
  329. // results. Once Next returns Done, all subsequent calls will return Done.
  330. func (it *StringIterator) Next() (string, error) {
  331. var item string
  332. if err := it.nextFunc(); err != nil {
  333. return item, err
  334. }
  335. item = it.items[0]
  336. it.items = it.items[1:]
  337. return item, nil
  338. }
  339. func (it *StringIterator) bufLen() int {
  340. return len(it.items)
  341. }
  342. func (it *StringIterator) takeBuf() interface{} {
  343. b := it.items
  344. it.items = nil
  345. return b
  346. }
  347. // TopicIterator manages a stream of *pubsubpb.Topic.
  348. type TopicIterator struct {
  349. items []*pubsubpb.Topic
  350. pageInfo *iterator.PageInfo
  351. nextFunc func() error
  352. // InternalFetch is for use by the Google Cloud Libraries only.
  353. // It is not part of the stable interface of this package.
  354. //
  355. // InternalFetch returns results from a single call to the underlying RPC.
  356. // The number of results is no greater than pageSize.
  357. // If there are no more results, nextPageToken is empty and err is nil.
  358. InternalFetch func(pageSize int, pageToken string) (results []*pubsubpb.Topic, nextPageToken string, err error)
  359. }
  360. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  361. func (it *TopicIterator) PageInfo() *iterator.PageInfo {
  362. return it.pageInfo
  363. }
  364. // Next returns the next result. Its second return value is iterator.Done if there are no more
  365. // results. Once Next returns Done, all subsequent calls will return Done.
  366. func (it *TopicIterator) Next() (*pubsubpb.Topic, error) {
  367. var item *pubsubpb.Topic
  368. if err := it.nextFunc(); err != nil {
  369. return item, err
  370. }
  371. item = it.items[0]
  372. it.items = it.items[1:]
  373. return item, nil
  374. }
  375. func (it *TopicIterator) bufLen() int {
  376. return len(it.items)
  377. }
  378. func (it *TopicIterator) takeBuf() interface{} {
  379. b := it.items
  380. it.items = nil
  381. return b
  382. }