forked from looplab/eventhorizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventbus.go
378 lines (334 loc) · 10.6 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
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
// Copyright (c) 2014 - The Event Horizon authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gcp
import (
"context"
"errors"
"fmt"
"log"
"strings"
"sync"
"time"
"cloud.google.com/go/pubsub"
"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson"
"google.golang.org/api/option"
// Register uuid.UUID as BSON type.
_ "github.com/wr4thon/eventhorizon/types/mongodb"
eh "github.com/wr4thon/eventhorizon"
)
// DefaultQueueSize is the default queue size per handler for publishing events.
var DefaultQueueSize = 10
// EventBus is a local event bus that delegates handling of published events
// to all matching registered handlers, in order of registration.
type EventBus struct {
appID string
client *pubsub.Client
topic *pubsub.Topic
registered map[eh.EventHandlerType]struct{}
registeredMu sync.RWMutex
errCh chan eh.EventBusError
wg sync.WaitGroup
}
// NewEventBus creates an EventBus, with optional GCP connection settings.
func NewEventBus(projectID, appID string, opts ...option.ClientOption) (*EventBus, error) {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, projectID, opts...)
if err != nil {
return nil, err
}
// Get or create the topic.
name := appID + "_events"
topic := client.Topic(name)
if ok, err := topic.Exists(ctx); err != nil {
return nil, err
} else if !ok {
if topic, err = client.CreateTopic(ctx, name); err != nil {
return nil, err
}
}
topic.EnableMessageOrdering = true
return &EventBus{
appID: appID,
client: client,
topic: topic,
registered: map[eh.EventHandlerType]struct{}{},
errCh: make(chan eh.EventBusError, 100),
}, nil
}
// HandlerType implements the HandlerType method of the eventhorizon.EventHandler interface.
func (b *EventBus) HandlerType() eh.EventHandlerType {
return "eventbus"
}
const (
aggregateTypeAttribute = "aggregate_type"
eventTypeAttribute = "event_type"
)
// HandleEvent implements the HandleEvent method of the eventhorizon.EventHandler interface.
func (b *EventBus) HandleEvent(ctx context.Context, event eh.Event) error {
e := evt{
AggregateID: event.AggregateID().String(),
AggregateType: event.AggregateType(),
EventType: event.EventType(),
Version: event.Version(),
Timestamp: event.Timestamp(),
Context: eh.MarshalContext(ctx),
}
// Marshal event data if there is any.
if event.Data() != nil {
var err error
if e.RawData, err = bson.Marshal(event.Data()); err != nil {
return errors.New("could not marshal event data: " + err.Error())
}
}
// Marshal the event (using BSON for now).
data, err := bson.Marshal(e)
if err != nil {
return errors.New("could not marshal event: " + err.Error())
}
res := b.topic.Publish(ctx, &pubsub.Message{
Data: data,
Attributes: map[string]string{
aggregateTypeAttribute: event.AggregateType().String(),
event.EventType().String(): "", // The event type as a key to save space when filtering.
},
OrderingKey: event.AggregateID().String(),
})
if _, err := res.Get(ctx); err != nil {
return errors.New("could not publish event: " + err.Error())
}
return nil
}
// AddHandler implements the AddHandler method of the eventhorizon.EventBus interface.
func (b *EventBus) AddHandler(ctx context.Context, m eh.EventMatcher, h eh.EventHandler) error {
if m == nil {
return eh.ErrMissingMatcher
}
if h == nil {
return eh.ErrMissingHandler
}
// Check handler existence.
b.registeredMu.Lock()
defer b.registeredMu.Unlock()
if _, ok := b.registered[h.HandlerType()]; ok {
return eh.ErrHandlerAlreadyAdded
}
// Build the subscription filter.
filter := createFilter(m)
if len(filter) >= 256 {
return fmt.Errorf("match filter is longer than 256 chars: %d", len(filter))
}
// Get or create the subscription.
subscriptionID := b.appID + "_" + h.HandlerType().String()
sub := b.client.Subscription(subscriptionID)
if ok, err := sub.Exists(ctx); err != nil {
return fmt.Errorf("could not check existing subscription: %w", err)
} else if !ok {
if sub, err = b.client.CreateSubscription(ctx, subscriptionID,
pubsub.SubscriptionConfig{
Topic: b.topic,
Filter: filter,
EnableMessageOrdering: true,
},
); err != nil {
return fmt.Errorf("could not create subscription: %w", err)
}
} else if ok {
cfg, err := sub.Config(ctx)
if err != nil {
return fmt.Errorf("could not get subscription config: %w", err)
}
if cfg.Filter != filter {
return fmt.Errorf("the existing filter for '%s' differs, please remove to recreate", h.HandlerType())
}
if !cfg.EnableMessageOrdering {
return fmt.Errorf("message ordering not enabled for subscription '%s', please remove to recreate", h.HandlerType())
}
}
// Default is to use 10 goroutines which is often not needed for multiple
// handlers.
sub.ReceiveSettings.NumGoroutines = 2
// Register handler.
b.registered[h.HandlerType()] = struct{}{}
// Handle until context is cancelled.
b.wg.Add(1)
go b.handle(ctx, m, h, sub)
return nil
}
// Errors implements the Errors method of the eventhorizon.EventBus interface.
func (b *EventBus) Errors() <-chan eh.EventBusError {
return b.errCh
}
// Wait for all channels to close in the event bus group
func (b *EventBus) Wait() {
b.wg.Wait()
}
// Handles all events coming in on the channel.
func (b *EventBus) handle(ctx context.Context, m eh.EventMatcher, h eh.EventHandler, sub *pubsub.Subscription) {
defer b.wg.Done()
for {
if err := sub.Receive(ctx, b.handler(m, h)); err != nil {
err = fmt.Errorf("could not receive: %w", err)
select {
case b.errCh <- eh.EventBusError{Err: err, Ctx: ctx}:
default:
log.Printf("missed error in GCP event bus: %s", err)
}
// Retry the receive loop if there was an error.
time.Sleep(time.Second)
continue
}
return
}
}
func (b *EventBus) handler(m eh.EventMatcher, h eh.EventHandler) func(ctx context.Context, msg *pubsub.Message) {
return func(ctx context.Context, msg *pubsub.Message) {
// Decode the raw BSON event data.
var e evt
if err := bson.Unmarshal(msg.Data, &e); err != nil {
err = fmt.Errorf("could not unmarshal event: %w", err)
select {
case b.errCh <- eh.EventBusError{Err: err, Ctx: ctx}:
default:
log.Printf("missed error in GCP event bus: %s", err)
}
msg.Nack()
return
}
// Create an event of the correct type and decode from raw BSON.
if len(e.RawData) > 0 {
var err error
if e.data, err = eh.CreateEventData(e.EventType); err != nil {
err = fmt.Errorf("could not create event data: %w", err)
select {
case b.errCh <- eh.EventBusError{Err: err, Ctx: ctx}:
default:
log.Printf("missed error in GCP event bus: %s", err)
}
msg.Nack()
return
}
if err := bson.Unmarshal(e.RawData, e.data); err != nil {
err = fmt.Errorf("could not unmarshal event data: %w", err)
select {
case b.errCh <- eh.EventBusError{Err: err, Ctx: ctx}:
default:
log.Printf("missed error in GCP event bus: %s", err)
}
msg.Nack()
return
}
e.RawData = nil
}
event := event{evt: e}
ctx = eh.UnmarshalContext(ctx, e.Context)
// Ignore non-matching events.
if !m.Match(event) {
msg.Ack()
return
}
// Handle the event if it did match.
if err := h.HandleEvent(ctx, event); err != nil {
err = fmt.Errorf("could not handle event (%s): %w", h.HandlerType(), err)
select {
case b.errCh <- eh.EventBusError{Err: err, Ctx: ctx, Event: event}:
default:
log.Printf("missed error in GCP event bus: %s", err)
}
msg.Nack()
return
}
msg.Ack()
}
}
// Creates a filter in the GCP pub sub filter syntax:
// https://cloud.google.com/pubsub/docs/filtering
func createFilter(m eh.EventMatcher) string {
switch m := m.(type) {
case eh.MatchEvents:
s := make([]string, len(m))
for i, et := range m {
s[i] = fmt.Sprintf(`attributes:"%s"`, et) // Filter event types by key to save space.
}
return strings.Join(s, " OR ")
case eh.MatchAggregates:
s := make([]string, len(m))
for i, at := range m {
s[i] = fmt.Sprintf(`attributes.%s="%s"`, aggregateTypeAttribute, at)
}
return strings.Join(s, " OR ")
case eh.MatchAny:
s := make([]string, len(m))
for i, sm := range m {
s[i] = fmt.Sprintf("(%s)", createFilter(sm))
}
return strings.Join(s, " OR ")
case eh.MatchAll:
s := make([]string, len(m))
for i, sm := range m {
s[i] = fmt.Sprintf("(%s)", createFilter(sm))
}
return strings.Join(s, " AND ")
default:
return ""
}
}
// evt is the internal event used on the wire only.
type evt struct {
EventType eh.EventType `bson:"event_type"`
RawData bson.Raw `bson:"data,omitempty"`
data eh.EventData `bson:"-"`
Timestamp time.Time `bson:"timestamp"`
AggregateType eh.AggregateType `bson:"aggregate_type"`
AggregateID string `bson:"_id"`
Version int `bson:"version"`
Context map[string]interface{} `bson:"context"`
}
// event is the private implementation of the eventhorizon.Event interface
// for a MongoDB event store.
type event struct {
evt
}
// EventType implements the EventType method of the eventhorizon.Event interface.
func (e event) EventType() eh.EventType {
return e.evt.EventType
}
// Data implements the Data method of the eventhorizon.Event interface.
func (e event) Data() eh.EventData {
return e.evt.data
}
// Timestamp implements the Timestamp method of the eventhorizon.Event interface.
func (e event) Timestamp() time.Time {
return e.evt.Timestamp
}
// AggregateType implements the AggregateType method of the eventhorizon.Event interface.
func (e event) AggregateType() eh.AggregateType {
return e.evt.AggregateType
}
// AggrgateID implements the AggrgateID method of the eventhorizon.Event interface.
func (e event) AggregateID() uuid.UUID {
id, err := uuid.Parse(e.evt.AggregateID)
if err != nil {
return uuid.Nil
}
return id
}
// Version implements the Version method of the eventhorizon.Event interface.
func (e event) Version() int {
return e.evt.Version
}
// String implements the String method of the eventhorizon.Event interface.
func (e event) String() string {
return fmt.Sprintf("%s@%d", e.evt.EventType, e.evt.Version)
}