-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdispatcher.go
173 lines (143 loc) · 4.15 KB
/
dispatcher.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
package eventually
import (
"context"
"fmt"
"github.com/ahmedkamals/eventually/internal/errors"
"time"
)
type (
// Dispatcher is responsible for dispatching topics.
Dispatcher interface {
// monitorDispatch of topicsMap publishing requests.
monitorDispatch(context.Context)
// dispatch topic(s).
dispatch(...Descriptor)
// publishAfter drops a message at consumers mail boxes after specified duration.
publishAfter(context.Context, time.Duration, ...Descriptor)
// schedulePublish drops a message at consumers' mail boxes 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)
}
// bus struct
dispatcher struct {
eventStore EventStore
dispatchChan chan Descriptor
logger Logger
errorQueue ErrorQueue
}
)
// NewDispatcher creates a new Dispatcher.
func NewDispatcher(eventStore EventStore, logger Logger, errorQueue ErrorQueue, bufferSize int) Dispatcher {
return &dispatcher{
eventStore: eventStore,
dispatchChan: make(chan Descriptor, bufferSize),
logger: logger,
errorQueue: errorQueue,
}
}
func (d *dispatcher) getTopicConsumers(aggregateID UUID, topic Descriptor) []Consumer {
consumers := make([]Consumer, 0)
topicConsumers, ok := d.eventStore.Load(aggregateID)
if !ok {
return consumers
}
for topicConsumer := range topicConsumers.Iterator() {
match := topicConsumer.MatchCriteria()
if match != nil && !match(topic) {
continue
}
consumers = append(consumers, topicConsumer)
}
return consumers
}
func (d *dispatcher) getMatchedConsumers(topic Descriptor) []Consumer {
const op errors.Operation = "Dispatcher.getMatchedConsumers"
defer func() {
if err := recover(); err != nil {
d.errorQueue.Report(errors.E(op, errors.Panic, err))
}
}()
universalConsumers := d.getTopicConsumers(universalTopicUUID, nil)
topicConsumers := d.getTopicConsumers(topic.AggregateID(), topic)
if len(universalConsumers) == 0 {
return topicConsumers
}
uniqueConsumers := make(map[Consumer]struct{})
for _, consumer := range universalConsumers {
uniqueConsumers[consumer] = struct{}{}
}
for _, consumer := range topicConsumers {
uniqueConsumers[consumer] = struct{}{}
}
consumers := make([]Consumer, len(uniqueConsumers))
index := 0
for consumer := range uniqueConsumers {
consumers[index] = consumer
index++
}
return consumers
}
// publish a message and drops it at consumers mail boxes.
func (d *dispatcher) publish(topic Descriptor) {
const op errors.Operation = "Dispatcher.publish"
d.logger.Log(fmt.Sprintf("Publishing topic %s", topic))
consumers := d.getMatchedConsumers(topic)
if len(consumers) == 0 {
return
}
for _, consumer := range consumers {
go func(consumer Consumer) {
defer func() {
if err := recover(); err != nil {
d.errorQueue.Report(errors.E(op, errors.Panic, err))
}
}()
consumer.Drop(topic)
}(consumer)
}
}
func (d *dispatcher) publishOnTimeBasis(ctx context.Context, topic Descriptor, timeChan <-chan time.Time) {
for {
select {
case <-timeChan:
d.dispatch(topic)
case <-ctx.Done():
d.errorQueue.Report(ctx.Err())
return
}
}
}
func (d *dispatcher) publishAfter(ctx context.Context, duration time.Duration, topics ...Descriptor) {
for _, topic := range topics {
go d.publishOnTimeBasis(ctx, topic, time.After(duration))
}
}
func (d *dispatcher) schedulePublish(ctx context.Context, ticker *time.Ticker, topics ...Descriptor) {
for _, topic := range topics {
go d.publishOnTimeBasis(ctx, topic, ticker.C)
}
}
// monitorDispatch of topicsMap to their relevant consumers.
func (d *dispatcher) monitorDispatch(controlCtx context.Context) {
const op errors.Operation = "Dispatcher.monitorDispatch"
defer func() {
if err := recover(); err != nil {
d.errorQueue.Report(errors.E(op, errors.Panic, err))
}
}()
for {
select {
case topic := <-d.dispatchChan:
d.publish(topic)
case <-controlCtx.Done():
return
}
}
}
func (d *dispatcher) dispatch(topics ...Descriptor) {
for _, topic := range topics {
go func(topic Descriptor) {
d.dispatchChan <- topic
}(topic)
}
}