topic.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2016 Google Inc. All Rights Reserved.
  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. package pubsub
  15. import (
  16. "fmt"
  17. "time"
  18. "golang.org/x/net/context"
  19. )
  20. const MaxPublishBatchSize = 1000
  21. // TopicHandle is a reference to a PubSub topic.
  22. type TopicHandle struct {
  23. c *Client
  24. // The fully qualified identifier for the topic, in the format "projects/<projid>/topics/<name>"
  25. name string
  26. }
  27. // NewTopic creates a new topic.
  28. // The specified topic name must start with a letter, and contain only letters
  29. // ([A-Za-z]), numbers ([0-9]), dashes (-), underscores (_), periods (.),
  30. // tildes (~), plus (+) or percent signs (%). It must be between 3 and 255
  31. // characters in length, and must not start with "goog".
  32. // If the topic already exists an error will be returned.
  33. func (c *Client) NewTopic(ctx context.Context, name string) (*TopicHandle, error) {
  34. t := c.Topic(name)
  35. err := c.s.createTopic(ctx, t.Name())
  36. return t, err
  37. }
  38. // Topic creates a reference to a topic.
  39. func (c *Client) Topic(name string) *TopicHandle {
  40. return &TopicHandle{c: c, name: fmt.Sprintf("projects/%s/topics/%s", c.projectID, name)}
  41. }
  42. // Topics lists all of the topics for the client's project.
  43. func (c *Client) Topics(ctx context.Context) ([]*TopicHandle, error) {
  44. topicNames, err := c.s.listProjectTopics(ctx, c.fullyQualifiedProjectName())
  45. if err != nil {
  46. return nil, err
  47. }
  48. topics := []*TopicHandle{}
  49. for _, t := range topicNames {
  50. topics = append(topics, &TopicHandle{c: c, name: t})
  51. }
  52. return topics, nil
  53. }
  54. // Name returns the globally unique name for the topic.
  55. func (t *TopicHandle) Name() string {
  56. return t.name
  57. }
  58. // Delete deletes the topic.
  59. func (t *TopicHandle) Delete(ctx context.Context) error {
  60. return t.c.s.deleteTopic(ctx, t.name)
  61. }
  62. // Exists reports whether the topic exists on the server.
  63. func (t *TopicHandle) Exists(ctx context.Context) (bool, error) {
  64. if t.name == "_deleted-topic_" {
  65. return false, nil
  66. }
  67. return t.c.s.topicExists(ctx, t.name)
  68. }
  69. // Subscriptions lists the subscriptions for this topic.
  70. func (t *TopicHandle) Subscriptions(ctx context.Context) ([]*SubscriptionHandle, error) {
  71. subNames, err := t.c.s.listTopicSubscriptions(ctx, t.name)
  72. if err != nil {
  73. return nil, err
  74. }
  75. subs := []*SubscriptionHandle{}
  76. for _, s := range subNames {
  77. subs = append(subs, &SubscriptionHandle{c: t.c, name: s})
  78. }
  79. return subs, nil
  80. }
  81. // Subscribe creates a new subscription to the topic.
  82. // The specified subscription name must start with a letter, and contain only
  83. // letters ([A-Za-z]), numbers ([0-9]), dashes (-), underscores (_), periods
  84. // (.), tildes (~), plus (+) or percent signs (%). It must be between 3 and 255
  85. // characters in length, and must not start with "goog".
  86. //
  87. // ackDeadline is the maximum time after a subscriber receives a message before
  88. // the subscriber should acknowledge the message. It must be between 10 and 600
  89. // seconds (inclusive), and is rounded down to the nearest second. If the
  90. // provided ackDeadline is 0, then the default value of 10 seconds is used.
  91. // Note: messages which are obtained via an Iterator need not be acknowledged
  92. // within this deadline, as the deadline will be automatically extended.
  93. //
  94. // pushConfig may be set to configure this subscription for push delivery.
  95. //
  96. // If the subscription already exists an error will be returned.
  97. func (t *TopicHandle) Subscribe(ctx context.Context, name string, ackDeadline time.Duration, pushConfig *PushConfig) (*SubscriptionHandle, error) {
  98. if ackDeadline == 0 {
  99. ackDeadline = 10 * time.Second
  100. }
  101. if d := ackDeadline.Seconds(); d < 10 || d > 600 {
  102. return nil, fmt.Errorf("ack deadline must be between 10 and 600 seconds; got: %v", d)
  103. }
  104. sub := t.c.Subscription(name)
  105. err := t.c.s.createSubscription(ctx, t.name, sub.Name(), ackDeadline, pushConfig)
  106. return sub, err
  107. }
  108. // Publish publishes the supplied Messages to the topic.
  109. // If successful, the server-assigned message IDs are returned in the same order as the supplied Messages.
  110. // At most MaxPublishBatchSize messages may be supplied.
  111. func (t *TopicHandle) Publish(ctx context.Context, msgs ...*Message) ([]string, error) {
  112. if len(msgs) == 0 {
  113. return nil, nil
  114. }
  115. if len(msgs) > MaxPublishBatchSize {
  116. return nil, fmt.Errorf("pubsub: got %d messages, but maximum batch size is %d", len(msgs), MaxPublishBatchSize)
  117. }
  118. return t.c.s.publishMessages(ctx, t.name, msgs)
  119. }