/
natty.go
614 lines (480 loc) · 16.9 KB
/
natty.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
package natty
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"sync"
"time"
"github.com/gofrs/uuid"
"github.com/nats-io/nats.go"
"github.com/pkg/errors"
"github.com/relistan/go-director"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
const (
DefaultMaxMsgs = 10_000
DefaultFetchSize = 100
DefaultFetchTimeout = time.Second * 1
DefaultDeliverPolicy = nats.DeliverLastPolicy
DefaultSubBatchSize = 256
DefaultWorkerIdleTimeout = time.Minute
DefaultPublishTimeout = time.Second * 5 // TODO: figure out a good value for this
)
var (
ErrEmptyStreamName = errors.New("StreamName cannot be empty")
ErrEmptyConsumerName = errors.New("ConsumerName cannot be empty")
ErrEmptySubject = errors.New("Subject cannot be empty")
)
type Mode int
type INatty interface {
// Consume subscribes to given subject and executes callback every time a
// message is received. Consumed messages must be explicitly ACK'd or NAK'd.
//
// This is a blocking call; cancellation should be performed via the context.
Consume(ctx context.Context, cfg *ConsumerConfig, cb func(ctx context.Context, msg *nats.Msg) error) error
// Publish publishes a single message with the given subject; this method
// will perform automatic batching as configured during `natty.New(..)`
Publish(ctx context.Context, subject string, data []byte)
// DeletePublisher shuts down a publisher and deletes it from the internal publisherMap
DeletePublisher(ctx context.Context, id string) bool
// CreateStream creates a new stream if it does not exist
CreateStream(ctx context.Context, name string, subjects []string) error
// DeleteStream deletes an existing stream
DeleteStream(ctx context.Context, name string) error
// CreateConsumer creates a new consumer if it does not exist
CreateConsumer(ctx context.Context, streamName, consumerName string, filterSubject ...string) error
// DeleteConsumer deletes an existing consumer
DeleteConsumer(ctx context.Context, consumerName, streamName string) error
// NATS key/value Get/Put/Delete/Update functionality operates on "buckets"
// that are exposed via a 'KeyValue' instance. To simplify our interface,
// our Put method will automatically create the bucket if it does not already
// exist. Get() and Delete() will not automatically create a bucket.
//
// If your functionality is creating many ephemeral buckets, you may want to
// delete the bucket after you are done via DeleteBucket().
// Get will fetch the value for a given bucket and key. Will NOT auto-create
// bucket if it does not exist.
Get(ctx context.Context, bucket string, key string) ([]byte, error)
// Create will attempt to create a key in KV. It will return an error if
// the key already exists. Will auto-create the bucket if it does not
// already exist.
Create(ctx context.Context, bucket string, key string, data []byte, keyTTL ...time.Duration) error
// Put will put a new value for a given bucket and key. Will auto-create
// the bucket if it does not already exist.
Put(ctx context.Context, bucket string, key string, data []byte, ttl ...time.Duration) error
// Delete will delete a key from a given bucket. Will no-op if the bucket
// or key does not exist.
Delete(ctx context.Context, bucket string, key string) error
// CreateBucket will attempt to create a new bucket. Will return an error if
// bucket already exists.
CreateBucket(ctx context.Context, bucket string, ttl time.Duration, replicas int, description ...string) error
// DeleteBucket will delete the specified bucket
DeleteBucket(ctx context.Context, bucket string) error
// WatchBucket returns an instance of nats.KeyWatcher for the given bucket
WatchBucket(ctx context.Context, bucket string) (nats.KeyWatcher, error)
// WatchKey returns an instance of nats.KeyWatcher for the given bucket and key
WatchKey(ctx context.Context, bucket, key string) (nats.KeyWatcher, error)
// Keys will return all of the keys in a bucket (empty slice if none found)
Keys(ctx context.Context, bucket string) ([]string, error)
// Refresh will attempt to perform a "safe" refresh of a key that has a TTL.
// Natty will first attempt to fetch the key so that it can get its revision
// and then perform an Update() referencing the revision. If the revision
// does not match, it will return an error.
Refresh(ctx context.Context, bucket, key string) error
// Status queries the status of the KV bucket
Status(ctx context.Context, bucket string) (nats.KeyValueStatus, error)
// AsLeader enables simple leader election by using NATS k/v functionality.
//
// AsLeader will execute opts.Func if and only if the node executing AsLeader
// acquires leader role. It will continue executing opts.Func until it loses
// leadership and another node becomes leader.
AsLeader(ctx context.Context, opts AsLeaderConfig, f func() error) error
// HaveLeader returns bool indicating whether node-name in given cfg is the
// leader for the cfg.Bucket and cfg.Key
HaveLeader(ctx context.Context, nodeName, bucketName, keyName string) bool
// GetLeader returns the current leader for a given bucket and key
GetLeader(ctx context.Context, bucketName, keyName string) (string, error)
}
type Config struct {
// NatsURL defines the NATS urls the library will attempt to connect to. Iff
// first URL fails, we will try to connect to the next one. Only fail if all
// URLs fail.
NatsURL []string
// MaxMsgs defines the maximum number of messages a stream will contain.
MaxMsgs int64
// FetchSize defines the number of messages to fetch from the stream during
// a single Fetch() call.
FetchSize int
// FetchTimeout defines how long a Fetch() call will wait to attempt to reach
// defined FetchSize before continuing.
FetchTimeout time.Duration
// DeliverPolicy defines the policy the library will use to deliver messages.
// Default: DeliverLastPolicy which will deliver from the last message that
// the consumer has seen.
DeliverPolicy nats.DeliverPolicy
// Logger allows you to inject a logger into the library. Optional.
Logger Logger
// Whether to use TLS
UseTLS bool
// TLS CA certificate file
TLSCACertFile string
// TLS client certificate file
TLSClientCertFile string
// TLS client key file
TLSClientKeyFile string
// Do not perform server certificate checks
TLSSkipVerify bool
// PublishBatchSize is how many messages to async publish at once
// Default: 256
PublishBatchSize int
// ServiceShutdownContext is used by main() to shutdown services before application termination
ServiceShutdownContext context.Context
// MainShutdownFunc is triggered by watchForShutdown() after all publisher queues are exhausted
// and is used to trigger shutdown of APIs and then main()
MainShutdownFunc context.CancelFunc
// WorkerIdleTimeout determines how long to keep a publish worker alive if no activity
WorkerIdleTimeout time.Duration
// PublishTimeout is how long to wait for a batch of async publish calls to be ACK'd
PublishTimeout time.Duration
// PublishErrorCh will receive any
PublishErrorCh chan *PublishError
}
// ConsumerConfig is used to pass configuration options to Consume()
type ConsumerConfig struct {
// Subject is the subject to consume off of a stream
Subject string
// StreamName is the name of JS stream to consume from.
// This should first be created with CreateStream()
StreamName string
// ConsumerName is the consumer that was made with CreateConsumer()
ConsumerName string
// Looper is optional, if none is provided, one will be created
Looper director.Looper
// ErrorCh is used to retrieve any errors returned during asynchronous publishing
// If nil, errors will only be logged
ErrorCh chan error
}
type Publisher struct {
Subject string
QueueMutex *sync.RWMutex
Queue []*message
Natty *Natty
IdleTimeout time.Duration
looper director.Looper
// ErrorCh is optional. It will receive async publish errors if specified
// Otherwise errors will only be logged
ErrorCh chan *PublishError
// PublisherContext is used to close a specific publisher
PublisherContext context.Context
// PublisherCancel is used to cancel a specific publisher's context
PublisherCancel context.CancelFunc
// ServiceShutdownContext is used by main() to shutdown services before application termination
ServiceShutdownContext context.Context
log Logger
}
// message is a convenience struct to hold message data for a batch
type message struct {
Subject string
Value []byte
}
// PublishError is a wrapper struct used to return errors to code that occur during async batch publishes
type PublishError struct {
Subject string
Message error
}
type Natty struct {
*Config
nc *nats.Conn
js nats.JetStreamContext
consumerLooper director.Looper
kvMap *KeyValueMap
kvMutex *sync.RWMutex
publisherMutex *sync.RWMutex
publisherMap map[string]*Publisher
// key == asLeaderKey(bucket, key)
// value == node name
leaderMap map[string]string
leaderMutex *sync.RWMutex
log Logger
}
func New(cfg *Config) (*Natty, error) {
if err := validateConfig(cfg); err != nil {
return nil, errors.Wrap(err, "invalid config")
}
var connected bool
var nc *nats.Conn
var err error
var tlsConfig *tls.Config
if cfg.UseTLS {
tlsConfig, err = GenerateTLSConfig(cfg.TLSCACertFile, cfg.TLSClientCertFile, cfg.TLSClientKeyFile, cfg.TLSSkipVerify)
if err != nil {
return nil, errors.Wrap(err, "failed to create TLS config")
}
}
// Attempt to connect
for _, address := range cfg.NatsURL {
if cfg.UseTLS {
nc, err = nats.Connect(address, nats.Secure(tlsConfig))
} else {
nc, err = nats.Connect(address)
}
if err != nil {
fmt.Printf("unable to connect to '%s': %s\n", address, err)
continue
}
connected = true
break
}
if !connected {
return nil, errors.Wrap(err, "failed to connect to NATS")
}
// Create js context
js, err := nc.JetStream()
if err != nil {
return nil, errors.Wrap(err, "failed to create jetstream context")
}
n := &Natty{
nc: nc,
js: js,
Config: cfg,
kvMap: &KeyValueMap{
rwMutex: &sync.RWMutex{},
kvMap: make(map[string]nats.KeyValue),
},
publisherMutex: &sync.RWMutex{},
publisherMap: make(map[string]*Publisher),
leaderMap: make(map[string]string),
leaderMutex: &sync.RWMutex{},
}
// Inject logger (if provided)
n.log = cfg.Logger
if n.log == nil {
n.log = &NoOpLogger{}
}
return n, nil
}
func (n *Natty) DeleteStream(ctx context.Context, name string) error {
span, _ := tracer.StartSpanFromContext(ctx, "natty.DeleteStream")
defer span.Finish()
if err := n.js.DeleteStream(name); err != nil {
err = errors.Wrap(err, "unable to delete stream")
span.SetTag("error", err)
return err
}
return nil
}
func (n *Natty) CreateStream(ctx context.Context, name string, subjects []string) error {
span, _ := tracer.StartSpanFromContext(ctx, "natty.CreateStream")
defer span.Finish()
// Check if stream exists
_, err := n.js.StreamInfo(name)
if err == nil {
// We have a stream already, nothing else to do
return nil
} else if !errors.Is(err, nats.ErrStreamNotFound) {
err = errors.Wrap(err, "unable to create stream")
span.SetTag("error", err)
return err
}
_, err = n.js.AddStream(&nats.StreamConfig{
Name: name,
Subjects: subjects,
Retention: nats.LimitsPolicy, // Limit to age
MaxAge: time.Hour * 24 * 30, // 30 days max retention
Storage: nats.FileStorage, // Store on disk
})
if err != nil {
err = errors.Wrap(err, "unable to create stream")
span.SetTag("error", err)
return err
}
return nil
}
func GenerateTLSConfig(caCertFile, clientKeyFile, clientCertFile string, tlsSkipVerify bool) (*tls.Config, error) {
if caCertFile == "" && clientKeyFile == "" && clientCertFile == "" {
return &tls.Config{
InsecureSkipVerify: tlsSkipVerify,
}, nil
}
var certpool *x509.CertPool
if caCertFile != "" {
certpool = x509.NewCertPool()
pemCerts, err := ioutil.ReadFile(caCertFile)
if err == nil {
certpool.AppendCertsFromPEM(pemCerts)
}
}
var cert tls.Certificate
if clientKeyFile != "" && clientCertFile != "" {
var err error
// Import client certificate/key pair
cert, err = tls.LoadX509KeyPair(clientCertFile, clientKeyFile)
if err != nil {
return nil, errors.Wrap(err, "unable to load ssl keypair")
}
// Just to print out the client certificate..
cert.Leaf, err = x509.ParseCertificate(cert.Certificate[0])
if err != nil {
return nil, errors.Wrap(err, "unable to parse certificate")
}
}
// Create tls.Config with desired tls properties
return &tls.Config{
RootCAs: certpool,
ClientAuth: tls.NoClientCert,
ClientCAs: nil,
InsecureSkipVerify: tlsSkipVerify,
Certificates: []tls.Certificate{cert},
}, nil
}
func (n *Natty) CreateConsumer(ctx context.Context, streamName, consumerName string, filterSubject ...string) error {
span, _ := tracer.StartSpanFromContext(ctx, "natty.CreateConsumer")
defer span.Finish()
var filter string
if len(filterSubject) > 0 {
filter = filterSubject[0]
}
if _, err := n.js.AddConsumer(streamName, &nats.ConsumerConfig{
Durable: consumerName,
AckPolicy: nats.AckExplicitPolicy,
FilterSubject: filter,
}); err != nil {
err = errors.Wrap(err, "unable to create consumer")
span.SetTag("error", err)
return err
}
return nil
}
func (n *Natty) DeleteConsumer(ctx context.Context, consumerName, streamName string) error {
span, _ := tracer.StartSpanFromContext(ctx, "natty.CreateConsumer")
defer span.Finish()
if err := n.js.DeleteConsumer(streamName, consumerName); err != nil {
err = errors.Wrap(err, "unable to delete consumer")
span.SetTag("error", err)
return err
}
return nil
}
// Consume will create a durable consumer and consume messages from the configured stream
func (n *Natty) Consume(ctx context.Context, cfg *ConsumerConfig, f func(ctx context.Context, msg *nats.Msg) error) error {
if err := validateConsumerConfig(cfg); err != nil {
return errors.Wrap(err, "invalid consumer config")
}
sub, err := n.js.PullSubscribe(cfg.Subject, cfg.ConsumerName)
if err != nil {
return errors.Wrap(err, "unable to create subscription")
}
defer func() {
if err := sub.Unsubscribe(); err != nil {
n.log.Errorf("unable to unsubscribe from (stream: '%s', subj: '%s'): %s",
cfg.StreamName, cfg.Subject, err)
}
}()
var quit bool
cfg.Looper.Loop(func() error {
// This is needed to prevent context flood in case .Quit() wasn't picked
// up quickly enough by director
if quit {
time.Sleep(25 * time.Millisecond)
return nil
}
msgs, err := sub.Fetch(n.FetchSize, nats.Context(ctx))
if err != nil {
if err == context.Canceled {
n.log.Debugf("context canceled (stream: %s, subj: %s)",
cfg.StreamName, cfg.Subject)
cfg.Looper.Quit()
quit = true
return nil
}
if err == context.DeadlineExceeded {
// No problem, context timed out - try again
return nil
}
n.report(cfg.ErrorCh, fmt.Errorf("unable to fetch messages from (stream: '%s', subj: '%s'): %s",
cfg.StreamName, cfg.Subject, err))
return nil
}
for _, m := range msgs {
if err := f(ctx, m); err != nil {
n.report(cfg.ErrorCh, fmt.Errorf("callback func failed during message processing (stream: '%s', subj: '%s', msg: '%s'): %s",
cfg.StreamName, cfg.Subject, m.Data, err))
}
}
return nil
})
n.log.Debugf("consumer exiting (stream: %s, subj: %s)", cfg.StreamName, cfg.Subject)
return nil
}
func (n *Natty) report(errorCh chan error, err error) {
n.log.Error(err)
if errorCh != nil {
// Write the err in a goroutine to avoid block in case chan is full
go func() {
select {
case errorCh <- err:
default:
n.log.Warnf("consumer error channel is full; discarding error")
}
}()
}
n.log.Error(err)
}
func validateConfig(cfg *Config) error {
if cfg == nil {
return errors.New("config cannot be nil")
}
if len(cfg.NatsURL) == 0 {
return errors.New("NatsURL cannot be empty")
}
if cfg.MaxMsgs == 0 {
cfg.MaxMsgs = DefaultMaxMsgs
}
if cfg.FetchSize == 0 {
cfg.FetchSize = DefaultFetchSize
}
if cfg.FetchTimeout == 0 {
cfg.FetchTimeout = DefaultFetchTimeout
}
if cfg.DeliverPolicy == 0 {
cfg.DeliverPolicy = DefaultDeliverPolicy
}
if cfg.PublishBatchSize == 0 {
cfg.PublishBatchSize = DefaultSubBatchSize
}
if cfg.WorkerIdleTimeout == 0 {
cfg.WorkerIdleTimeout = DefaultWorkerIdleTimeout
}
if cfg.PublishTimeout == 0 {
cfg.PublishTimeout = DefaultPublishTimeout
}
if cfg.ServiceShutdownContext == nil {
ctx, _ := context.WithCancel(context.Background())
cfg.ServiceShutdownContext = ctx
}
return nil
}
func validateConsumerConfig(cfg *ConsumerConfig) error {
if cfg.StreamName == "" {
return ErrEmptyStreamName
}
if cfg.ConsumerName == "" {
return ErrEmptyConsumerName
}
if cfg.Subject == "" {
return ErrEmptySubject
}
if cfg.Looper == nil {
cfg.Looper = director.NewFreeLooper(director.FOREVER, cfg.ErrorCh)
}
return nil
}
func MustNewUUID() string {
tmpUUID, err := uuid.NewV4()
if err != nil {
panic("unable to generate uuid: " + err.Error())
}
return tmpUUID.String()
}