-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient_store_channels_shard.go
81 lines (66 loc) · 1.64 KB
/
client_store_channels_shard.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
package wspubsub
import (
"sync"
)
type clientStoreChannelsShardBucket map[UUID]WebsocketClient
type clientStoreChannelsShard struct {
bucketSize int
mu sync.RWMutex
clients map[string]clientStoreChannelsShardBucket
}
func (s *clientStoreChannelsShard) Link(client WebsocketClient, channel string) {
s.mu.Lock()
if _, ok := s.clients[channel]; !ok {
s.clients[channel] = make(clientStoreChannelsShardBucket, s.bucketSize)
}
s.clients[channel][client.ID()] = client
s.mu.Unlock()
}
func (s *clientStoreChannelsShard) Unlink(clientID UUID, channels ...string) {
if len(channels) == 0 {
s.mu.Lock()
for channel := range s.clients {
delete(s.clients[channel], clientID)
}
s.mu.Unlock()
return
}
s.mu.Lock()
for _, channel := range channels {
delete(s.clients[channel], clientID)
}
s.mu.Unlock()
}
func (s *clientStoreChannelsShard) Count(channels ...string) int {
count := 0
if len(channels) == 0 {
s.mu.RLock()
for channel := range s.clients {
count += len(s.clients[channel])
}
s.mu.RUnlock()
return count
}
s.mu.RLock()
for _, channel := range channels {
count += len(s.clients[channel])
}
s.mu.RUnlock()
return count
}
func (s *clientStoreChannelsShard) Iterate(channel string, iterateFunc func(client WebsocketClient)) {
s.mu.RLock()
if clients, ok := s.clients[channel]; ok {
for _, client := range clients {
iterateFunc(client)
}
}
s.mu.RUnlock()
}
func newClientStoreChannelsShard(size int, bucketSize int) *clientStoreChannelsShard {
shard := &clientStoreChannelsShard{
bucketSize: bucketSize,
clients: make(map[string]clientStoreChannelsShardBucket, size),
}
return shard
}