-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
71 lines (61 loc) · 1.98 KB
/
handler.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
package pubsub
import (
"context"
"errors"
"fmt"
)
// ErrMissingHandler is fired when the dispatcher has not handler
// registered for the message name.
var ErrMissingHandler = errors.New("missing handler")
// Handler handles events.
type Handler interface {
HandleMessage(ctx context.Context, message *Message) error
}
// HandlerFunc that handles an event
type HandlerFunc func(ctx context.Context, message *Message) error
// HandleMessage handles the message using the function.
func (f HandlerFunc) HandleMessage(ctx context.Context, message *Message) error {
return f(ctx, message)
}
// Dispatcher is a message handler middleware that can be used to register
// different handlers for the same consumer, based on the message name.
func Dispatcher(handlers map[string]Handler) HandlerFunc {
return func(ctx context.Context, message *Message) error {
h, ok := handlers[message.Name]
if !ok {
return fmt.Errorf("%w: %s", ErrMissingHandler, message.Name)
}
return h.HandleMessage(ctx, message)
}
}
// Recoverer will prevent panics in the handler
func Recoverer(next Handler) Handler {
return HandlerFunc(func(ctx context.Context, message *Message) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic recovered: %v", r)
}
}()
err = next.HandleMessage(ctx, message)
return
})
}
// Acknowledge will acknowledge a message and pass it to the next handler.
func Acknowledge(next Handler) Handler {
return HandlerFunc(func(ctx context.Context, message *Message) (err error) {
if message != nil {
// Ack result is available downstream.
_ = message.Ack(ctx)
}
return next.HandleMessage(ctx, message)
})
}
// WrapHandler will wrap the handler in the given middlewares.
func WrapHandler(handler Handler, middlewares ...func(Handler) Handler) Handler {
for _, mw := range middlewares {
handler = mw(handler)
}
return HandlerFunc(func(ctx context.Context, message *Message) (err error) {
return handler.HandleMessage(ctx, message)
})
}