forked from distribution/distribution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubsub.go
323 lines (288 loc) · 10.7 KB
/
pubsub.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package pubsub contains a Google Cloud Pub/Sub client.
//
// This package is experimental and may make backwards-incompatible changes.
//
// More information about Google Cloud Pub/Sub is available at
// https://cloud.google.com/pubsub/docs
package pubsub // import "google.golang.org/cloud/pubsub"
import (
"encoding/base64"
"errors"
"fmt"
"net/http"
"time"
"google.golang.org/api/googleapi"
raw "google.golang.org/api/pubsub/v1beta2"
"google.golang.org/cloud"
"google.golang.org/cloud/internal"
"google.golang.org/cloud/internal/transport"
"golang.org/x/net/context"
)
const (
// ScopePubSub grants permissions to view and manage Pub/Sub
// topics and subscriptions.
ScopePubSub = "https://www.googleapis.com/auth/pubsub"
// ScopeCloudPlatform grants permissions to view and manage your data
// across Google Cloud Platform services.
ScopeCloudPlatform = "https://www.googleapis.com/auth/cloud-platform"
)
const prodAddr = "https://pubsub.googleapis.com/"
const userAgent = "gcloud-golang-pubsub/20151008"
// batchLimit is maximun size of a single batch.
const batchLimit = 1000
// Message represents a Pub/Sub message.
type Message struct {
// ID identifies this message.
ID string
// AckID is the identifier to acknowledge this message.
AckID string
// Data is the actual data in the message.
Data []byte
// Attributes represents the key-value pairs the current message
// is labelled with.
Attributes map[string]string
}
// Client is a Google Pub/Sub client, which may be used to perform Pub/Sub operations with a project.
// Note: Some operations are not yet available via Client, and must be performed via the legacy standalone functions.
// It must be constructed via NewClient.
type Client struct {
projectID string
s *raw.Service
}
// NewClient create a new PubSub client.
func NewClient(ctx context.Context, projectID string, opts ...cloud.ClientOption) (*Client, error) {
o := []cloud.ClientOption{
cloud.WithEndpoint(prodAddr),
cloud.WithScopes(raw.PubsubScope, raw.CloudPlatformScope),
cloud.WithUserAgent(userAgent),
}
o = append(o, opts...)
httpClient, endpoint, err := transport.NewHTTPClient(ctx, o...)
if err != nil {
return nil, fmt.Errorf("dialing: %v", err)
}
s, err := raw.New(httpClient)
if err != nil {
return nil, err
}
s.BasePath = endpoint
c := &Client{
projectID: projectID,
s: s,
}
return c, nil
}
// TODO(jbd): Add subscription and topic listing.
// CreateSub creates a Pub/Sub subscription on the backend.
// A subscription should subscribe to an existing topic.
//
// The messages that haven't acknowledged will be pushed back to the
// subscription again when the default acknowledgement deadline is
// reached. You can override the default deadline by providing a
// non-zero deadline. Deadline must not be specified to
// precision greater than one second.
//
// As new messages are being queued on the subscription, you
// may recieve push notifications regarding to the new arrivals.
// To receive notifications of new messages in the queue,
// specify an endpoint callback URL.
// If endpoint is an empty string the backend will not notify the
// client of new messages.
//
// If the subscription already exists an error will be returned.
func CreateSub(ctx context.Context, name string, topic string, deadline time.Duration, endpoint string) error {
sub := &raw.Subscription{
Topic: fullTopicName(internal.ProjID(ctx), topic),
}
if int64(deadline) > 0 {
if !isSec(deadline) {
return errors.New("pubsub: deadline must not be specified to precision greater than one second")
}
sub.AckDeadlineSeconds = int64(deadline / time.Second)
}
if endpoint != "" {
sub.PushConfig = &raw.PushConfig{PushEndpoint: endpoint}
}
_, err := rawService(ctx).Projects.Subscriptions.Create(fullSubName(internal.ProjID(ctx), name), sub).Do()
return err
}
// DeleteSub deletes the subscription.
func DeleteSub(ctx context.Context, name string) error {
_, err := rawService(ctx).Projects.Subscriptions.Delete(fullSubName(internal.ProjID(ctx), name)).Do()
return err
}
// ModifyAckDeadline modifies the acknowledgement deadline
// for the messages retrieved from the specified subscription.
// Deadline must not be specified to precision greater than one second.
func ModifyAckDeadline(ctx context.Context, sub string, id string, deadline time.Duration) error {
if !isSec(deadline) {
return errors.New("pubsub: deadline must not be specified to precision greater than one second")
}
_, err := rawService(ctx).Projects.Subscriptions.ModifyAckDeadline(fullSubName(internal.ProjID(ctx), sub), &raw.ModifyAckDeadlineRequest{
AckDeadlineSeconds: int64(deadline / time.Second),
AckId: id,
}).Do()
return err
}
// ModifyPushEndpoint modifies the URL endpoint to modify the resource
// to handle push notifications coming from the Pub/Sub backend
// for the specified subscription.
func ModifyPushEndpoint(ctx context.Context, sub, endpoint string) error {
_, err := rawService(ctx).Projects.Subscriptions.ModifyPushConfig(fullSubName(internal.ProjID(ctx), sub), &raw.ModifyPushConfigRequest{
PushConfig: &raw.PushConfig{
PushEndpoint: endpoint,
},
}).Do()
return err
}
// SubExists returns true if subscription exists.
func SubExists(ctx context.Context, name string) (bool, error) {
_, err := rawService(ctx).Projects.Subscriptions.Get(fullSubName(internal.ProjID(ctx), name)).Do()
if e, ok := err.(*googleapi.Error); ok && e.Code == http.StatusNotFound {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
// Ack acknowledges one or more Pub/Sub messages on the
// specified subscription.
func Ack(ctx context.Context, sub string, id ...string) error {
for idx, ackID := range id {
if ackID == "" {
return fmt.Errorf("pubsub: empty ackID detected at index %d", idx)
}
}
_, err := rawService(ctx).Projects.Subscriptions.Acknowledge(fullSubName(internal.ProjID(ctx), sub), &raw.AcknowledgeRequest{
AckIds: id,
}).Do()
return err
}
func toMessage(resp *raw.ReceivedMessage) (*Message, error) {
if resp.Message == nil {
return &Message{AckID: resp.AckId}, nil
}
data, err := base64.StdEncoding.DecodeString(resp.Message.Data)
if err != nil {
return nil, err
}
return &Message{
AckID: resp.AckId,
Data: data,
Attributes: resp.Message.Attributes,
ID: resp.Message.MessageId,
}, nil
}
// Pull pulls messages from the subscription. It returns up to n
// number of messages, and n could not be larger than 100.
func Pull(ctx context.Context, sub string, n int) ([]*Message, error) {
return pull(ctx, sub, n, true)
}
// PullWait pulls messages from the subscription. If there are not
// enough messages left in the subscription queue, it will block until
// at least n number of messages arrive or timeout occurs, and n could
// not be larger than 100.
func PullWait(ctx context.Context, sub string, n int) ([]*Message, error) {
return pull(ctx, sub, n, false)
}
func pull(ctx context.Context, sub string, n int, retImmediately bool) ([]*Message, error) {
if n < 1 || n > batchLimit {
return nil, fmt.Errorf("pubsub: cannot pull less than one, more than %d messages, but %d was given", batchLimit, n)
}
resp, err := rawService(ctx).Projects.Subscriptions.Pull(fullSubName(internal.ProjID(ctx), sub), &raw.PullRequest{
ReturnImmediately: retImmediately,
MaxMessages: int64(n),
}).Do()
if err != nil {
return nil, err
}
msgs := make([]*Message, len(resp.ReceivedMessages))
for i := 0; i < len(resp.ReceivedMessages); i++ {
msg, err := toMessage(resp.ReceivedMessages[i])
if err != nil {
return nil, fmt.Errorf("pubsub: cannot decode the retrieved message at index: %d, PullResponse: %+v", i, resp.ReceivedMessages[i])
}
msgs[i] = msg
}
return msgs, nil
}
// CreateTopic creates a new topic with the specified name on the backend.
// It will return an error if topic already exists.
func CreateTopic(ctx context.Context, name string) error {
_, err := rawService(ctx).Projects.Topics.Create(fullTopicName(internal.ProjID(ctx), name), &raw.Topic{}).Do()
return err
}
// DeleteTopic deletes the specified topic.
func DeleteTopic(ctx context.Context, name string) error {
_, err := rawService(ctx).Projects.Topics.Delete(fullTopicName(internal.ProjID(ctx), name)).Do()
return err
}
// TopicExists returns true if a topic exists with the specified name.
func TopicExists(ctx context.Context, name string) (bool, error) {
_, err := rawService(ctx).Projects.Topics.Get(fullTopicName(internal.ProjID(ctx), name)).Do()
if e, ok := err.(*googleapi.Error); ok && e.Code == http.StatusNotFound {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
// Publish publish messages to the topic's subscribers. It returns
// message IDs upon success.
func Publish(ctx context.Context, topic string, msgs ...*Message) ([]string, error) {
var rawMsgs []*raw.PubsubMessage
if len(msgs) == 0 {
return nil, errors.New("pubsub: no messages to publish")
}
if len(msgs) > batchLimit {
return nil, fmt.Errorf("pubsub: %d messages given, but maximum batch size is %d", len(msgs), batchLimit)
}
rawMsgs = make([]*raw.PubsubMessage, len(msgs))
for i, msg := range msgs {
rawMsgs[i] = &raw.PubsubMessage{
Data: base64.StdEncoding.EncodeToString(msg.Data),
Attributes: msg.Attributes,
}
}
resp, err := rawService(ctx).Projects.Topics.Publish(fullTopicName(internal.ProjID(ctx), topic), &raw.PublishRequest{
Messages: rawMsgs,
}).Do()
if err != nil {
return nil, err
}
return resp.MessageIds, nil
}
// fullSubName returns the fully qualified name for a subscription.
// E.g. /subscriptions/project-id/subscription-name.
func fullSubName(proj, name string) string {
return fmt.Sprintf("projects/%s/subscriptions/%s", proj, name)
}
// fullTopicName returns the fully qualified name for a topic.
// E.g. /topics/project-id/topic-name.
func fullTopicName(proj, name string) string {
return fmt.Sprintf("projects/%s/topics/%s", proj, name)
}
func isSec(dur time.Duration) bool {
return dur%time.Second == 0
}
func rawService(ctx context.Context) *raw.Service {
return internal.Service(ctx, "pubsub", func(hc *http.Client) interface{} {
svc, _ := raw.New(hc)
return svc
}).(*raw.Service)
}