-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager_internal.go
228 lines (198 loc) · 4.76 KB
/
manager_internal.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
package event
import (
"reflect"
"sort"
"sync"
"github.com/go-logr/logr"
)
// manager implements Manager interface.
type manager struct {
activeSubscribers sync.WaitGroup // Wait for all active subscribers
log logr.Logger
recoverPanic bool
mu sync.RWMutex // Protects following fields
subscribers map[Type]*subscriberList // Event type to subscribers
}
type subscriberList struct {
subs []*subscriber // Subscribers sorted by priority
wg sync.WaitGroup // Wait for active subscribers in list
}
// subscriber is a subscriber to an event.
type subscriber struct {
priority int // The higher the priority, the earlier the subscriber is called.
fn HandlerFunc // The event handler func.
}
func (m *manager) Wait(events ...Event) {
if len(events) == 0 {
m.activeSubscribers.Wait()
return
}
m.mu.RLock()
subs := m.subscribers
m.mu.RUnlock()
for _, event := range events {
eventType := typeOf(event)
list, ok := subs[eventType]
if ok {
list.wg.Wait()
}
}
}
func (m *manager) HasSubscriber(events ...Event) bool {
m.mu.RLock()
defer m.mu.RUnlock()
if len(events) == 0 {
return len(m.subscribers) != 0
}
if m.subscribers[anyType] != nil {
return true
}
for _, event := range events {
if m.subscribers[typeOf(event)] != nil {
return true
}
}
return false
}
func (m *manager) UnsubscribeAll(events ...Event) int {
m.mu.Lock()
defer m.mu.Unlock()
var count int
if len(events) == 0 {
for _, list := range m.subscribers {
count += len(list.subs)
}
m.subscribers = make(map[Type]*subscriberList)
return count
}
for _, event := range events {
eventType := typeOf(event)
list, ok := m.subscribers[eventType]
if !ok {
continue
}
count += len(list.subs)
delete(m.subscribers, eventType)
}
return count
}
func (m *manager) Subscribe(eventType Event, priority int, fn HandlerFunc) (unsubscribe func()) {
return m.subscribe(typeOf(eventType), priority, fn)
}
func (m *manager) subscribe(eventType Type, priority int, fn HandlerFunc) (unsubscribe func()) {
m.mu.Lock()
defer m.mu.Unlock()
sub := &subscriber{
priority: priority,
fn: fn,
}
// Get-add subscriber list for event type
list, ok := m.subscribers[eventType]
if ok {
list.subs = append(list.subs, sub)
// Sort subscribers by priority
sort.Slice(list.subs, func(i, j int) bool {
return list.subs[i].priority > list.subs[j].priority
})
} else {
m.subscribers[eventType] = &subscriberList{subs: []*subscriber{sub}}
}
// Unsubscribe func
var once sync.Once
return func() { once.Do(func() { m.unsubscribe(eventType, sub) }) }
}
func (m *manager) unsubscribe(eventType Type, sub *subscriber) {
m.mu.Lock()
defer m.mu.Unlock()
list, ok := m.subscribers[eventType]
if !ok {
return
}
if len(list.subs) == 1 {
delete(m.subscribers, eventType)
return
}
for i, s := range list.subs {
if s != sub { // Find by pointer
continue
}
// Delete subscriber from list while maintaining the order.
copy(list.subs[i:], list.subs[i+1:]) // Shift list[i+1:] left one index.
list.subs[len(list.subs)-1] = nil // Erase last element (write zero value).
list.subs = list.subs[:len(list.subs)-1]
return
}
}
func (m *manager) FireParallel(event Event, after ...HandlerFunc) {
m.activeSubscribers.Add(1)
go func() {
defer m.activeSubscribers.Done()
m.fire(event)
var i int
if m.recoverPanic {
defer func() {
if r := recover(); r != nil {
m.log.Error(nil,
"recovered from panic by an 'after fire' func",
"panic", r,
"eventType", typeOf(event),
"index", i)
}
}()
}
var fn HandlerFunc
for i, fn = range after {
fn(event)
}
}()
}
func (m *manager) Fire(event Event) {
m.activeSubscribers.Add(1)
defer m.activeSubscribers.Done()
m.fire(event)
}
var anyType = typeOf(any(nil))
func (m *manager) fire(event Event) {
eventType := typeOf(event)
m.mu.RLock()
list := m.subscribers[eventType]
anyList := m.subscribers[anyType]
m.mu.RUnlock()
m.fireSubscribers(event, anyList)
m.fireSubscribers(event, list)
}
func (m *manager) fireSubscribers(event Event, list *subscriberList) {
if list == nil {
return
}
list.wg.Add(1)
defer list.wg.Done()
for _, sub := range list.subs {
m.callSubscriber(sub, event)
}
}
func (m *manager) callSubscriber(sub *subscriber, event Event) {
if m.recoverPanic {
defer func() {
if r := recover(); r != nil {
m.log.Error(nil, "recovered from panic from an event subscriber",
"panic", r,
"eventType", typeOf(event),
"subscriberPriority", sub.priority)
}
}()
}
sub.fn(event)
}
// typeOf returns the reflect.Type of e.
func typeOf(e Event) (t Type) {
switch o := e.(type) {
case reflect.Type:
t = o
case reflect.Value:
t = o.Type()
default:
t = reflect.TypeOf(e)
}
return t
}