This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventbus.go
330 lines (285 loc) · 8.45 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
// Copyright (c) 2021 - 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 rabbitmq
import (
"context"
"fmt"
"github.com/google/uuid"
"log"
"sync"
"time"
"github.com/streadway/amqp"
"go.mongodb.org/mongo-driver/bson"
eh "github.com/looplab/eventhorizon"
_ "github.com/looplab/eventhorizon/types/mongodb"
)
// 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
exchange string
amqpConn *amqp.Connection
amqpChannel *amqp.Channel
registered map[eh.EventHandlerType]struct{}
registeredMu sync.RWMutex
errCh chan eh.EventBusError
wg sync.WaitGroup
}
func connect(server string) (*amqp.Connection, *amqp.Channel, error) {
conn, err := amqp.Dial(server)
if err != nil {
return nil, nil, fmt.Errorf("failed connecting to RabbitMQ: %w", err)
}
ch, err := conn.Channel()
if err != nil {
return nil, nil, fmt.Errorf("failed opening channel: %w", err)
}
return conn, ch, nil
}
// NewEventBus creates an EventBus, with optional GCP connection settings.
func NewEventBus(connectionString string, topicSuffix string) (*EventBus, error) {
//ctx := context.Background()
topic := topicSuffix + "_events"
conn, ch, err := connect(connectionString)
if err != nil {
return nil, err
}
if err := ch.ExchangeDeclare(
topic, // name
"fanout", // type
true, // durable
false, // auto-deleted
false, // internal
false, // no-wait
nil, // arguments
); err != nil {
return nil, err
}
return &EventBus{
amqpConn: conn,
amqpChannel: ch,
exchange: 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 (
aggregateTypeHeader = "aggregate_type"
eventTypeHeader = "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(),
Metadata: event.Metadata(),
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 fmt.Errorf("could not marshal event data: %w", err)
}
}
// Marshal the event (using BSON for now).
data, err := bson.Marshal(e)
if err != nil {
return fmt.Errorf("could not marshal event: %w", err)
}
if err := b.amqpChannel.Publish(
b.exchange,
"",
false,
false,
amqp.Publishing{
Headers: amqp.Table{
aggregateTypeHeader: event.AggregateType().String(),
eventTypeHeader: event.EventType().String(),
},
ContentType: "application/bson",
Body: data,
}); err != nil {
return fmt.Errorf("could not publish event: %w", err)
}
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
}
// Get or create the subscription.
groupID := b.appID + "_" + h.HandlerType().String()
q, err := b.amqpChannel.QueueDeclare(
groupID, // name
false, // durable
false, // delete when unused
true, // exclusive
false, // no-wait
nil, // arguments
)
if err != nil {
return fmt.Errorf("failed to declare a queue: %w", err)
}
err = b.amqpChannel.QueueBind(
q.Name, // queue name
"", // routing key
b.exchange, // exchange
false,
nil,
)
if err != nil {
return fmt.Errorf("failed to bind a queue: %w", err)
}
msgs, err := b.amqpChannel.Consume(
q.Name, // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
if err != nil {
return fmt.Errorf("failed to consume channel: %w", err)
}
// Register handler.
b.registered[h.HandlerType()] = struct{}{}
// Handle until context is cancelled.
b.wg.Add(1)
go b.handle(ctx, m, h, msgs)
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()
if err := b.amqpChannel.Close(); err != nil {
log.Printf("eventhorizon: failed to close RabbitMQ queue: %s", err)
}
if err := b.amqpConn.Close(); err != nil {
log.Printf("eventhorizon: failed to close RabbitMQ connection: %s", err)
}
}
// Handles all events coming in on the channel.
func (b *EventBus) handle(ctx context.Context, m eh.EventMatcher, h eh.EventHandler, msgs <-chan amqp.Delivery) {
defer b.wg.Done()
handler := b.handler(m, h)
for msg := range msgs {
handler(ctx, msg)
}
}
//
func (b *EventBus) handler(m eh.EventMatcher, h eh.EventHandler) func(ctx context.Context, msg amqp.Delivery) {
return func(ctx context.Context, msg amqp.Delivery) {
// Decode the raw BSON event data.
var e evt
if err := bson.Unmarshal(msg.Body, &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("eventhorizon: missed error in RabbitMQ event bus: %s", err)
}
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("eventhorizon: missed error in Kafka event bus: %s", err)
}
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("eventhorizon: missed error in Kafka event bus: %s", err)
}
return
}
e.RawData = nil
}
ctx = eh.UnmarshalContext(ctx, e.Context)
aggregateID, err := uuid.Parse(e.AggregateID)
if err != nil {
aggregateID = uuid.Nil
}
event := eh.NewEvent(
e.EventType,
e.data,
e.Timestamp,
eh.ForAggregate(
e.AggregateType,
aggregateID,
e.Version,
),
eh.WithMetadata(e.Metadata),
)
// Ignore non-matching events.
if !m.Match(event) {
// r.CommitMessages(ctx, msg)
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("eventhorizon: missed error in RabbitMQ event bus: %s", err)
}
return
}
//r.CommitMessages(ctx, msg)
}
}
// 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"`
Metadata map[string]interface{} `bson:"metadata"`
Context map[string]interface{} `bson:"context"`
}