-
Notifications
You must be signed in to change notification settings - Fork 1
/
ebus.go
213 lines (185 loc) · 3.99 KB
/
ebus.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
// Froxy - HTTP over SSH proxy
//
// Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
// See LICENSE for license terms and conditions
//
// Event bus
package main
import (
"reflect"
"sync"
"sync/atomic"
)
//
// The event
//
type Event uint
const (
EventStartup = Event(iota)
EventConnStateChanged
EventCountersChanged
EventServerParamsChanged
EventSitesChanged
EventKeysChanged
EventShutdownRequested
EventIpAddrChanged
)
//
// Event->string
//
func (e Event) String() string {
switch e {
case EventStartup:
return "EventStartup"
case EventConnStateChanged:
return "EventConnStateChanged"
case EventCountersChanged:
return "EventCountersChanged"
case EventServerParamsChanged:
return "EventServerParamsChanged"
case EventSitesChanged:
return "EventSitesChanged"
case EventKeysChanged:
return "EventKeysChanged"
case EventShutdownRequested:
return "EventShutdownRequested"
case EventIpAddrChanged:
return "EventIpAddrChanged"
}
panic("internal error")
}
//
// The event bus
//
type Ebus struct {
lock sync.Mutex // Access lock
subscribers map[<-chan Event]*subscriber // Table of subscribers
pending uint32 // Pending events
wake chan struct{} // Wake-up delivery goroutine
}
//
// The event subscriber
//
type subscriber struct {
out reflect.Value // reflect.ValueOf from subscriber's channel send end
mask uint32 // Mask of events the subscriber interested in
pending uint32 // Not delivered yet events
last Event // Last delivered event
}
//
// Create new event bus
//
func NewEbus() *Ebus {
ebus := &Ebus{
subscribers: make(map[<-chan Event]*subscriber),
wake: make(chan struct{}),
}
go ebus.goroutine()
return ebus
}
//
// Subscribe to events
// If no events are specified, subscriber will receive all events
//
func (ebus *Ebus) Sub(events ...Event) <-chan Event {
mask := ^uint32(1)
if len(events) != 0 {
mask = 0
for _, e := range events {
mask |= 1 << e
}
}
c := make(chan Event)
s := &subscriber{
out: reflect.ValueOf(c),
mask: mask,
}
ebus.lock.Lock()
ebus.subscribers[c] = s
ebus.lock.Unlock()
return c
}
//
// Cancel events subscription
//
func (ebus *Ebus) Unsub(c <-chan Event) {
ebus.lock.Lock()
delete(ebus.subscribers, c)
ebus.lock.Unlock()
}
//
// Raise an event
//
func (ebus *Ebus) Raise(e Event) {
bit := uint32(1 << e)
old := uint32(0)
// This is equivalent of atomic bitwise or:
// old, ebus.pending = ebus.pending, ebus.pending|bit
for ok := false; !ok; {
old = atomic.LoadUint32(&ebus.pending)
ok = atomic.CompareAndSwapUint32(&ebus.pending, old, old|bit)
}
if old == 0 {
ebus.wake <- struct{}{}
}
}
//
// Ebus goroutine
//
func (ebus *Ebus) goroutine() {
// Acquire the lock
ebus.lock.Lock()
defer ebus.lock.Unlock()
// Initialize buffers
cases := make([]reflect.SelectCase, 16)
backmap := make([]*subscriber, 16) // select case->subscriber
// The event loop
for {
// Refill array of reflect.Select() cases
cases = cases[:0]
backmap = backmap[:0]
cases = append(cases,
reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(ebus.wake),
})
backmap = append(backmap, nil)
for _, s := range ebus.subscribers {
if s.pending != 0 {
event := s.next()
cases = append(cases,
reflect.SelectCase{
Dir: reflect.SelectSend,
Chan: s.out,
Send: reflect.ValueOf(event),
})
backmap = append(backmap, s)
}
}
// Wait for something to happen
ebus.lock.Unlock()
choosen, _, _ := reflect.Select(cases)
ebus.lock.Lock()
// Dispatch the event
if choosen == 0 {
pending := atomic.SwapUint32(&ebus.pending, 0)
for _, s := range ebus.subscribers {
s.pending |= pending & s.mask
}
} else {
s := backmap[choosen]
s.pending &^= 1 << s.last
}
}
}
//
// Get next pending event
//
func (s *subscriber) next() Event {
e := Event(0)
for (s.pending & (1 << e)) == 0 {
e++
}
s.last = e
return e
}