This repository has been archived by the owner on Jun 18, 2022. It is now read-only.
forked from looplab/eventhorizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventbus.go
293 lines (253 loc) · 8.25 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
// 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"
"sync"
"time"
"cloud.google.com/go/pubsub"
"github.com/globalsign/mgo/bson"
"github.com/google/uuid"
"google.golang.org/api/option"
eh "github.com/EllisDon-Aegean/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
}
// 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
}
}
return &EventBus{
appID: appID,
client: client,
topic: topic,
registered: map[eh.EventHandlerType]struct{}{},
errCh: make(chan eh.EventBusError, 100),
}, nil
}
// PublishEvent implements the PublishEvent method of the eventhorizon.EventBus interface.
func (b *EventBus) PublishEvent(ctx context.Context, event eh.Event) error {
e := evt{
AggregateID: event.AggregateID(),
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 {
rawData, err := bson.Marshal(event.Data())
if err != nil {
return errors.New("could not marshal event data: " + err.Error())
}
e.RawData = bson.Raw{Kind: 3, Data: rawData}
}
// Marshal the event (using BSON for now).
data, err := bson.Marshal(e)
if err != nil {
return errors.New("could not marshal event: " + err.Error())
}
// NOTE: Using a new context here.
// TODO: Why?
publishCtx := context.Background()
res := b.topic.Publish(publishCtx, &pubsub.Message{
Data: data,
})
if _, err := res.Get(publishCtx); 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(m eh.EventMatcher, h eh.EventHandler) {
sub := b.subscription(m, h, false)
go b.handle(m, h, sub)
}
// AddObserver implements the AddObserver method of the eventhorizon.EventBus interface.
func (b *EventBus) AddObserver(m eh.EventMatcher, h eh.EventHandler) {
sub := b.subscription(m, h, true)
go b.handle(m, h, sub)
}
// Errors implements the Errors method of the eventhorizon.EventBus interface.
func (b *EventBus) Errors() <-chan eh.EventBusError {
return b.errCh
}
// Checks the matcher and handler and gets the event subscription.
func (b *EventBus) subscription(m eh.EventMatcher, h eh.EventHandler, observer bool) *pubsub.Subscription {
b.registeredMu.Lock()
defer b.registeredMu.Unlock()
if m == nil {
panic("matcher can't be nil")
}
if h == nil {
panic("handler can't be nil")
}
if _, ok := b.registered[h.HandlerType()]; ok {
panic(fmt.Sprintf("multiple registrations for %s", h.HandlerType()))
}
b.registered[h.HandlerType()] = struct{}{}
id := string(h.HandlerType())
if observer { // Generate unique ID for each observer.
id = fmt.Sprintf("%s-%s", id, uuid.New())
}
ctx := context.Background()
// Get or create the subscription.
subscriptionID := b.appID + "_" + id
sub := b.client.Subscription(subscriptionID)
if ok, err := sub.Exists(ctx); err != nil {
panic("could not check subscription: " + err.Error())
} else if !ok {
if sub, err = b.client.CreateSubscription(ctx, subscriptionID,
pubsub.SubscriptionConfig{
Topic: b.topic,
AckDeadline: 60 * time.Second,
},
); err != nil {
panic("could not create subscription: " + err.Error())
}
}
return sub
}
// Handles all events coming in on the channel.
func (b *EventBus) handle(m eh.EventMatcher, h eh.EventHandler, sub *pubsub.Subscription) {
for {
ctx := context.Background()
if err := sub.Receive(ctx, b.handler(m, h)); err != context.Canceled {
select {
case b.errCh <- eh.EventBusError{Ctx: ctx, Err: errors.New("could not receive: " + err.Error())}:
default:
}
}
time.Sleep(time.Second)
}
}
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) {
// Manually decode the raw BSON event.
data := bson.Raw{
Kind: 3,
Data: msg.Data,
}
var e evt
if err := data.Unmarshal(&e); err != nil {
select {
case b.errCh <- eh.EventBusError{Err: errors.New("could not unmarshal event: " + err.Error()), Ctx: ctx}:
default:
}
msg.Nack()
return
}
// Create an event of the correct type.
if data, err := eh.CreateEventData(e.EventType); err == nil {
// Manually decode the raw BSON event.
if err := e.RawData.Unmarshal(data); err != nil {
select {
case b.errCh <- eh.EventBusError{Err: errors.New("could not unmarshal event data: " + err.Error()), Ctx: ctx}:
default:
}
msg.Nack()
return
}
// Set concrete event and zero out the decoded event.
e.data = data
e.RawData = bson.Raw{}
}
event := event{evt: e}
ctx = eh.UnmarshalContext(e.Context)
if !m(event) {
msg.Ack()
return
}
// Notify all observers about the event.
if err := h.HandleEvent(ctx, event); err != nil {
select {
case b.errCh <- eh.EventBusError{Err: fmt.Errorf("could not handle event (%s): %s", h.HandlerType(), err.Error()), Ctx: ctx, Event: event}:
default:
}
msg.Nack()
return
}
msg.Ack()
}
}
// 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() string {
return e.evt.AggregateID
}
// 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)
}