-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhub.go
206 lines (171 loc) · 3.13 KB
/
hub.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
package hub
import (
"context"
"time"
)
type Hub struct {
pub chan interface{}
sub chan chan interface{}
unsub chan chan interface{}
done chan struct{}
stop chan struct{}
}
func msgUnref(any interface{}) {
if u, ok := any.(interface{ Unref() }); ok {
u.Unref()
}
}
func msgRefDelta(any interface{}, delta int64) {
if rd, ok := any.(interface{ RefDelta(int64) }); ok {
rd.RefDelta(delta)
}
}
func (h *Hub) Stop() { h.stop <- struct{}{} }
func (h *Hub) StopNonBlock() {
select {
case h.stop <- struct{}{}:
default:
}
}
func (h *Hub) StopWithContext(ctx context.Context) {
select {
case h.stop <- struct{}{}:
case <-ctx.Done():
}
}
func (h *Hub) Done() { <-h.done }
func (h *Hub) DoneWithContext(ctx context.Context) {
select {
case <-h.done:
case <-ctx.Done():
}
}
func (h *Hub) Start(ctx context.Context) {
if ctx == nil {
ctx = context.TODO()
}
defer func() {
select {
case h.done <- struct{}{}:
default:
}
}()
subs := map[chan interface{}]struct{}{}
fnBroadcast := func(msg interface{}) {
if len(subs) == 0 {
msgUnref(msg)
} else {
msgRefDelta(msg, int64(len(subs)))
for sub := range subs {
select {
case sub <- msg:
default:
//! TODO: drop and requeu
msgUnref(msg)
}
}
}
}
fnCloseSub := func(sub chan interface{}) {
for { // drain
select {
case msg := <-sub:
msgUnref(msg)
default:
close(sub)
return
}
}
}
fnClose := func() {
for sub := range subs {
fnCloseSub(sub)
}
}
for {
select {
case <-h.stop:
fnClose()
return
case <-ctx.Done():
fnClose()
return
case sub := <-h.sub:
subs[sub] = struct{}{}
case sub := <-h.unsub:
delete(subs, sub)
fnCloseSub(sub)
case msg := <-h.pub:
fnBroadcast(msg)
}
}
}
func (h *Hub) Sub(capacity int) (sub chan interface{}) {
if capacity <= 0 {
sub = make(chan interface{})
} else {
sub = make(chan interface{}, capacity)
}
h.sub <- sub
return sub
}
func (h *Hub) SubWithContext(ctx context.Context, capacity int) (sub chan interface{}) {
if capacity <= 0 {
sub = make(chan interface{})
} else {
sub = make(chan interface{}, capacity)
}
select {
case h.sub <- sub:
case <-ctx.Done():
}
return sub
}
func (h *Hub) Unsub(sub chan interface{}) {
h.unsub <- sub
}
func (h *Hub) UnsubNonBlock(sub chan interface{}) {
select {
case h.unsub <- sub:
default:
}
}
func (h *Hub) UnsubWithTimeout(sub chan interface{}, timeout time.Duration) {
timer := time.NewTimer(timeout)
defer func() {
if !timer.Stop() {
<-timer.C
}
}()
select {
case h.unsub <- sub:
case <-timer.C:
}
}
func (h *Hub) UnsubWithContext(ctx context.Context, sub chan interface{}) {
select {
case h.unsub <- sub:
case <-ctx.Done():
}
}
func (h *Hub) Pub(msg interface{}) {
h.pub <- msg
}
func (h *Hub) PubWithContext(ctx context.Context, msg interface{}) {
select {
case h.pub <- msg:
case <-ctx.Done():
}
}
func (h *Hub) Init() {
h.pub = make(chan interface{})
h.sub = make(chan chan interface{})
h.unsub = make(chan chan interface{})
h.stop = make(chan struct{}, 1)
h.done = make(chan struct{}, 1)
}
func NewHub() *Hub {
h := new(Hub)
h.Init()
return h
}