-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbroadcaster.go
More file actions
176 lines (139 loc) · 2.71 KB
/
broadcaster.go
File metadata and controls
176 lines (139 loc) · 2.71 KB
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
package worker
import (
"sync"
"sync/atomic"
)
// resultBroadcaster fans out results to multiple subscribers.
type resultBroadcaster struct {
input chan Result
inputMu sync.Mutex
mu sync.RWMutex
subs map[uint64]chan Result
nextID uint64
closed atomic.Bool
drops atomic.Int64
policy atomic.Uint32
}
func newResultBroadcaster(buffer int) *resultBroadcaster {
if buffer <= 0 {
buffer = DefaultMaxTasks
}
broadcaster := &resultBroadcaster{
input: make(chan Result, buffer),
subs: make(map[uint64]chan Result),
}
broadcaster.policy.Store(uint32(DropNewest))
go broadcaster.loop()
return broadcaster
}
// Subscribe returns a channel and an unsubscribe function.
func (b *resultBroadcaster) Subscribe(buffer int) (<-chan Result, func()) {
if buffer <= 0 {
buffer = 1
}
ch := make(chan Result, buffer)
b.mu.Lock()
if b.closed.Load() {
b.mu.Unlock()
close(ch)
return ch, func() {}
}
id := b.nextID
b.nextID++
b.subs[id] = ch
b.mu.Unlock()
unsubscribe := func() {
b.mu.Lock()
if existing, ok := b.subs[id]; ok {
delete(b.subs, id)
close(existing)
}
b.mu.Unlock()
}
return ch, unsubscribe
}
// Publish sends a result to subscribers.
func (b *resultBroadcaster) Publish(res Result) {
if b.closed.Load() {
return
}
b.inputMu.Lock()
defer b.inputMu.Unlock()
if b.closed.Load() {
return
}
b.input <- res
}
// Close shuts down the broadcaster and closes all subscriber channels.
func (b *resultBroadcaster) Close() {
if b.closed.Swap(true) {
return
}
b.inputMu.Lock()
close(b.input)
b.inputMu.Unlock()
}
func (b *resultBroadcaster) Drops() int64 {
return b.drops.Load()
}
func (b *resultBroadcaster) SetDropPolicy(policy ResultDropPolicy) {
switch policy {
case DropNewest, DropOldest:
default:
policy = DropNewest
}
b.policy.Store(uint32(policy))
}
func (b *resultBroadcaster) dropPolicy() ResultDropPolicy {
policy := b.policy.Load()
if policy > uint32(DropOldest) {
return DropNewest
}
return ResultDropPolicy(policy)
}
func (b *resultBroadcaster) loop() {
for res := range b.input {
policy := b.dropPolicy()
b.mu.RLock()
for _, ch := range b.subs {
if !b.deliver(policy, ch, res) {
b.drops.Add(1)
}
}
b.mu.RUnlock()
}
b.mu.Lock()
for id, ch := range b.subs {
close(ch)
delete(b.subs, id)
}
b.mu.Unlock()
}
func (*resultBroadcaster) deliver(policy ResultDropPolicy, ch chan Result, res Result) bool {
//nolint:exhaustive
switch policy {
case DropOldest:
select {
case ch <- res:
return true
default:
}
select {
case <-ch:
default:
}
select {
case ch <- res:
return true
default:
return false
}
default:
select {
case ch <- res:
return true
default:
return false
}
}
}