forked from ThreeDotsLabs/watermill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
826 lines (668 loc) · 22.3 KB
/
router.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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
package message
import (
"context"
"fmt"
"runtime/debug"
"sync"
"time"
"github.com/pkg/errors"
"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/internal"
sync_internal "github.com/ThreeDotsLabs/watermill/pubsub/sync"
)
var (
// ErrOutputInNoPublisherHandler happens when a handler func returned some messages in a no-publisher handler.
// todo: maybe change the handler func signature in no-publisher handler so that there's no possibility for this
ErrOutputInNoPublisherHandler = errors.New("returned output messages in a handler without publisher")
)
// HandlerFunc is function called when message is received.
//
// msg.Ack() is called automatically when HandlerFunc doesn't return error.
// When HandlerFunc returns error, msg.Nack() is called.
// When msg.Ack() was called in handler and HandlerFunc returns error,
// msg.Nack() will be not sent because Ack was already sent.
//
// HandlerFunc's are executed parallel when multiple messages was received
// (because msg.Ack() was sent in HandlerFunc or Subscriber supports multiple consumers).
type HandlerFunc func(msg *Message) ([]*Message, error)
// NoPublishHandlerFunc is HandlerFunc alternative, which doesn't produce any messages.
type NoPublishHandlerFunc func(msg *Message) error
// PassthroughHandler is a handler that passes the message unchanged from the subscriber to the publisher.
var PassthroughHandler HandlerFunc = func(msg *Message) ([]*Message, error) {
return []*Message{msg}, nil
}
// HandlerMiddleware allows us to write something like decorators to HandlerFunc.
// It can execute something before handler (for example: modify consumed message)
// or after (modify produced messages, ack/nack on consumed message, handle errors, logging, etc.).
//
// It can be attached to the router by using `AddMiddleware` method.
//
// Example:
//
// func ExampleMiddleware(h message.HandlerFunc) message.HandlerFunc {
// return func(message *message.Message) ([]*message.Message, error) {
// fmt.Println("executed before handler")
// producedMessages, err := h(message)
// fmt.Println("executed after handler")
//
// return producedMessages, err
// }
// }
type HandlerMiddleware func(h HandlerFunc) HandlerFunc
// RouterPlugin is function which is executed on Router start.
type RouterPlugin func(*Router) error
// PublisherDecorator wraps the underlying Publisher, adding some functionality.
type PublisherDecorator func(pub Publisher) (Publisher, error)
// SubscriberDecorator wraps the underlying Subscriber, adding some functionality.
type SubscriberDecorator func(sub Subscriber) (Subscriber, error)
// RouterConfig holds the Router's configuration options.
type RouterConfig struct {
// CloseTimeout determines how long router should work for handlers when closing.
CloseTimeout time.Duration
}
func (c *RouterConfig) setDefaults() {
if c.CloseTimeout == 0 {
c.CloseTimeout = time.Second * 30
}
}
// Validate returns Router configuration error, if any.
func (c RouterConfig) Validate() error {
return nil
}
// NewRouter creates a new Router with given configuration.
func NewRouter(config RouterConfig, logger watermill.LoggerAdapter) (*Router, error) {
config.setDefaults()
if err := config.Validate(); err != nil {
return nil, errors.Wrap(err, "invalid config")
}
return &Router{
config: config,
handlers: map[string]*handler{},
handlersWg: &sync.WaitGroup{},
runningHandlersWg: &sync.WaitGroup{},
runningHandlersWgLock: &sync.Mutex{},
handlerAdded: make(chan struct{}),
handlersLock: &sync.RWMutex{},
closingInProgressCh: make(chan struct{}),
closedCh: make(chan struct{}),
logger: logger,
running: make(chan struct{}),
}, nil
}
type middleware struct {
Handler HandlerMiddleware
HandlerName string
IsRouterLevel bool
}
// Router is responsible for handling messages from subscribers using provided handler functions.
//
// If the handler function returns a message, the message is published with the publisher.
// You can use middlewares to wrap handlers with common logic like logging, instrumentation, etc.
type Router struct {
config RouterConfig
middlewares []middleware
plugins []RouterPlugin
handlers map[string]*handler
handlersLock *sync.RWMutex
handlersWg *sync.WaitGroup
runningHandlersWg *sync.WaitGroup
runningHandlersWgLock *sync.Mutex
handlerAdded chan struct{}
closingInProgressCh chan struct{}
closedCh chan struct{}
closed bool
closedLock sync.Mutex
logger watermill.LoggerAdapter
publisherDecorators []PublisherDecorator
subscriberDecorators []SubscriberDecorator
isRunning bool
running chan struct{}
}
// Logger returns the Router's logger.
func (r *Router) Logger() watermill.LoggerAdapter {
return r.logger
}
// AddMiddleware adds a new middleware to the router.
//
// The order of middleware matters. Middleware added at the beginning is executed first.
func (r *Router) AddMiddleware(m ...HandlerMiddleware) {
r.logger.Debug("Adding middleware", watermill.LogFields{"count": fmt.Sprintf("%d", len(m))})
r.addRouterLevelMiddleware(m...)
}
func (r *Router) addRouterLevelMiddleware(m ...HandlerMiddleware) {
for _, handlerMiddleware := range m {
middleware := middleware{
Handler: handlerMiddleware,
HandlerName: "",
IsRouterLevel: true,
}
r.middlewares = append(r.middlewares, middleware)
}
}
func (r *Router) addHandlerLevelMiddleware(handlerName string, m ...HandlerMiddleware) {
for _, handlerMiddleware := range m {
middleware := middleware{
Handler: handlerMiddleware,
HandlerName: handlerName,
IsRouterLevel: false,
}
r.middlewares = append(r.middlewares, middleware)
}
}
// AddPlugin adds a new plugin to the router.
// Plugins are executed during startup of the router.
//
// A plugin can, for example, close the router after SIGINT or SIGTERM is sent to the process (SignalsHandler plugin).
func (r *Router) AddPlugin(p ...RouterPlugin) {
r.logger.Debug("Adding plugins", watermill.LogFields{"count": fmt.Sprintf("%d", len(p))})
r.plugins = append(r.plugins, p...)
}
// AddPublisherDecorators wraps the router's Publisher.
// The first decorator is the innermost, i.e. calls the original publisher.
func (r *Router) AddPublisherDecorators(dec ...PublisherDecorator) {
r.logger.Debug("Adding publisher decorators", watermill.LogFields{"count": fmt.Sprintf("%d", len(dec))})
r.publisherDecorators = append(r.publisherDecorators, dec...)
}
// AddSubscriberDecorators wraps the router's Subscriber.
// The first decorator is the innermost, i.e. calls the original subscriber.
func (r *Router) AddSubscriberDecorators(dec ...SubscriberDecorator) {
r.logger.Debug("Adding subscriber decorators", watermill.LogFields{"count": fmt.Sprintf("%d", len(dec))})
r.subscriberDecorators = append(r.subscriberDecorators, dec...)
}
// Handlers returns all registered handlers.
func (r *Router) Handlers() map[string]HandlerFunc {
handlers := map[string]HandlerFunc{}
for handlerName, handler := range r.handlers {
handlers[handlerName] = handler.handlerFunc
}
return handlers
}
// DuplicateHandlerNameError is sent in a panic when you try to add a second handler with the same name.
type DuplicateHandlerNameError struct {
HandlerName string
}
func (d DuplicateHandlerNameError) Error() string {
return fmt.Sprintf("handler with name %s already exists", d.HandlerName)
}
// AddHandler adds a new handler.
//
// handlerName must be unique. For now, it is used only for debugging.
//
// subscribeTopic is a topic from which handler will receive messages.
//
// publishTopic is a topic to which router will produce messages returned by handlerFunc.
// When handler needs to publish to multiple topics,
// it is recommended to just inject Publisher to Handler or implement middleware
// which will catch messages and publish to topic based on metadata for example.
//
// If handler is added while router is already running, you need to explicitly call RunHandlers().
func (r *Router) AddHandler(
handlerName string,
subscribeTopic string,
subscriber Subscriber,
publishTopic string,
publisher Publisher,
handlerFunc HandlerFunc,
) *Handler {
r.logger.Info("Adding handler", watermill.LogFields{
"handler_name": handlerName,
"topic": subscribeTopic,
})
r.handlersLock.Lock()
defer r.handlersLock.Unlock()
if _, ok := r.handlers[handlerName]; ok {
panic(DuplicateHandlerNameError{handlerName})
}
publisherName, subscriberName := internal.StructName(publisher), internal.StructName(subscriber)
newHandler := &handler{
name: handlerName,
logger: r.logger,
subscriber: subscriber,
subscribeTopic: subscribeTopic,
subscriberName: subscriberName,
publisher: publisher,
publishTopic: publishTopic,
publisherName: publisherName,
handlerFunc: handlerFunc,
runningHandlersWg: r.runningHandlersWg,
runningHandlersWgLock: r.runningHandlersWgLock,
messagesCh: nil,
routersCloseCh: r.closingInProgressCh,
startedCh: make(chan struct{}),
}
r.handlersWg.Add(1)
r.handlers[handlerName] = newHandler
select {
case r.handlerAdded <- struct{}{}:
default:
// closeWhenAllHandlersStopped is not always waiting for handlerAdded
}
return &Handler{
router: r,
handler: newHandler,
}
}
// AddNoPublisherHandler adds a new handler.
// This handler cannot return messages.
// When message is returned it will occur an error and Nack will be sent.
//
// handlerName must be unique. For now, it is used only for debugging.
//
// subscribeTopic is a topic from which handler will receive messages.
//
// subscriber is Subscriber from which messages will be consumed.
//
// If handler is added while router is already running, you need to explicitly call RunHandlers().
func (r *Router) AddNoPublisherHandler(
handlerName string,
subscribeTopic string,
subscriber Subscriber,
handlerFunc NoPublishHandlerFunc,
) *Handler {
handlerFuncAdapter := func(msg *Message) ([]*Message, error) {
return nil, handlerFunc(msg)
}
return r.AddHandler(handlerName, subscribeTopic, subscriber, "", disabledPublisher{}, handlerFuncAdapter)
}
// Run runs all plugins and handlers and starts subscribing to provided topics.
// This call is blocking while the router is running.
//
// When all handlers have stopped (for example, because subscriptions were closed), the router will also stop.
//
// To stop Run() you should call Close() on the router.
//
// ctx will be propagated to all subscribers.
//
// When all handlers are stopped (for example: because of closed connection), Run() will be also stopped.
func (r *Router) Run(ctx context.Context) (err error) {
if r.isRunning {
return errors.New("router is already running")
}
r.isRunning = true
ctx, cancel := context.WithCancel(ctx)
defer cancel()
r.logger.Debug("Loading plugins", nil)
for _, plugin := range r.plugins {
if err := plugin(r); err != nil {
return errors.Wrapf(err, "cannot initialize plugin %v", plugin)
}
}
if err := r.RunHandlers(ctx); err != nil {
return err
}
close(r.running)
go r.closeWhenAllHandlersStopped(ctx)
<-r.closingInProgressCh
cancel()
r.logger.Info("Waiting for messages", watermill.LogFields{
"timeout": r.config.CloseTimeout,
})
<-r.closedCh
r.logger.Info("All messages processed", nil)
return nil
}
// RunHandlers runs all handlers that were added after Run().
// RunHandlers is idempotent, so can be called multiple times safely.
func (r *Router) RunHandlers(ctx context.Context) error {
if !r.isRunning {
return errors.New("you can't call RunHandlers on non-running router")
}
r.handlersLock.Lock()
defer r.handlersLock.Unlock()
r.logger.Info("Running router handlers", watermill.LogFields{"count": len(r.handlers)})
for name, h := range r.handlers {
name := name
h := h
if h.started {
continue
}
if err := r.decorateHandlerPublisher(h); err != nil {
return errors.Wrapf(err, "could not decorate publisher of handler %s", name)
}
if err := r.decorateHandlerSubscriber(h); err != nil {
return errors.Wrapf(err, "could not decorate subscriber of handler %s", name)
}
r.logger.Debug("Subscribing to topic", watermill.LogFields{
"subscriber_name": h.name,
"topic": h.subscribeTopic,
})
ctx, cancel := context.WithCancel(ctx)
messages, err := h.subscriber.Subscribe(ctx, h.subscribeTopic)
if err != nil {
cancel()
return errors.Wrapf(err, "cannot subscribe topic %s", h.subscribeTopic)
}
h.messagesCh = messages
h.started = true
close(h.startedCh)
h.stopFn = cancel
h.stopped = make(chan struct{})
go func() {
defer cancel()
h.run(ctx, r.middlewares)
r.handlersWg.Done()
r.logger.Info("Subscriber stopped", watermill.LogFields{
"subscriber_name": h.name,
"topic": h.subscribeTopic,
})
r.handlersLock.Lock()
delete(r.handlers, name)
r.handlersLock.Unlock()
}()
}
return nil
}
// closeWhenAllHandlersStopped closed router, when all handlers has stopped,
// because for example all subscriptions are closed.
func (r *Router) closeWhenAllHandlersStopped(ctx context.Context) {
r.handlersLock.RLock()
hasHandlers := len(r.handlers) == 0
r.handlersLock.RUnlock()
if hasHandlers {
// in that situation router will be closed immediately (even if they are no routers)
// let's wait for
select {
case <-r.handlerAdded:
// it should be some handler to track
case <-r.closedCh:
// let's avoid goroutine leak
return
}
}
r.handlersWg.Wait()
if r.IsClosed() {
// already closed
return
}
// Only log an error if the context was not canceled, but handlers were stopped.
select {
case <-ctx.Done():
default:
r.logger.Error("All handlers stopped, closing router", errors.New("all router handlers stopped"), nil)
}
if err := r.Close(); err != nil {
r.logger.Error("Cannot close router", err, nil)
}
}
// Running is closed when router is running.
// In other words: you can wait till router is running using
//
// fmt.Println("Starting router")
// go r.Run(ctx)
// <- r.Running()
// fmt.Println("Router is running")
//
// Warning: for historical reasons, this channel is not aware of router closing - the channel will be closed if the router has been running and closed.
func (r *Router) Running() chan struct{} {
return r.running
}
// IsRunning returns true when router is running.
//
// Warning: for historical reasons, this method is not aware of router closing.
// If you want to know if the router was closed, use IsClosed.
func (r *Router) IsRunning() bool {
select {
case <-r.running:
return true
default:
return false
}
}
// Close gracefully closes the router with a timeout provided in the configuration.
func (r *Router) Close() error {
r.closedLock.Lock()
defer r.closedLock.Unlock()
r.handlersLock.Lock()
defer r.handlersLock.Unlock()
if r.closed {
return nil
}
r.closed = true
r.logger.Info("Closing router", nil)
defer r.logger.Info("Router closed", nil)
close(r.closingInProgressCh)
defer close(r.closedCh)
timeouted := r.waitForHandlers()
if timeouted {
return errors.New("router close timeout")
}
return nil
}
func (r *Router) waitForHandlers() bool {
var waitGroup sync.WaitGroup
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
r.handlersWg.Wait()
}()
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
r.runningHandlersWgLock.Lock()
defer r.runningHandlersWgLock.Unlock()
r.runningHandlersWg.Wait()
}()
return sync_internal.WaitGroupTimeout(&waitGroup, r.config.CloseTimeout)
}
func (r *Router) IsClosed() bool {
r.closedLock.Lock()
defer r.closedLock.Unlock()
return r.closed
}
type handler struct {
name string
logger watermill.LoggerAdapter
subscriber Subscriber
subscribeTopic string
subscriberName string
publisher Publisher
publishTopic string
publisherName string
handlerFunc HandlerFunc
runningHandlersWg *sync.WaitGroup
runningHandlersWgLock *sync.Mutex
messagesCh <-chan *Message
started bool
startedCh chan struct{}
stopFn context.CancelFunc
stopped chan struct{}
routersCloseCh chan struct{}
}
func (h *handler) run(ctx context.Context, middlewares []middleware) {
h.logger.Info("Starting handler", watermill.LogFields{
"subscriber_name": h.name,
"topic": h.subscribeTopic,
})
middlewareHandler := h.handlerFunc
// first added middlewares should be executed first (so should be at the top of call stack)
for i := len(middlewares) - 1; i >= 0; i-- {
currentMiddleware := middlewares[i]
isValidHandlerLevelMiddleware := currentMiddleware.HandlerName == h.name
if currentMiddleware.IsRouterLevel || isValidHandlerLevelMiddleware {
middlewareHandler = currentMiddleware.Handler(middlewareHandler)
}
}
go h.handleClose(ctx)
for msg := range h.messagesCh {
h.runningHandlersWgLock.Lock()
h.runningHandlersWg.Add(1)
h.runningHandlersWgLock.Unlock()
go h.handleMessage(msg, middlewareHandler)
}
if h.publisher != nil {
h.logger.Debug("Waiting for publisher to close", nil)
if err := h.publisher.Close(); err != nil {
h.logger.Error("Failed to close publisher", err, nil)
}
h.logger.Debug("Publisher closed", nil)
}
h.logger.Debug("Router handler stopped", nil)
close(h.stopped)
}
// Handler handles Messages.
type Handler struct {
router *Router
handler *handler
}
// AddMiddleware adds new middleware to the specified handler in the router.
//
// The order of middleware matters. Middleware added at the beginning is executed first.
func (h *Handler) AddMiddleware(m ...HandlerMiddleware) {
handler := h.handler
handler.logger.Debug("Adding middleware to handler", watermill.LogFields{
"count": fmt.Sprintf("%d", len(m)),
"handlerName": handler.name,
})
h.router.addHandlerLevelMiddleware(handler.name, m...)
}
// Started returns channel which is stopped when handler is running.
func (h *Handler) Started() chan struct{} {
return h.handler.startedCh
}
// Stop stops the handler.
// Stop is asynchronous.
// You can check if handler was stopped with Stopped() function.
func (h *Handler) Stop() {
if !h.handler.started {
panic("handler is not started")
}
h.handler.stopFn()
}
// Stopped returns channel which is stopped when handler did stop.
func (h *Handler) Stopped() chan struct{} {
return h.handler.stopped
}
// decorateHandlerPublisher applies the decorator chain to handler's publisher.
// They are applied in reverse order, so that the later decorators use the result of former ones.
func (r *Router) decorateHandlerPublisher(h *handler) error {
var err error
pub := h.publisher
for i := len(r.publisherDecorators) - 1; i >= 0; i-- {
decorator := r.publisherDecorators[i]
pub, err = decorator(pub)
if err != nil {
return errors.Wrap(err, "could not apply publisher decorator")
}
}
r.handlers[h.name].publisher = pub
return nil
}
// decorateHandlerSubscriber applies the decorator chain to handler's subscriber.
// They are applied in regular order, so that the later decorators use the result of former ones.
func (r *Router) decorateHandlerSubscriber(h *handler) error {
var err error
sub := h.subscriber
// add values to message context to subscriber
// it goes before other decorators, so that they may take advantage of these values
messageTransform := func(msg *Message) {
if msg != nil {
h.addHandlerContext(msg)
}
}
sub, err = MessageTransformSubscriberDecorator(messageTransform)(sub)
if err != nil {
return errors.Wrapf(err, "cannot wrap subscriber with context decorator")
}
for _, decorator := range r.subscriberDecorators {
sub, err = decorator(sub)
if err != nil {
return errors.Wrap(err, "could not apply subscriber decorator")
}
}
r.handlers[h.name].subscriber = sub
return nil
}
// addHandlerContext enriches the contex with values that are relevant within this handler's context.
func (h *handler) addHandlerContext(messages ...*Message) {
for i, msg := range messages {
ctx := msg.Context()
if h.name != "" {
ctx = context.WithValue(ctx, handlerNameKey, h.name)
}
if h.publisherName != "" {
ctx = context.WithValue(ctx, publisherNameKey, h.publisherName)
}
if h.subscriberName != "" {
ctx = context.WithValue(ctx, subscriberNameKey, h.subscriberName)
}
if h.subscribeTopic != "" {
ctx = context.WithValue(ctx, subscribeTopicKey, h.subscribeTopic)
}
if h.publishTopic != "" {
ctx = context.WithValue(ctx, publishTopicKey, h.publishTopic)
}
messages[i].SetContext(ctx)
}
}
func (h *handler) handleClose(ctx context.Context) {
select {
case <-h.routersCloseCh:
// for backward compatibility we are closing subscriber
h.logger.Debug("Waiting for subscriber to close", nil)
if err := h.subscriber.Close(); err != nil {
h.logger.Error("Failed to close subscriber", err, nil)
}
h.logger.Debug("Subscriber closed", nil)
case <-ctx.Done():
// we are closing subscriber just when entire router is closed
}
h.stopFn()
}
func (h *handler) handleMessage(msg *Message, handler HandlerFunc) {
defer h.runningHandlersWg.Done()
msgFields := watermill.LogFields{"message_uuid": msg.UUID}
defer func() {
if recovered := recover(); recovered != nil {
h.logger.Error(
"Panic recovered in handler. Stack: "+string(debug.Stack()),
errors.Errorf("%s", recovered),
msgFields,
)
msg.Nack()
}
}()
h.logger.Trace("Received message", msgFields)
producedMessages, err := handler(msg)
if err != nil {
h.logger.Error("Handler returned error", err, nil)
msg.Nack()
return
}
h.addHandlerContext(producedMessages...)
if err := h.publishProducedMessages(producedMessages, msgFields); err != nil {
h.logger.Error("Publishing produced messages failed", err, nil)
msg.Nack()
return
}
msg.Ack()
h.logger.Trace("Message acked", msgFields)
}
func (h *handler) publishProducedMessages(producedMessages Messages, msgFields watermill.LogFields) error {
if len(producedMessages) == 0 {
return nil
}
if h.publisher == nil {
return ErrOutputInNoPublisherHandler
}
h.logger.Trace("Sending produced messages", msgFields.Add(watermill.LogFields{
"produced_messages_count": len(producedMessages),
"publish_topic": h.publishTopic,
}))
for _, msg := range producedMessages {
if err := h.publisher.Publish(h.publishTopic, msg); err != nil {
// todo - how to deal with it better/transactional/retry?
h.logger.Error("Cannot publish message", err, msgFields.Add(watermill.LogFields{
"not_sent_message": fmt.Sprintf("%#v", producedMessages),
}))
return err
}
}
return nil
}
type disabledPublisher struct{}
func (disabledPublisher) Publish(topic string, messages ...*Message) error {
return ErrOutputInNoPublisherHandler
}
func (disabledPublisher) Close() error {
return nil
}