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

138 lines
5.6 KiB

  1. // Copyright 2016 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. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. Package pubsub provides an easy way to publish and receive Google Cloud Pub/Sub
  16. messages, hiding the details of the underlying server RPCs. Google Cloud
  17. Pub/Sub is a many-to-many, asynchronous messaging system that decouples senders
  18. and receivers.
  19. More information about Google Cloud Pub/Sub is available at
  20. https://cloud.google.com/pubsub/docs
  21. See https://godoc.org/cloud.google.com/go for authentication, timeouts,
  22. connection pooling and similar aspects of this package.
  23. Publishing
  24. Google Cloud Pub/Sub messages are published to topics. Topics may be created
  25. using the pubsub package like so:
  26. topic, err := pubsubClient.CreateTopic(context.Background(), "topic-name")
  27. Messages may then be published to a topic:
  28. res := topic.Publish(ctx, &pubsub.Message{Data: []byte("payload")})
  29. Publish queues the message for publishing and returns immediately. When enough
  30. messages have accumulated, or enough time has elapsed, the batch of messages is
  31. sent to the Pub/Sub service.
  32. Publish returns a PublishResult, which behaves like a future: its Get method
  33. blocks until the message has been sent to the service.
  34. The first time you call Publish on a topic, goroutines are started in the
  35. background. To clean up these goroutines, call Stop:
  36. topic.Stop()
  37. Receiving
  38. To receive messages published to a topic, clients create subscriptions
  39. to the topic. There may be more than one subscription per topic; each message
  40. that is published to the topic will be delivered to all of its subscriptions.
  41. Subsciptions may be created like so:
  42. sub, err := pubsubClient.CreateSubscription(context.Background(), "sub-name",
  43. pubsub.SubscriptionConfig{Topic: topic})
  44. Messages are then consumed from a subscription via callback.
  45. err := sub.Receive(context.Background(), func(ctx context.Context, m *Message) {
  46. log.Printf("Got message: %s", m.Data)
  47. m.Ack()
  48. })
  49. if err != nil {
  50. // Handle error.
  51. }
  52. The callback is invoked concurrently by multiple goroutines, maximizing
  53. throughput. To terminate a call to Receive, cancel its context.
  54. Once client code has processed the message, it must call Message.Ack, otherwise
  55. the message will eventually be redelivered. As an optimization, if the client
  56. cannot or doesn't want to process the message, it can call Message.Nack to
  57. speed redelivery. For more information and configuration options, see
  58. "Deadlines" below.
  59. Note: It is possible for Messages to be redelivered, even if Message.Ack has
  60. been called. Client code must be robust to multiple deliveries of messages.
  61. Note: This uses pubsub's streaming pull feature. This feature properties that
  62. may be surprising. Please take a look at https://cloud.google.com/pubsub/docs/pull#streamingpull
  63. for more details on how streaming pull behaves compared to the synchronous
  64. pull method.
  65. Deadlines
  66. The default pubsub deadlines are suitable for most use cases, but may be
  67. overridden. This section describes the tradeoffs that should be considered
  68. when overriding the defaults.
  69. Behind the scenes, each message returned by the Pub/Sub server has an
  70. associated lease, known as an "ACK deadline". Unless a message is
  71. acknowledged within the ACK deadline, or the client requests that
  72. the ACK deadline be extended, the message will become eligible for redelivery.
  73. As a convenience, the pubsub client will automatically extend deadlines until
  74. either:
  75. * Message.Ack or Message.Nack is called, or
  76. * The "MaxExtension" period elapses from the time the message is fetched from the server.
  77. ACK deadlines are extended periodically by the client. The initial ACK
  78. deadline given to messages is 10s. The period between extensions, as well as the
  79. length of the extension, automatically adjust depending on the time it takes to ack
  80. messages, up to 10m. This has the effect that subscribers that process messages
  81. quickly have their message ack deadlines extended for a short amount, whereas
  82. subscribers that process message slowly have their message ack deadlines extended
  83. for a large amount. The net effect is fewer RPCs sent from the client library.
  84. For example, consider a subscriber that takes 3 minutes to process each message.
  85. Since the library has already recorded several 3 minute "time to ack"s in a
  86. percentile distribution, future message extensions are sent with a value of 3
  87. minutes, every 3 minutes. Suppose the application crashes 5 seconds after the
  88. library sends such an extension: the Pub/Sub server would wait the remaining
  89. 2m55s before re-sending the messages out to other subscribers.
  90. Please note that the client library does not use the subscription's AckDeadline
  91. by default. To enforce the subscription AckDeadline, set MaxExtension to the
  92. subscription's AckDeadline:
  93. cfg, err := sub.Config(ctx)
  94. if err != nil {
  95. // TODO handle err
  96. }
  97. sub.ReceiveSettings.MaxExtension = cfg.AckDeadline
  98. Slow Message Processing
  99. For use cases where message processing exceeds 30 minutes, we recommend using
  100. the base client in a pull model, since long-lived streams are periodically killed
  101. by firewalls. See the example at https://godoc.org/cloud.google.com/go/pubsub/apiv1#example-SubscriberClient-Pull-LengthyClientProcessing
  102. */
  103. package pubsub // import "cloud.google.com/go/pubsub"