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.
 
 
 

124 lines
5.2 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 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. Deadlines
  62. The default pubsub deadlines are suitable for most use cases, but may be
  63. overridden. This section describes the tradeoffs that should be considered
  64. when overriding the defaults.
  65. Behind the scenes, each message returned by the Pub/Sub server has an
  66. associated lease, known as an "ACK deadline".
  67. Unless a message is acknowledged within the ACK deadline, or the client requests that
  68. the ACK deadline be extended, the message will become elegible for redelivery.
  69. As a convenience, the pubsub package will automatically extend deadlines until
  70. either:
  71. * Message.Ack or Message.Nack is called, or
  72. * the "MaxExtension" period elapses from the time the message is fetched from the server.
  73. The initial ACK deadline given to each messages defaults to 10 seconds, but may
  74. be overridden during subscription creation. Selecting an ACK deadline is a
  75. tradeoff between message redelivery latency and RPC volume. If the pubsub
  76. package fails to acknowledge or extend a message (e.g. due to unexpected
  77. termination of the process), a shorter ACK deadline will generally result in
  78. faster message redelivery by the Pub/Sub system. However, a short ACK deadline
  79. may also increase the number of deadline extension RPCs that the pubsub package
  80. sends to the server.
  81. The default max extension period is DefaultReceiveSettings.MaxExtension, and can
  82. be overridden by setting Subscription.ReceiveSettings.MaxExtension. Selecting a
  83. max extension period is a tradeoff between the speed at which client code must
  84. process messages, and the redelivery delay if messages fail to be acknowledged
  85. (e.g. because client code neglects to do so). Using a large MaxExtension
  86. increases the available time for client code to process messages. However, if
  87. the client code neglects to call Message.Ack/Nack, a large MaxExtension will
  88. increase the delay before the message is redelivered.
  89. Slow Message Processing
  90. For use cases where message processing exceeds 30 minutes, we recommend using
  91. the base client in a pull model, since long-lived streams are periodically killed
  92. by firewalls. See the example at https://godoc.org/cloud.google.com/go/pubsub/apiv1#example-SubscriberClient-Pull-LengthyClientProcessing
  93. */
  94. package pubsub // import "cloud.google.com/go/pubsub"