forked from centrifugal/centrifugo
/
hubs.go
290 lines (243 loc) · 5.83 KB
/
hubs.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package libcentrifugo
import (
"sync"
"github.com/FZambia/go-logger"
)
// clientHub manages client connections.
type clientHub struct {
sync.RWMutex
// match ConnID with actual client connection.
conns map[ConnID]clientConn
// registry to hold active client connections grouped by UserID.
users map[UserID]map[ConnID]struct{}
// registry to hold active subscriptions of clients on channels.
subs map[ChannelID]map[ConnID]struct{}
}
// newClientHub initializes clientHub.
func newClientHub() *clientHub {
return &clientHub{
conns: make(map[ConnID]clientConn),
users: make(map[UserID]map[ConnID]struct{}),
subs: make(map[ChannelID]map[ConnID]struct{}),
}
}
// shutdown unsubscribes users from all channels and disconnects them.
func (h *clientHub) shutdown() {
var wg sync.WaitGroup
h.RLock()
for _, user := range h.users {
wg.Add(len(user))
for uid := range user {
cc, ok := h.conns[uid]
if !ok {
wg.Done()
continue
}
go func(cc clientConn) {
for _, ch := range cc.channels() {
cc.unsubscribe(ch)
}
cc.close("shutting down")
wg.Done()
}(cc)
}
}
h.RUnlock()
wg.Wait()
}
// add adds connection into clientHub connections registry.
func (h *clientHub) add(c clientConn) error {
h.Lock()
defer h.Unlock()
uid := c.uid()
user := c.user()
h.conns[uid] = c
_, ok := h.users[user]
if !ok {
h.users[user] = make(map[ConnID]struct{})
}
h.users[user][uid] = struct{}{}
return nil
}
// remove removes connection from clientHub connections registry.
func (h *clientHub) remove(c clientConn) error {
h.Lock()
defer h.Unlock()
uid := c.uid()
user := c.user()
delete(h.conns, uid)
// try to find connection to delete, return early if not found.
if _, ok := h.users[user]; !ok {
return nil
}
if _, ok := h.users[user][uid]; !ok {
return nil
}
// actually remove connection from hub.
delete(h.users[user], uid)
// clean up users map if it's needed.
if len(h.users[user]) == 0 {
delete(h.users, user)
}
return nil
}
// userConnections returns all connections of user with UserID in project.
func (h *clientHub) userConnections(user UserID) map[ConnID]clientConn {
h.RLock()
defer h.RUnlock()
userConnections, ok := h.users[user]
if !ok {
return map[ConnID]clientConn{}
}
var conns map[ConnID]clientConn
conns = make(map[ConnID]clientConn, len(userConnections))
for uid := range userConnections {
c, ok := h.conns[uid]
if !ok {
continue
}
conns[uid] = c
}
return conns
}
// addSub adds connection into clientHub subscriptions registry.
func (h *clientHub) addSub(chID ChannelID, c clientConn) (bool, error) {
h.Lock()
defer h.Unlock()
uid := c.uid()
h.conns[uid] = c
_, ok := h.subs[chID]
if !ok {
h.subs[chID] = make(map[ConnID]struct{})
}
h.subs[chID][uid] = struct{}{}
if !ok {
return true, nil
}
return false, nil
}
// removeSub removes connection from clientHub subscriptions registry.
func (h *clientHub) removeSub(chID ChannelID, c clientConn) (bool, error) {
h.Lock()
defer h.Unlock()
uid := c.uid()
// try to find subscription to delete, return early if not found.
if _, ok := h.subs[chID]; !ok {
return true, nil
}
if _, ok := h.subs[chID][uid]; !ok {
return true, nil
}
// actually remove subscription from hub.
delete(h.subs[chID], uid)
// clean up subs map if it's needed.
if len(h.subs[chID]) == 0 {
delete(h.subs, chID)
return true, nil
}
return false, nil
}
// broadcast sends message to all clients subscribed on channel.
func (h *clientHub) broadcast(chID ChannelID, message []byte) error {
h.RLock()
defer h.RUnlock()
// get connections currently subscribed on channel
channelSubscriptions, ok := h.subs[chID]
if !ok {
return nil
}
// iterate over them and send message individually
for uid := range channelSubscriptions {
c, ok := h.conns[uid]
if !ok {
continue
}
err := c.send(message)
if err != nil {
logger.ERROR.Println(err)
}
}
return nil
}
// nClients returns total number of client connections.
func (h *clientHub) nClients() int {
h.RLock()
defer h.RUnlock()
total := 0
for _, clientConnections := range h.users {
total += len(clientConnections)
}
return total
}
// nUniqueClients returns a number of unique users connected.
func (h *clientHub) nUniqueClients() int {
h.RLock()
defer h.RUnlock()
return len(h.users)
}
// nChannels returns a total number of different channels.
func (h *clientHub) nChannels() int {
h.RLock()
defer h.RUnlock()
return len(h.subs)
}
// channels returns a slice of all active channels.
func (h *clientHub) channels() []ChannelID {
h.RLock()
defer h.RUnlock()
channels := make([]ChannelID, len(h.subs))
i := 0
for ch := range h.subs {
channels[i] = ch
i++
}
return channels
}
// hasSubscribers returns whether or not there are any subscribers for a given channel.
func (h *clientHub) hasSubscribers(chID ChannelID) bool {
h.RLock()
defer h.RUnlock()
conns, ok := h.subs[chID]
if !ok {
return false
}
return len(conns) > 0
}
// adminHub manages admin connections from web interface.
type adminHub struct {
sync.RWMutex
// Registry to hold active admin connections.
connections map[ConnID]adminConn
}
// newAdminHub initializes new adminHub.
func newAdminHub() *adminHub {
return &adminHub{
connections: make(map[ConnID]adminConn),
}
}
// add adds connection to adminHub connections registry.
func (h *adminHub) add(c adminConn) error {
h.Lock()
defer h.Unlock()
h.connections[c.uid()] = c
return nil
}
// remove removes connection from adminHub connections registry.
func (h *adminHub) remove(c adminConn) error {
h.Lock()
defer h.Unlock()
delete(h.connections, c.uid())
return nil
}
// broadcast sends message to all connected admins.
func (h *adminHub) broadcast(message []byte) error {
h.RLock()
defer h.RUnlock()
for _, c := range h.connections {
err := c.send(message)
if err != nil {
logger.ERROR.Println(err)
}
}
return nil
}