-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnatsrouter.go
455 lines (397 loc) · 12.4 KB
/
natsrouter.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
/*
This is a Go package called `natsrouter`, which provides middleware-supported
NATS message routing. It defines `NatsRouter` struct that holds the NATS
connection and an array of middleware functions. New function creates a new
instance of `NatsRouter`, `WithMiddleware` adds middleware functions to the
router, and `Subscribe` and `QueueSubscribe` register handlers for NATS
messages. `Queue` and `Subject` structs provide additional routing capabilities
by allowing to group subscriptions and create more specific subscription
subjects. `getSubject` method of `Subject` is used to create the final
subscription subject by joining the subjects with dots and verifying that ">"
is the last in the chain.
*/
package natsrouter
import (
"bytes"
"compress/flate"
"compress/gzip"
"context"
"encoding/json"
"errors"
"io"
"strings"
"sync"
"github.com/nats-io/nats.go"
)
var ErrUnsupportedEncoding = errors.New("unsupported encoding")
// Handler function that adds a `context.Context` to a `*nats.Msg`
type NatsCtxHandler func(*NatsMsg) error
// Middleware function that takes a `NatsCtxHandler` and returns a new `NatsCtxHandler`
type NatsMiddlewareFunc func(NatsCtxHandler) NatsCtxHandler
// NatsRouter is a middleware-supported NATS router that provides a fluent API
// for subscribing to subjects and chaining middleware functions.
type NatsRouter struct {
nc *nats.Conn
mw []NatsMiddlewareFunc
options RouterOptions
quit chan struct{}
chanWg sync.WaitGroup
closed chan struct{}
}
// Defines a struct for the router options, which currently contains error
// config, default request id tag (for error reporting) and optional list of
// NATS connection options.
type RouterOptions struct {
ErrorConfig *ErrorConfig
RequestIdTag string
NatsOptions nats.Options
}
// Defines a function type that will be used to define options for the router.
type RouterOption func(*RouterOptions)
// Define error config in the router options.
func WithErrorConfig(ec *ErrorConfig) RouterOption {
return func(o *RouterOptions) {
o.ErrorConfig = ec
}
}
// Define error config as strings in the router options.
func WithErrorConfigString(tag, format string) RouterOption {
return func(o *RouterOptions) {
o.ErrorConfig = &ErrorConfig{
Tag: tag,
Format: format,
}
}
}
// Define new request id header tag
func WithRequestIdTag(tag string) RouterOption {
return func(o *RouterOptions) {
o.RequestIdTag = tag
}
}
// Set nats.Options for the connection before connecting
func WithNatsOptions(nopts nats.Options) RouterOption {
return func(o *RouterOptions) {
o.NatsOptions = nopts
}
}
// Apply one or more nats.Option to the config before connecting
func WithNatsOption(options... nats.Option) RouterOption {
return func(o *RouterOptions) {
for _, opt := range options {
if opt != nil {
opt(&o.NatsOptions)
}
}
}
}
// Get default RouterOptions
func GetDefaultRouterOptions() RouterOptions {
return RouterOptions{
&ErrorConfig{"error", "json"},
"request_id",
nats.GetDefaultOptions(),
}
}
// Create a new NatsRouter with a *nats.Conn and an optional list of
// RouterOptions functions. It sets the default RouterOptions to use a default
// ErrorConfig, and then iterates through each option function, calling it with
// the RouterOptions struct pointer to set any additional options.
//
// Deprecated: Use Connect instead. This does not support properly draining
// publications and subscriptions.
func NewRouter(nc *nats.Conn, options ...RouterOption) *NatsRouter {
router := &NatsRouter{
nc: nc,
options: RouterOptions{
&ErrorConfig{"error", "json"},
"request_id",
nats.GetDefaultOptions(),
},
quit: make(chan struct{}),
closed: make(chan struct{}),
}
for _, opt := range options {
opt(&router.options)
}
return router
}
// Creates a new NatsRouter with a string address and an optional list of
// RouterOptions functions. It connects to the NATS server using the provided
// address, and then calls NewRouter to create a new NatsRouter with the
// resulting *nats.Conn and optional RouterOptions. If there was an error
// connecting to the server, it returns nil and the error.
//
// Deprecated: Use Connect instead. This does not support properly draining
// publications and subscriptions.
func NewRouterWithAddress(addr string, options ...RouterOption) (*NatsRouter, error) {
nc, err := nats.Connect(addr)
if err != nil {
return nil, err
}
return NewRouter(nc, options...), nil
}
// Process the url string argument to Connect.
// Return an array of urls, even if only one.
func processUrlString(url string) []string {
urls := strings.Split(url, ",")
var j int
for _, s := range urls {
u := strings.TrimSpace(s)
if len(u) > 0 {
urls[j] = u
j++
}
}
return urls[:j]
}
func Connect(url string, options ...RouterOption) (*NatsRouter, error) {
opts := GetDefaultRouterOptions()
opts.NatsOptions.Servers = processUrlString(url)
for _, opt := range options {
if opt != nil {
opt(&opts)
}
}
return opts.Connect()
}
// Connect will attempt to connect to a NATS server with multiple options.
func (r RouterOptions) Connect() (*NatsRouter, error) {
// Check options, set defaults if necessary
if r.ErrorConfig == nil {
r.ErrorConfig = &ErrorConfig{"error", "json"}
}
if r.RequestIdTag == "" {
r.RequestIdTag = "request_id"
}
// Create router instance
router := &NatsRouter{
options: r,
quit: make(chan struct{}),
closed: make(chan struct{}),
}
// Set custom closed callback
if r.NatsOptions.ClosedCB != nil {
// Preserve original CB as well (via a closure)
r.NatsOptions.ClosedCB = func(orig nats.ConnHandler) nats.ConnHandler {
original := orig
return func(nc *nats.Conn) {
// First notify own channel
close(router.closed)
// And then call the original handler
original(nc)
}
}(r.NatsOptions.ClosedCB)
} else {
r.NatsOptions.ClosedCB = func(_ *nats.Conn) {
close(router.closed)
}
}
// Perform actual connection
nc, err := r.NatsOptions.Connect()
if err != nil {
return nil, err
}
router.nc = nc
return router, nil
}
// Drain pubs/subs and close connection to NATS server
func (n *NatsRouter) Drain() {
// First close any channel-based subscriptions
close(n.quit)
n.chanWg.Wait()
// Then start draining the connection
n.nc.Drain()
// Wait until it is done
<-n.closed
}
// Close connection to NATS server
func (n *NatsRouter) Close() {
close(n.quit)
n.chanWg.Wait()
n.nc.Close()
}
// Get current underlying NATS connection
func (n *NatsRouter) Conn() *nats.Conn {
return n.nc
}
// Returns a new `NatsRouter` with additional middleware functions
func (n *NatsRouter) WithMiddleware(fns ...NatsMiddlewareFunc) *NatsRouter {
return &NatsRouter{
nc: n.nc,
mw: append(n.mw, fns...),
options: n.options,
quit: make(chan struct{}),
}
}
// Alias for `WithMiddleware`
func (n *NatsRouter) Use(fns ...NatsMiddlewareFunc) *NatsRouter {
return n.WithMiddleware(fns...)
}
// Subscribe registers a handler function for the specified subject and returns
// a *nats.Subscription. The handler function is wrapped with any registered
// middleware functions in reverse order.
func (n *NatsRouter) Subscribe(subject string, handler NatsCtxHandler) (*nats.Subscription, error) {
return n.nc.Subscribe(subject, n.msgHandler(handler))
}
// QueueSubscribe registers a handler function for the specified subject and
// queue group and returns a *nats.Subscription. The handler function is wrapped
// with any registered middleware functions in reverse order.
func (n *NatsRouter) QueueSubscribe(subject, queue string, handler NatsCtxHandler) (*nats.Subscription, error) {
return n.nc.QueueSubscribe(subject, queue, n.msgHandler(handler))
}
// Read possibly-compressed content
func readCompressedData(encoding string, data []byte) ([]byte, error) {
if encoding == "gzip" {
zr, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
return nil, err
}
defer zr.Close()
return io.ReadAll(zr)
} else if encoding == "deflate" {
fr := flate.NewReader(bytes.NewReader(data))
defer fr.Close()
return io.ReadAll(fr)
} else if len(encoding) > 0 {
return nil, ErrUnsupportedEncoding
}
return data, nil
}
// Handler that wraps function call with any registered middleware functions in
// reverse order. On any error, an error message is automatically sent as a
// response to the request.
func (n *NatsRouter) msgHandler(handler NatsCtxHandler) func(*nats.Msg) {
return func(msg *nats.Msg) {
var err error
data, err := readCompressedData(msg.Header.Get("encoding"), msg.Data)
if err == nil {
// Replace data with decompressed data
msg.Data = data
natsMsg := &NatsMsg{
msg,
context.Background(),
}
var wrappedHandler NatsCtxHandler = handler
for i := len(n.mw) - 1; i >= 0; i-- {
wrappedHandler = n.mw[i](wrappedHandler)
}
err = wrappedHandler(natsMsg)
}
if err != nil {
handlerErr, ok := err.(*HandlerError)
if !ok {
handlerErr = &HandlerError{
Message: err.Error(),
Code: 500,
}
}
errData, _ := json.Marshal(handlerErr)
reply := nats.NewMsg(msg.Reply)
if len(n.options.RequestIdTag) > 0 {
if reqId, ok := msg.Header[n.options.RequestIdTag]; ok {
reply.Header.Add(n.options.RequestIdTag, reqId[0])
}
}
reply.Header.Add(n.options.ErrorConfig.Tag, n.options.ErrorConfig.Format)
reply.Data = errData
msg.RespondMsg(reply)
}
}
}
// Same as Subscribe, except uses channels. Note that error handling is
// available only for middleware, since the message is processed first by
// middleware and then inserted into the *NatsMsg channel.
func (n *NatsRouter) ChanSubscribe(subject string, ch chan *NatsMsg) (*nats.Subscription, error) {
intCh := make(chan *nats.Msg, 64)
n.chanWg.Add(1)
go n.chanMsgHandler(ch, intCh)
return n.nc.ChanSubscribe(subject, intCh)
}
// Same as QueueSubscribe, except uses channels. Note that error handling is
// available only for middleware, since the message is processed first by
// middleware and then inserted into the *NatsMsg channel.
func (n *NatsRouter) ChanQueueSubscribe(subject, queue string, ch chan *NatsMsg) (*nats.Subscription, error) {
intCh := make(chan *nats.Msg, 64)
n.chanWg.Add(1)
go n.chanMsgHandler(ch, intCh)
return n.nc.ChanQueueSubscribe(subject, queue, intCh)
}
// Handler that wraps function call with any registered middleware functions in
// reverse order. On any error, an error message is automatically sent as a
// response to the request.
func (n *NatsRouter) chanMsgHandler(ch chan *NatsMsg, intCh chan *nats.Msg) {
defer n.chanWg.Done()
handler := func(natsMsg *NatsMsg) error {
ch <- natsMsg
return nil
}
chanLoop:
for {
select {
case msg := <-intCh:
var err error
data, err := readCompressedData(msg.Header.Get("encoding"), msg.Data)
if err == nil {
// Replace data with decompressed data
msg.Data = data
natsMsg := &NatsMsg{
msg,
context.Background(),
}
var wrappedHandler NatsCtxHandler = handler
for i := len(n.mw) - 1; i >= 0; i-- {
wrappedHandler = n.mw[i](wrappedHandler)
}
// Errors are only handled for the middleware
err = wrappedHandler(natsMsg)
}
if err != nil {
handlerErr, ok := err.(*HandlerError)
if !ok {
handlerErr = &HandlerError{
Message: err.Error(),
Code: 500,
}
}
errData, _ := json.Marshal(handlerErr)
reply := nats.NewMsg(msg.Reply)
if len(n.options.RequestIdTag) > 0 {
if reqId, ok := msg.Header[n.options.RequestIdTag]; ok {
reply.Header.Add(n.options.RequestIdTag, reqId[0])
}
}
reply.Header.Add(n.options.ErrorConfig.Tag, n.options.ErrorConfig.Format)
reply.Data = errData
msg.RespondMsg(reply)
}
case <-n.quit:
break chanLoop
}
}
}
// Publish is a passthrough function for the `nats` Publish function
func (n *NatsRouter) Publish(subject string, data []byte) error {
return n.nc.Publish(subject, data)
}
// Returns a new `Queue` object with the given queue name
func (n *NatsRouter) Queue(queue string) *Queue {
return &Queue{
n: n,
queue: queue,
}
}
// Returns a new `Subject` object with the given subject name(s)
func (n *NatsRouter) Subject(subjects ...string) *Subject {
return &Subject{
n: n,
subjects: subjects,
}
}
// Returns a new `Subject` object with the wildcard ALL_SUBJECT. This is also knows as a "wiretap" mode, listening on all requests.
func (n *NatsRouter) Wiretap() *Subject {
return &Subject{
n: n,
subjects: []string{ALL_SUBJECT},
}
}