-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
stan.go
409 lines (343 loc) · 8.1 KB
/
stan.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Package stan provides a NATS Streaming broker
package stan
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/google/uuid"
"github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/codec/json"
"github.com/micro/go-micro/v2/cmd"
log "github.com/micro/go-micro/v2/logger"
stan "github.com/nats-io/stan.go"
)
type stanBroker struct {
sync.RWMutex
addrs []string
conn stan.Conn
opts broker.Options
sopts stan.Options
nopts []stan.Option
clusterID string
clientID string
connectTimeout time.Duration
connectRetry bool
done chan struct{}
ctx context.Context
}
type subscriber struct {
t string
s stan.Subscription
dq bool
opts broker.SubscribeOptions
}
type publication struct {
t string
msg *stan.Msg
m *broker.Message
err error
}
func init() {
cmd.DefaultBrokers["stan"] = NewBroker
}
func (n *publication) Topic() string {
return n.t
}
func (n *publication) Message() *broker.Message {
return n.m
}
func (n *publication) Ack() error {
return n.msg.Ack()
}
func (n *publication) Error() error {
return n.err
}
func (n *subscriber) Options() broker.SubscribeOptions {
return n.opts
}
func (n *subscriber) Topic() string {
return n.t
}
func (n *subscriber) Unsubscribe() error {
if n.s == nil {
return nil
}
// go-micro server Unsubscribe can't handle durable queues, so close as stan suggested
// from nats streaming readme:
// When a client disconnects, the streaming server is not notified, hence the importance of calling Close()
if !n.dq {
err := n.s.Unsubscribe()
if err != nil {
return err
}
}
return n.Close()
}
func (n *subscriber) Close() error {
if n.s != nil {
return n.s.Close()
}
return nil
}
func (n *stanBroker) Address() string {
// stan does not support connected server info
if len(n.addrs) > 0 {
return n.addrs[0]
}
return ""
}
func setAddrs(addrs []string) []string {
cAddrs := make([]string, 0, len(addrs))
for _, addr := range addrs {
if len(addr) == 0 {
continue
}
if !strings.HasPrefix(addr, "nats://") {
addr = "nats://" + addr
}
cAddrs = append(cAddrs, addr)
}
if len(cAddrs) == 0 {
cAddrs = []string{stan.DefaultNatsURL}
}
return cAddrs
}
func (n *stanBroker) reconnectCB(c stan.Conn, err error) {
if n.connectRetry {
if err := n.connect(); err != nil {
log.Error(err)
}
}
}
func (n *stanBroker) connect() error {
timeout := make(<-chan time.Time)
n.RLock()
if n.connectTimeout > 0 {
timeout = time.After(n.connectTimeout)
}
clusterID := n.clusterID
clientID := n.clientID
nopts := n.nopts
n.RUnlock()
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
fn := func() error {
c, err := stan.Connect(clusterID, clientID, nopts...)
if err == nil {
n.Lock()
n.conn = c
n.Unlock()
}
return err
}
// don't wait for first try
if err := fn(); err == nil {
return nil
}
n.RLock()
done := n.done
n.RUnlock()
// wait loop
for {
select {
// context closed
case <-n.opts.Context.Done():
return nil
// call close, don't wait anymore
case <-done:
return nil
// in case of timeout fail with a timeout error
case <-timeout:
return fmt.Errorf("[stan]: timeout connect to %v", n.addrs)
// got a tick, try to connect
case <-ticker.C:
err := fn()
if err == nil {
log.Infof("[stan]: successeful connected to %v", n.addrs)
return nil
}
log.Errorf("[stan]: failed to connect %v: %v\n", n.addrs, err)
}
}
return nil
}
func (n *stanBroker) Connect() error {
n.RLock()
if n.conn != nil {
n.RUnlock()
return nil
}
n.RUnlock()
clusterID, ok := n.opts.Context.Value(clusterIDKey{}).(string)
if !ok || len(clusterID) == 0 {
return errors.New("must specify ClusterID Option")
}
clientID, ok := n.opts.Context.Value(clientIDKey{}).(string)
if !ok || len(clientID) == 0 {
clientID = uuid.New().String()
}
n.Lock()
if v, ok := n.opts.Context.Value(connectRetryKey{}).(bool); ok && v {
n.connectRetry = true
}
if td, ok := n.opts.Context.Value(connectTimeoutKey{}).(time.Duration); ok {
n.connectTimeout = td
}
if n.sopts.ConnectionLostCB != nil && n.connectRetry {
n.Unlock()
return errors.New("impossible to use custom ConnectionLostCB and ConnectRetry(true)")
}
nopts := []stan.Option{
stan.NatsURL(n.sopts.NatsURL),
stan.NatsConn(n.sopts.NatsConn),
stan.ConnectWait(n.sopts.ConnectTimeout),
stan.PubAckWait(n.sopts.AckTimeout),
stan.MaxPubAcksInflight(n.sopts.MaxPubAcksInflight),
stan.Pings(n.sopts.PingInterval, n.sopts.PingMaxOut),
}
if n.connectRetry {
nopts = append(nopts, stan.SetConnectionLostHandler(n.reconnectCB))
}
nopts = append(nopts, stan.NatsURL(strings.Join(n.addrs, ",")))
n.nopts = nopts
n.clusterID = clusterID
n.clientID = clientID
n.Unlock()
return n.connect()
}
func (n *stanBroker) Disconnect() error {
var err error
n.Lock()
defer n.Unlock()
if n.done != nil {
close(n.done)
n.done = nil
}
if n.conn != nil {
err = n.conn.Close()
}
return err
}
func (n *stanBroker) Init(opts ...broker.Option) error {
for _, o := range opts {
o(&n.opts)
}
n.addrs = setAddrs(n.opts.Addrs)
return nil
}
func (n *stanBroker) Options() broker.Options {
return n.opts
}
func (n *stanBroker) Publish(topic string, msg *broker.Message, opts ...broker.PublishOption) error {
b, err := n.opts.Codec.Marshal(msg)
if err != nil {
return err
}
n.RLock()
defer n.RUnlock()
return n.conn.Publish(topic, b)
}
func (n *stanBroker) Subscribe(topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
n.RLock()
if n.conn == nil {
n.RUnlock()
return nil, errors.New("not connected")
}
n.RUnlock()
var ackSuccess bool
opt := broker.SubscribeOptions{
AutoAck: true,
}
for _, o := range opts {
o(&opt)
}
// Make sure context is setup
if opt.Context == nil {
opt.Context = context.Background()
}
ctx := opt.Context
if subscribeContext, ok := ctx.Value(subscribeContextKey{}).(context.Context); ok && subscribeContext != nil {
ctx = subscribeContext
}
var stanOpts []stan.SubscriptionOption
if !opt.AutoAck {
stanOpts = append(stanOpts, stan.SetManualAckMode())
}
if subOpts, ok := ctx.Value(subscribeOptionKey{}).([]stan.SubscriptionOption); ok && len(subOpts) > 0 {
stanOpts = append(stanOpts, subOpts...)
}
if bval, ok := ctx.Value(ackSuccessKey{}).(bool); ok && bval {
stanOpts = append(stanOpts, stan.SetManualAckMode())
ackSuccess = true
}
bopts := stan.DefaultSubscriptionOptions
for _, bopt := range stanOpts {
if err := bopt(&bopts); err != nil {
return nil, err
}
}
opt.AutoAck = !bopts.ManualAcks
if dn, ok := n.opts.Context.Value(durableKey{}).(string); ok && len(dn) > 0 {
stanOpts = append(stanOpts, stan.DurableName(dn))
bopts.DurableName = dn
}
fn := func(msg *stan.Msg) {
var m broker.Message
p := &publication{m: &m, msg: msg, t: msg.Subject}
// unmarshal message
if err := n.opts.Codec.Unmarshal(msg.Data, &m); err != nil {
p.err = err
p.m.Body = msg.Data
return
}
// execute the handler
p.err = handler(p)
// if there's no error and success auto ack is enabled ack it
if p.err == nil && ackSuccess {
msg.Ack()
}
}
var sub stan.Subscription
var err error
n.RLock()
if len(opt.Queue) > 0 {
sub, err = n.conn.QueueSubscribe(topic, opt.Queue, fn, stanOpts...)
} else {
sub, err = n.conn.Subscribe(topic, fn, stanOpts...)
}
n.RUnlock()
if err != nil {
return nil, err
}
return &subscriber{dq: len(bopts.DurableName) > 0, s: sub, opts: opt, t: topic}, nil
}
func (n *stanBroker) String() string {
return "stan"
}
func NewBroker(opts ...broker.Option) broker.Broker {
options := broker.Options{
// Default codec
Codec: json.Marshaler{},
Context: context.Background(),
}
for _, o := range opts {
o(&options)
}
stanOpts := stan.GetDefaultOptions()
if n, ok := options.Context.Value(optionsKey{}).(stan.Options); ok {
stanOpts = n
}
if len(options.Addrs) == 0 {
options.Addrs = strings.Split(stanOpts.NatsURL, ",")
}
nb := &stanBroker{
done: make(chan struct{}),
opts: options,
sopts: stanOpts,
addrs: setAddrs(options.Addrs),
}
return nb
}