forked from giautm/eh-kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventbus.go
300 lines (254 loc) · 6.97 KB
/
eventbus.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
package kafka
import (
"context"
"fmt"
"sync"
"time"
"github.com/google/uuid"
eh "github.com/looplab/eventhorizon"
"github.com/pkg/errors"
"github.com/giautm/eh-kafka/json"
)
type KeyValueLogger interface {
Log(keyVals ...interface{}) error
}
var Logger KeyValueLogger = &nopLogger{}
var ErrHandlerNil = errors.New("handler can't be nil")
var ErrMatcherNil = errors.New("matcher can't be nil")
var ErrTimedOut = errors.New("cannot run handle: timeout")
type TopicProducer func(event eh.Event) (topic string, key string)
type TopicsConsumer func(event eh.EventHandler) []string
func DefaultTopicProducer(topic string) TopicProducer {
return func(event eh.Event) (string, string) {
return topic, event.AggregateID().String()
}
}
func DefaultTopicsConsumer(topic string) TopicsConsumer {
topics := []string{topic}
return func(event eh.EventHandler) []string {
return topics
}
}
// EventBus is an event bus that notifies registered EventHandlers of
// published events. It will use the SimpleEventHandlingStrategy by default.
type EventBus struct {
cancelFn context.CancelFunc
client KafkaClient
consumerCtx context.Context
consumerTopicsFunc TopicsConsumer
encoder Encoder
producer KafkaProducer
producerTopicFunc TopicProducer
timeout time.Duration
waitConsumer bool
registered map[eh.EventHandlerType]none
registeredMu sync.RWMutex
waitGroup sync.WaitGroup
errors chan eh.EventBusError
closed chan none
closeOnce sync.Once
}
// NewEventBus creates a EventBus.
func NewEventBus(
ctx context.Context,
brokers []string,
producerTopicFunc TopicProducer,
consumerTopicsFunc TopicsConsumer,
opts ...Option,
) (*EventBus, error) {
client := NewClient(brokers)
return NewEventBusWithConfig(ctx, client, producerTopicFunc, consumerTopicsFunc, opts...)
}
func NewEventBusWithBatchProducer(
ctx context.Context,
brokers []string,
producerTopicFunc TopicProducer,
consumerTopicsFunc TopicsConsumer,
opts ...Option,
) (*EventBus, error) {
client := NewClientWithConfig(brokers, NewConfigWithBatchProducer())
return NewEventBusWithConfig(ctx, client, producerTopicFunc, consumerTopicsFunc, opts...)
}
// NewEventBus creates a EventBus.
func NewEventBusWithConfig(
ctx context.Context,
client KafkaClient,
producerTopicFunc TopicProducer,
consumerTopicsFunc TopicsConsumer,
opts ...Option,
) (*EventBus, error) {
options := Options{
Encoder: json.NewEncoder(),
Timeout: time.Second * 90,
}
for _, o := range opts {
o(&options)
}
producer, err := client.NewProducer()
if err != nil {
return nil, err
}
consumerCtx, cancel := context.WithCancel(ctx)
return &EventBus{
cancelFn: cancel,
client: client,
consumerCtx: consumerCtx,
consumerTopicsFunc: consumerTopicsFunc,
encoder: options.Encoder,
producer: producer,
producerTopicFunc: producerTopicFunc,
registered: map[eh.EventHandlerType]none{},
timeout: options.Timeout,
waitConsumer: false,
errors: make(chan eh.EventBusError, 100),
closed: make(chan none),
}, nil
}
func (b *EventBus) SetWaitConsumer(wait bool) {
b.waitConsumer = wait
}
func (b *EventBus) Close() (err error) {
b.closeOnce.Do(func() {
close(b.closed)
close(b.errors)
b.cancelFn()
err = b.producer.Close()
b.waitGroup.Wait()
Logger.Log("msg", "All consumers exited")
})
return err
}
// PublishEvent publishes an event to all handlers capable of handling it.
func (b *EventBus) PublishEvent(ctx context.Context, event eh.Event) error {
data, err := b.encoder.Encode(ctx, event)
if err != nil {
return err
}
topic, key := b.producerTopicFunc(event)
_, _, err = b.producer.SendMessage(ctx, topic, ([]byte)(key), data)
if err != nil {
return errors.Wrapf(err,
"could not publish event (%s) (%s)", event, ctx)
}
return nil
}
// AddHandler implements the AddHandler method of the eventhorizon.EventBus interface.
func (b *EventBus) AddHandler(m eh.EventMatcher, h eh.EventHandler) {
if err := b.subscription(m, h, false); err != nil {
panic(err)
}
}
// AddObserver implements the AddObserver method of the eventhorizon.EventBus interface.
func (b *EventBus) AddObserver(m eh.EventMatcher, h eh.EventHandler) {
if err := b.subscription(m, h, true); err != nil {
panic(err)
}
}
// Errors returns an error channel where async handling errors are sent.
func (b *EventBus) Errors() <-chan eh.EventBusError {
return b.errors
}
// Checks the matcher and handler and gets the event subscription.
func (b *EventBus) subscription(m eh.EventMatcher, h eh.EventHandler, observer bool) error {
if m == nil {
return ErrMatcherNil
}
if h == nil {
return ErrHandlerNil
}
hType := h.HandlerType()
b.registeredMu.Lock()
if _, ok := b.registered[hType]; ok {
b.registeredMu.Unlock()
return errors.Errorf("multiple registrations for %s", hType)
}
b.registered[hType] = none{}
b.registeredMu.Unlock()
groupID := string(hType)
if observer { // Generate unique ID for each observer.
uid := uuid.New()
groupID = fmt.Sprintf("%s-%s", groupID, uid)
}
topics := b.consumerTopicsFunc(h)
return b.handle(b.consumerCtx, groupID, topics, b.handler(m, h))
}
func (b *EventBus) handle(ctx context.Context, groupID string, topics []string, handler HandlerFunc) error {
c, err := b.client.NewConsumer(ctx, groupID, topics)
if err != nil {
return err
}
go func() {
for err := range c.Errors() {
b.handleError(err)
Logger.Log("groupID", groupID, "err", err)
}
Logger.Log("groupID", groupID, "msg", "Handle error exited")
}()
// Start consumer
go func() {
b.waitGroup.Add(1)
Logger.Log("groupID", groupID, "msg", "New consumer")
defer func() {
b.handleError(c.Close())
b.waitGroup.Done()
Logger.Log("groupID", groupID, "msg", "Consumer exited")
}()
for {
select {
case <-b.closed:
return
default:
}
err := c.Receive(ctx, handler)
if err != nil {
if err == context.Canceled {
return
}
b.handleError(err)
}
}
}()
// (giautm) NOTE: Waiting for consumer started, helpful in unit test.
if b.waitConsumer {
if err = c.Wait(b.timeout); err != nil {
b.handleError(c.Close())
}
}
return err
}
func (b *EventBus) handleError(err error) {
select {
case <-b.closed:
return
default:
}
if err != nil {
select {
case b.errors <- eh.EventBusError{Err: err}:
default:
}
}
}
func (b *EventBus) handler(m eh.EventMatcher, h eh.EventHandler) HandlerFunc {
return func(_ context.Context, msg ConsumerMessage) error {
Logger.Log("topic", msg.Topic(), "offset", msg.Offset(), "key", (string)(msg.Key()))
event, ctx, err := b.encoder.Decode(msg.Value())
if err != nil {
return eh.EventBusError{
Err: err,
Ctx: ctx,
}
}
if m(event) {
// Notify all observers about the event.
if err := h.HandleEvent(ctx, event); err != nil {
return eh.EventBusError{
Err: errors.Wrapf(err, "could not handle event (%s)", h.HandlerType()),
Ctx: ctx,
Event: event,
}
}
}
return nil
}
}