forked from looplab/eventhorizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publisher.go
268 lines (226 loc) · 7.4 KB
/
publisher.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
// Copyright (c) 2014 - Max Ekman <max@looplab.se>
//
// 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"
"time"
"cloud.google.com/go/pubsub"
contextorg "golang.org/x/net/context"
"gopkg.in/mgo.v2/bson"
eh "github.com/looplab/eventhorizon"
"github.com/looplab/eventhorizon/publisher/local"
)
// ErrCouldNotMarshalEvent is when an event could not be marshaled into BSON.
var ErrCouldNotMarshalEvent = errors.New("could not marshal event")
// ErrCouldNotUnmarshalEvent is when an event could not be unmarshaled into a concrete type.
var ErrCouldNotUnmarshalEvent = errors.New("could not unmarshal event")
// EventPublisher is an event bus that notifies registered EventHandlers of
// published events. It will use the SimpleEventHandlingStrategy by default.
type EventPublisher struct {
*local.EventPublisher
client *pubsub.Client
topic *pubsub.Topic
ready chan bool // NOTE: Used for testing only
exit chan bool
}
// NewEventPublisher creates a EventPublisher.
func NewEventPublisher(projectID, appID string) (*EventPublisher, error) {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, projectID)
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
}
}
b := &EventPublisher{
EventPublisher: local.NewEventPublisher(),
client: client,
topic: topic,
ready: make(chan bool, 1), // Buffered to not block receive loop.
exit: make(chan bool),
}
go b.recv()
return b, nil
}
// PublishEvent implements the PublishEvent method of the eventhorizon.EventPublisher interface.
func (b *EventPublisher) PublishEvent(ctx context.Context, event eh.Event) error {
gcpEvent := gcpEvent{
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 ErrCouldNotMarshalEvent
}
gcpEvent.RawData = bson.Raw{Kind: 3, Data: rawData}
}
// Marshal the event (using BSON for now).
var data []byte
var err error
if data, err = bson.Marshal(gcpEvent); err != nil {
return ErrCouldNotMarshalEvent
}
// NOTE: Using a new context here.
var results []*pubsub.PublishResult
r := b.topic.Publish(context.Background(), &pubsub.Message{
Data: data,
})
results = append(results, r)
for _, r := range results {
id, err := r.Get(ctx)
if err != nil {
return err
}
// TODO: Use the message ID to avoid handling duplicate messages.
_ = id
}
return nil
}
// Close exits the receive goroutine by unsubscribing to all channels.
func (b *EventPublisher) Close() error {
select {
case b.exit <- true:
default:
log.Println("eventpublisher: already closed")
}
<-b.exit
return b.topic.Delete(context.Background())
}
func (b *EventPublisher) recv() {
ctx := context.Background()
// Create the subscription, it should not exist as we use a new UUID as name.
id := "subscriber_" + eh.NewUUID().String()
sub, err := b.client.CreateSubscription(ctx, id, pubsub.SubscriptionConfig{
Topic: b.topic,
AckDeadline: 10 * time.Second,
})
if err != nil {
// TODO: Handle error.
log.Println("eventpublisher: could not create subscription:", err)
return
}
log.Println("eventpublisher: start receiving")
go func() {
<-b.exit
if err := sub.Delete(ctx); err != nil {
log.Println("eventpublisher: could not delete subscription:", err)
}
log.Println("eventpublisher: stop receiving")
close(b.exit)
}()
// Don't block if no one is receiving and buffer is full.
select {
case b.ready <- true:
default:
}
err = sub.Receive(ctx, func(ctx contextorg.Context, m *pubsub.Message) {
if err := b.handleMessage(m); err != nil {
log.Println("eventpublisher: error publishing:", err)
}
m.Ack()
})
if err != contextorg.Canceled {
log.Println("eventpublisher: could not get next message:", err)
}
}
func (b *EventPublisher) handleMessage(msg *pubsub.Message) error {
// // TODO: Only ack true when event is handled correctly.
// defer msg.Done(true)
// Manually decode the raw BSON event.
data := bson.Raw{
Kind: 3,
Data: msg.Data,
}
var gcpEvent gcpEvent
if err := data.Unmarshal(&gcpEvent); err != nil {
// TODO: Forward the real error.
return ErrCouldNotUnmarshalEvent
}
// Create an event of the correct type.
if data, err := eh.CreateEventData(gcpEvent.EventType); err == nil {
// Manually decode the raw BSON event.
if err := gcpEvent.RawData.Unmarshal(data); err != nil {
// TODO: Forward the real error.
return ErrCouldNotUnmarshalEvent
}
// Set concrete event and zero out the decoded event.
gcpEvent.data = data
gcpEvent.RawData = bson.Raw{}
}
event := event{gcpEvent: gcpEvent}
ctx := eh.UnmarshalContext(gcpEvent.Context)
// Notify all observers about the event.
return b.EventPublisher.PublishEvent(ctx, event)
}
// gcpEvent is the internal event used with the gcp event bus.
type gcpEvent 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 eh.UUID `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 {
gcpEvent
}
// EventType implements the EventType method of the eventhorizon.Event interface.
func (e event) EventType() eh.EventType {
return e.gcpEvent.EventType
}
// Data implements the Data method of the eventhorizon.Event interface.
func (e event) Data() eh.EventData {
return e.gcpEvent.data
}
// Timestamp implements the Timestamp method of the eventhorizon.Event interface.
func (e event) Timestamp() time.Time {
return e.gcpEvent.Timestamp
}
// AggregateType implements the AggregateType method of the eventhorizon.Event interface.
func (e event) AggregateType() eh.AggregateType {
return e.gcpEvent.AggregateType
}
// AggrgateID implements the AggrgateID method of the eventhorizon.Event interface.
func (e event) AggregateID() eh.UUID {
return e.gcpEvent.AggregateID
}
// Version implements the Version method of the eventhorizon.Event interface.
func (e event) Version() int {
return e.gcpEvent.Version
}
// String implements the String method of the eventhorizon.Event interface.
func (e event) String() string {
return fmt.Sprintf("%s@%d", e.gcpEvent.EventType, e.gcpEvent.Version)
}