/
bus.go
198 lines (155 loc) · 3.95 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
package pubsub
import (
"errors"
lifecycle "github.com/boz/go-lifecycle"
)
// ErrNotRunning is the error with message "not running"
var ErrNotRunning = errors.New("not running")
// Event interface
type Event interface{}
// Bus is an async event bus that allows subscriptions to behave as a bus themselves.
// When an event is published, it is sent to all subscribers asynchronously - a subscriber
// cannot block other subscribers.
//
// NOTE: this should probably be in util/event or something (not in provider/event)
type Bus interface {
Publish(Event) error
Subscribe() (Subscriber, error)
Close()
Done() <-chan struct{}
}
// Subscriber emits events it sees on the channel returned by Events().
// A Clone() of a subscriber will emit all events that have not been emitted
// from the cloned subscriber. This is important so that events are not missed
// when adding subscribers for sub-components (see `provider/bidengine/{service,order}.go`)
type Subscriber interface {
Events() <-chan Event
Clone() (Subscriber, error)
Close()
Done() <-chan struct{}
}
type bus struct {
subscriptions map[*bus]bool
evbuf []Event
eventch chan Event
parentch chan *bus
pubch chan Event
subch chan chan<- Subscriber
unsubch chan *bus
lc lifecycle.Lifecycle
}
// NewBus runs a new bus and returns bus details
func NewBus() Bus {
bus := &bus{
subscriptions: make(map[*bus]bool),
pubch: make(chan Event),
subch: make(chan chan<- Subscriber),
unsubch: make(chan *bus),
lc: lifecycle.New(),
}
go bus.run()
return bus
}
func (b *bus) Publish(ev Event) error {
select {
case b.pubch <- ev:
return nil
case <-b.lc.ShuttingDown():
return ErrNotRunning
}
}
func (b *bus) Subscribe() (Subscriber, error) {
ch := make(chan Subscriber, 1)
select {
case b.subch <- ch:
return <-ch, nil
case <-b.lc.ShuttingDown():
return nil, ErrNotRunning
}
}
func (b *bus) Clone() (Subscriber, error) {
return b.Subscribe()
}
func (b *bus) Events() <-chan Event {
return b.eventch
}
func (b *bus) Close() {
b.lc.Shutdown(nil)
}
func (b *bus) Done() <-chan struct{} {
return b.lc.Done()
}
func (b *bus) run() {
defer b.lc.ShutdownCompleted()
var outch chan<- Event
var curev Event
loop:
for {
if b.eventch != nil && len(b.evbuf) > 0 {
// If we're emitting events (Subscriber mode) and there
// are events to emit, set up the output channel and output
// event accordingly.
outch = b.eventch
curev = b.evbuf[0]
} else {
// otherwise block the output (sending to a nil channel always blocks)
outch = nil
}
select {
case err := <-b.lc.ShutdownRequest():
b.lc.ShutdownInitiated(err)
break loop
case outch <- curev:
// Event was emitted. Shrink current event buffer.
b.evbuf = b.evbuf[1:]
case ev := <-b.pubch:
// publish event
// Buffer event.
if b.eventch != nil {
b.evbuf = append(b.evbuf, ev)
}
// Publish to children.
for sub := range b.subscriptions {
if err := sub.Publish(ev); err != nil && !errors.Is(err, ErrNotRunning) {
panic(err)
}
}
case ch := <-b.subch:
// new subscription
sub := newSubscriber(b)
b.subscriptions[sub] = true
ch <- sub
case sub := <-b.unsubch:
// subscription closed
delete(b.subscriptions, sub)
}
}
for sub := range b.subscriptions {
sub.lc.ShutdownAsync(nil)
}
for len(b.subscriptions) > 0 {
sub := <-b.unsubch
delete(b.subscriptions, sub)
}
if b.parentch != nil {
b.parentch <- b
}
}
func newSubscriber(parent *bus) *bus {
// Re-use bus struct, but populate output channel (eventch)
// to enable subscriber mode.
evbuf := make([]Event, len(parent.evbuf))
copy(evbuf, parent.evbuf)
sub := &bus{
eventch: make(chan Event),
parentch: parent.unsubch,
evbuf: evbuf,
subscriptions: make(map[*bus]bool),
pubch: make(chan Event),
subch: make(chan chan<- Subscriber),
unsubch: make(chan *bus),
lc: lifecycle.New(),
}
go sub.run()
return sub
}