-
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathkeyed_hub.go
More file actions
241 lines (218 loc) · 5.35 KB
/
Copy pathkeyed_hub.go
File metadata and controls
241 lines (218 loc) · 5.35 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
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
package centrifuge
import (
"sync"
"github.com/centrifugal/protocol"
)
// keyedHub is a per-channel reverse index: key → set of subscriber clients.
// Used by shared poll subscriptions for per-key fan-out.
type keyedHub struct {
mu sync.RWMutex
items map[string]map[string]*Client // key → clientUID → *Client
}
func newKeyedHub() *keyedHub {
return &keyedHub{
items: make(map[string]map[string]*Client),
}
}
func (h *keyedHub) addSubscriber(key string, c *Client) {
h.mu.Lock()
subs, ok := h.items[key]
if !ok {
subs = make(map[string]*Client)
h.items[key] = subs
}
subs[c.uid] = c
h.mu.Unlock()
}
func (h *keyedHub) removeSubscriber(key string, c *Client) (keyEmpty bool) {
h.mu.Lock()
subs, ok := h.items[key]
if ok {
delete(subs, c.uid)
if len(subs) == 0 {
delete(h.items, key)
keyEmpty = true
}
}
h.mu.Unlock()
return keyEmpty
}
func (h *keyedHub) subscribers(key string) []*Client {
h.mu.RLock()
subs, ok := h.items[key]
if !ok {
h.mu.RUnlock()
return nil
}
targets := make([]*Client, 0, len(subs))
for _, c := range subs {
targets = append(targets, c)
}
h.mu.RUnlock()
return targets
}
func (h *keyedHub) allKeys() []string {
h.mu.RLock()
keys := make([]string, 0, len(h.items))
for k := range h.items {
keys = append(keys, k)
}
h.mu.RUnlock()
return keys
}
func (h *keyedHub) subscriberCount(key string) int {
h.mu.RLock()
count := len(h.items[key])
h.mu.RUnlock()
return count
}
func (h *keyedHub) hasSubscriber(key string, c *Client) bool {
h.mu.RLock()
subs, ok := h.items[key]
if !ok {
h.mu.RUnlock()
return false
}
_, has := subs[c.uid]
h.mu.RUnlock()
return has
}
func (h *keyedHub) removeAllSubscribers(key string) {
h.mu.Lock()
delete(h.items, key)
h.mu.Unlock()
}
func (h *keyedHub) numKeys() int {
h.mu.RLock()
n := len(h.items)
h.mu.RUnlock()
return n
}
// collectAllClients returns the deduplicated set of *Client refs subscribed
// to any key on this hub. Used to enumerate every client of a shared-poll
// channel — for epoch-flip-driven unsubscribe in particular.
//
// Holds h.mu.RLock during enumeration only; the returned slice is safe to
// iterate without holding hub or channel-state locks.
func (h *keyedHub) collectAllClients() []*Client {
h.mu.RLock()
if len(h.items) == 0 {
h.mu.RUnlock()
return nil
}
seen := make(map[string]*Client)
for _, subs := range h.items {
for uid, c := range subs {
if _, ok := seen[uid]; ok {
continue
}
seen[uid] = c
}
}
h.mu.RUnlock()
out := make([]*Client, 0, len(seen))
for _, c := range seen {
out = append(out, c)
}
return out
}
// broadcastToKey sends a publication to all subscribers of a key.
// Each subscriber's per-connection version is checked — only clients
// with a version lower than pubVersion receive the publication.
// Must be called WITHOUT holding sharedPollChannelState.mu.
func (h *keyedHub) broadcastToKey(channel string, key string, pubVersion uint64, pub *protocol.Publication, prep preparedData) {
targets := h.subscribers(key)
for _, c := range targets {
c.keyedWritePublication(channel, key, pubVersion, pub, prep)
}
}
// broadcastRemoval sends a removal publication to all subscribers of a key.
// Must be called WITHOUT holding sharedPollChannelState.mu.
func (h *keyedHub) broadcastRemoval(channel string, key string) {
targets := h.subscribers(key)
if len(targets) == 0 {
return
}
pub := &protocol.Publication{Key: key, Removed: true}
for _, c := range targets {
c.keyedWriteRemoval(channel, key, pub)
}
}
// broadcastRemovalToUsers sends removal publications only to connections
// belonging to the specified users (or excluding specified users).
func (h *keyedHub) broadcastRemovalToUsers(channel string, key string, users []string, excludeUsers []string) {
targets := h.subscribers(key)
if len(targets) == 0 {
return
}
var userSet map[string]struct{}
var excludeSet map[string]struct{}
if len(users) > 0 {
userSet = make(map[string]struct{}, len(users))
for _, u := range users {
userSet[u] = struct{}{}
}
}
if len(excludeUsers) > 0 {
excludeSet = make(map[string]struct{}, len(excludeUsers))
for _, u := range excludeUsers {
excludeSet[u] = struct{}{}
}
}
pub := &protocol.Publication{Key: key, Removed: true}
for _, c := range targets {
uid := c.UserID()
if userSet != nil {
if _, ok := userSet[uid]; !ok {
continue
}
}
if excludeSet != nil {
if _, ok := excludeSet[uid]; ok {
continue
}
}
c.keyedWriteRemoval(channel, key, pub)
}
}
// removeSubscribersForUsers removes subscribers matching user/exclude filters.
func (h *keyedHub) removeSubscribersForUsers(key string, users []string, excludeUsers []string) {
var userSet map[string]struct{}
var excludeSet map[string]struct{}
if len(users) > 0 {
userSet = make(map[string]struct{}, len(users))
for _, u := range users {
userSet[u] = struct{}{}
}
}
if len(excludeUsers) > 0 {
excludeSet = make(map[string]struct{}, len(excludeUsers))
for _, u := range excludeUsers {
excludeSet[u] = struct{}{}
}
}
h.mu.Lock()
subs, ok := h.items[key]
if !ok {
h.mu.Unlock()
return
}
for uid, c := range subs {
user := c.UserID()
if userSet != nil {
if _, ok := userSet[user]; !ok {
continue
}
}
if excludeSet != nil {
if _, ok := excludeSet[user]; ok {
continue
}
}
delete(subs, uid)
}
if len(subs) == 0 {
delete(h.items, key)
}
h.mu.Unlock()
}