-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpubsub.go
186 lines (151 loc) · 3.99 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
package pb
import (
"context"
"encoding/json"
"fmt"
"time"
"cloud.google.com/go/pubsub"
)
type gcpPubSubProvider interface {
Close() error
CreateSubscription(context.Context, string, pubsub.SubscriptionConfig) (*pubsub.Subscription, error)
CreateTopic(context.Context, string) (*pubsub.Topic, error)
CreateTopicWithConfig(context.Context, string, *pubsub.TopicConfig) (*pubsub.Topic, error)
Subscription(string) *pubsub.Subscription
Topic(string) *pubsub.Topic
}
type PubSub struct {
clnt gcpPubSubProvider
ctx context.Context
opts *PubSubOptions
}
func (p *PubSub) ensurePublishSettings() {
if p.opts.PublishSettings == (pubsub.PublishSettings{}) {
p.opts.PublishSettings = pubsub.DefaultPublishSettings
}
}
func (p *PubSub) ensureReceiveSettings() {
if p.opts.ReceiveSettings == (pubsub.ReceiveSettings{}) {
p.opts.ReceiveSettings = pubsub.DefaultReceiveSettings
}
}
func (p *PubSub) Close() error {
return p.clnt.Close()
}
func (p *PubSub) CreateSubscription(id string, sid string, fltr string, cfg ...pubsub.SubscriptionConfig) error {
// ensure we have a subscription config
ss := pubsub.SubscriptionConfig{}
if len(cfg) > 0 {
ss = cfg[0]
}
// set topic for the subscription
t := p.clnt.Topic(id)
ss.Topic = t
// check to see if the requested subscription already exists
exists, err := p.clnt.Subscription(sid).Exists(p.ctx)
if err != nil {
return err
}
// create the subscription if it does not exist
if !exists {
// set the filter if provided
if fltr != "" {
ss.Filter = fltr
}
// create the subscription
if _, err := p.clnt.CreateSubscription(p.ctx, sid, ss); err != nil {
return err
}
}
return nil
}
func (p *PubSub) CreateSubscriptions(id string, sids map[string]string, cfg ...pubsub.SubscriptionConfig) error {
// create each subscription
for sid, f := range sids {
if err := p.CreateSubscription(id, sid, f, cfg...); err != nil {
return err
}
}
return nil
}
func (p *PubSub) CreateTopic(id string, cfg ...pubsub.TopicConfig) error {
// check to see if the requested topic already exists
exists, err := p.clnt.Topic(id).Exists(p.ctx)
if err != nil {
return err
}
// create the topic with or without configuration
ct := func() error {
if len(cfg) > 0 {
if _, err := p.clnt.CreateTopicWithConfig(p.ctx, id, &cfg[0]); err != nil {
return err
}
}
if _, err := p.clnt.CreateTopic(p.ctx, id); err != nil {
return err
}
return nil
}
// create the topic if it does not exist
if !exists {
if err := ct(); err != nil {
return err
}
}
// topic exists
return nil
}
func (p *PubSub) Publish(id string, d any, attrs ...map[string]string) error {
t := p.clnt.Topic(id)
// apply PublishSettings
p.ensurePublishSettings()
t.PublishSettings = p.opts.PublishSettings
// marshal provided data as JSON if needed
var dta []byte
if _, ok := d.([]byte); !ok {
data, err := json.Marshal(d)
if err != nil {
return err
}
dta = data
}
// ensure data to published is set
if dta == nil {
dta = d.([]byte)
}
// apply OriginatedAt attribute
mgd := mergeMaps(attrs...)
// set OriginatedAt attribute if not set and AutoOriginatedAt is true
if _, ok := mgd["OriginatedAt"]; p.opts.AutoOriginatedAt && !ok {
mgd["OriginatedAt"] = fmt.Sprintf("%v", time.Now().Unix())
}
// publish the message
res := t.Publish(p.ctx, &pubsub.Message{
Data: dta,
Attributes: mgd,
})
// get the result to ensure message was published
if _, err := res.Get(p.ctx); err != nil {
return err
}
return nil
}
func (p *PubSub) Receive(id string, mc chan<- *pubsub.Message) error {
sub := p.clnt.Subscription(id)
p.ensureReceiveSettings()
sub.ReceiveSettings = p.opts.ReceiveSettings
return sub.Receive(p.ctx, func(ctx context.Context, m *pubsub.Message) {
mc <- m
})
}
func NewPubSub(ctx context.Context, opts *PubSubOptions) (*PubSub, error) {
clnt, err := pubsub.NewClient(ctx, opts.ProjectID, opts.ClientOptions...)
if err != nil {
return nil, err
}
return &PubSub{
clnt: clnt,
ctx: ctx,
opts: opts,
}, nil
}