-
Notifications
You must be signed in to change notification settings - Fork 129
/
subscriber.go
82 lines (70 loc) · 1.88 KB
/
subscriber.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
package gofr
import (
"context"
"errors"
"runtime/debug"
"gofr.dev/pkg/gofr/container"
"gofr.dev/pkg/gofr/datasource/pubsub/kafka"
"gofr.dev/pkg/gofr/logging"
)
type SubscribeFunc func(c *Context) error
type SubscriptionManager struct {
container *container.Container
subscriptions map[string]SubscribeFunc
}
func newSubscriptionManager(c *container.Container) SubscriptionManager {
return SubscriptionManager{
container: c,
subscriptions: make(map[string]SubscribeFunc),
}
}
func (s *SubscriptionManager) startSubscriber(topic string, handler SubscribeFunc) {
// continuously subscribe in an infinite loop
for {
msg, err := s.container.GetSubscriber().Subscribe(context.Background(), topic)
if msg == nil {
continue
}
if errors.Is(err, kafka.ErrConsumerGroupNotProvided) {
s.container.Logger.Errorf("cannot subscribe as consumer_id is not provided in configs")
return
} else if err != nil {
s.container.Logger.Errorf("error while reading from topic %v, err: %v", topic, err.Error())
continue
}
ctx := newContext(nil, msg, s.container)
err = func(ctx *Context) error {
// TODO : Move panic recovery at central location which will manage for all the different cases.
defer panicRecovery(ctx.Logger)
return handler(ctx)
}(ctx)
// commit the message if the subscription function does not return error
if err == nil {
msg.Commit()
} else {
s.container.Logger.Errorf("error in handler for topic %s: %v", topic, err)
}
}
}
type panicLog struct {
Error string `json:"error,omitempty"`
StackTrace string `json:"stack_trace,omitempty"`
}
func panicRecovery(log logging.Logger) {
re := recover()
if re != nil {
var e string
switch t := re.(type) {
case string:
e = t
case error:
e = t.Error()
default:
e = "Unknown panic type"
}
log.Error(panicLog{
Error: e,
StackTrace: string(debug.Stack()),
})
}
}