-
Notifications
You must be signed in to change notification settings - Fork 2
/
bus.go
276 lines (220 loc) · 6.73 KB
/
bus.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
package eventually
import (
"context"
"fmt"
"github.com/ahmedkamals/eventually/internal/errors"
"reflect"
"time"
)
type (
producer interface {
// Publish topic(s).
Publish(...Descriptor)
// PublishAfter specified duration a topic(s).
PublishAfter(context.Context, time.Duration, ...Descriptor)
}
scheduler interface {
// SchedulePublish for a topic(s) when every specified duration elapses.
// The ticker should Run infinitely during the application life time, unless got context timeout.
SchedulePublish(context.Context, *time.Ticker, ...Descriptor)
}
runner interface {
// Run event bus operations.
Run(context.Context)
}
// Bus implements publish/subscribe pattern: https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern
Bus interface {
producer
scheduler
runner
// Topics returns the current registered topicsMap.
Topics() []Descriptor
// Length returns the number of subscribed topicsMap.
Length() int
// Subscribe a certain consumer to a single or a group of topicsMap.
Subscribe(Consumer, ...Descriptor) error
// SubscribeToAll topicsMap.
SubscribeToAll(Consumer) error
// Unsubscribe removes a consumer for a group of topicsMap.
Unsubscribe(Consumer, ...Descriptor) error
// Unregister removes all consumers for the specified topicsMap.
Unregister(...Descriptor) error
}
// Logger interface for logging operations.
Logger interface {
Log(string)
}
// ErrorQueue interface for error reporting.
ErrorQueue interface {
Report(error)
}
// bus struct
bus struct {
eventStore EventStore
dispatcher Dispatcher
registerChan chan subscription
unRegisterChan chan subscription
logger Logger
errorQueue ErrorQueue
}
)
func (b *bus) Topics() []Descriptor {
return b.eventStore.Topics()
}
func (b *bus) Length() int {
return b.eventStore.Length()
}
func (b *bus) Publish(topics ...Descriptor) {
b.dispatcher.dispatch(topics...)
}
func (b *bus) PublishAfter(ctx context.Context, duration time.Duration, topics ...Descriptor) {
go b.dispatcher.publishAfter(ctx, duration, topics...)
}
func (b *bus) SchedulePublish(ctx context.Context, ticker *time.Ticker, topics ...Descriptor) {
go b.dispatcher.schedulePublish(ctx, ticker, topics...)
}
func (b *bus) subscribe(ctx context.Context, consumer Consumer, topics ...Descriptor) {
const op errors.Operation = "Bus.subscribe"
defer b.recover(op)
for _, topic := range topics {
select {
case <-ctx.Done():
break
default:
}
if topic == nil {
b.errorQueue.Report(errors.E(op, errors.Invalid))
continue
}
if b.eventStore.IsSubscribed(topic, consumer) {
b.errorQueue.Report(errors.E(op, errors.Exist, errors.Errorf("%s", topic)))
continue
}
b.eventStore.Store(topic, consumer)
consumer.OnSubscribe(topic, func(topic Descriptor, consumer Consumer) {
b.logger.Log(fmt.Sprintf("Topic %s subscription is announced by %s.OnSubscribe", topic, consumer))
})
}
}
func (b *bus) Subscribe(consumer Consumer, topics ...Descriptor) error {
const op errors.Operation = "Bus.Subscribe"
if consumer == nil || reflect.ValueOf(consumer).IsNil() {
return errors.E(op, errors.Invalid)
}
if len(topics) == 0 {
return errors.E(op, errors.MinLength)
}
go func() {
b.registerChan <- newNormalSubscription(consumer, topics...)
}()
return nil
}
func (b *bus) SubscribeToAll(consumer Consumer) error {
const op errors.Operation = "Bus.SubscribeToAll"
if consumer == nil || reflect.ValueOf(consumer).IsNil() {
return errors.E(op, errors.Invalid)
}
go func() {
b.registerChan <- newUniversalSubscription(consumer)
}()
return nil
}
func (b *bus) unsubscribe(consumer Consumer, topics ...Descriptor) {
const op errors.Operation = "Bus.unsubscribe"
defer b.recover(op)
for _, topic := range topics {
if !b.eventStore.Remove(topic, consumer) {
b.errorQueue.Report(errors.E(op, errors.NotFound, errors.Errorf(" for consumer %#v to %s", consumer, topic)))
continue
}
consumer.Signout()
consumer.OnUnsubscribe(topic, func(topic Descriptor, consumer Consumer) {
b.logger.Log(fmt.Sprintf("Topic %s subscription cancellation is announced by %s.OnUnubscribe", topic, consumer))
})
}
}
func (b *bus) Unsubscribe(consumer Consumer, topics ...Descriptor) error {
const op errors.Operation = "Bus.Unsubscribe"
if len(topics) == 0 {
return errors.E(op, errors.MinLength)
}
go func() {
b.unRegisterChan <- newNormalSubscription(consumer, topics...)
}()
return nil
}
func (b *bus) unregister(topics ...Descriptor) {
const op errors.Operation = "Bus.unregister"
defer b.recover(op)
for _, topic := range topics {
b.eventStore.DeleteTopic(topic)
}
}
func (b *bus) Unregister(topics ...Descriptor) error {
const op errors.Operation = "bus.Unregister"
if len(topics) == 0 {
return errors.E(op, errors.MinLength)
}
go func() {
b.logger.Log(fmt.Sprintf("Unregistering topicsMap %s", topics))
b.unRegisterChan <- newNormalSubscription(nil, topics...)
}()
return nil
}
// monitorRegistration of consumers to topicsMap.
func (b *bus) monitorRegistration(controlCtx context.Context) {
const op errors.Operation = "Bus.monitorRegistration"
defer b.recover(op)
for {
select {
case subscription := <-b.registerChan:
if subscription.isUniversal() {
go b.subscribe(controlCtx, subscription.getConsumer(), newUniversalDescriptor("universalDescriptor", nil, 1))
return
}
go b.subscribe(controlCtx, subscription.getConsumer(), subscription.getTopics()...)
case <-controlCtx.Done():
return
}
}
}
// monitorUnRegistration forwards subscriptions cancellation for specific/all topicsMap.
func (b *bus) monitorUnRegistration(controlCtx context.Context) {
const op errors.Operation = "Bus.monitorUnRegistration"
defer b.recover(op)
for {
select {
case subscription := <-b.unRegisterChan:
if subscription.getConsumer() != nil {
go b.unsubscribe(subscription.getConsumer(), subscription.getTopics()...)
continue
}
go b.unregister(subscription.getTopics()...)
case <-controlCtx.Done():
return
}
}
}
func (b *bus) recover(op errors.Operation) {
if err := recover(); err != nil {
b.errorQueue.Report(errors.E(op, errors.Panic, err))
}
}
// Run the bus operations.
func (b *bus) Run(controlCtx context.Context) {
go b.monitorRegistration(controlCtx)
go b.dispatcher.monitorDispatch(controlCtx)
go b.monitorUnRegistration(controlCtx)
}
// NewBus creates new Bus
func NewBus(eventStore EventStore, logger Logger, errorQueue ErrorQueue, bufferSize int) Bus {
eventBus := &bus{
eventStore: eventStore,
dispatcher: NewDispatcher(eventStore, logger, errorQueue, bufferSize),
registerChan: make(chan subscription, bufferSize),
unRegisterChan: make(chan subscription, bufferSize),
logger: logger,
errorQueue: errorQueue,
}
return eventBus
}