-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
presence_memory.go
151 lines (124 loc) · 3.62 KB
/
presence_memory.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
package centrifuge
import (
"context"
"sync"
)
// MemoryPresenceManager is builtin default PresenceManager which allows running
// Centrifuge-based server without any external storage. All data managed inside process
// memory.
//
// With this PresenceManager you can only run single Centrifuge node. If you need to scale
// you should consider using another PresenceManager implementation instead – for example
// RedisPresenceManager.
//
// Running single node can be sufficient for many use cases especially when you
// need maximum performance and not too many online clients. Consider configuring
// your load balancer to have one backup Centrifuge node for HA in this case.
type MemoryPresenceManager struct {
node *Node
config MemoryPresenceManagerConfig
presenceHub *presenceHub
}
var _ PresenceManager = (*MemoryPresenceManager)(nil)
// MemoryPresenceManagerConfig is a MemoryPresenceManager config.
type MemoryPresenceManagerConfig struct{}
// NewMemoryPresenceManager initializes MemoryPresenceManager.
func NewMemoryPresenceManager(n *Node, c MemoryPresenceManagerConfig) (*MemoryPresenceManager, error) {
return &MemoryPresenceManager{
node: n,
config: c,
presenceHub: newPresenceHub(),
}, nil
}
// AddPresence - see PresenceManager interface description.
func (m *MemoryPresenceManager) AddPresence(ch string, uid string, info *ClientInfo) error {
return m.presenceHub.add(ch, uid, info)
}
// RemovePresence - see PresenceManager interface description.
func (m *MemoryPresenceManager) RemovePresence(ch string, clientID string, _ string) error {
return m.presenceHub.remove(ch, clientID)
}
// Presence - see PresenceManager interface description.
func (m *MemoryPresenceManager) Presence(ch string) (map[string]*ClientInfo, error) {
return m.presenceHub.get(ch)
}
// PresenceStats - see PresenceManager interface description.
func (m *MemoryPresenceManager) PresenceStats(ch string) (PresenceStats, error) {
return m.presenceHub.getStats(ch)
}
// Close is noop for now.
func (m *MemoryPresenceManager) Close(_ context.Context) error {
return nil
}
type presenceHub struct {
sync.RWMutex
presence map[string]map[string]*ClientInfo
}
func newPresenceHub() *presenceHub {
return &presenceHub{
presence: make(map[string]map[string]*ClientInfo),
}
}
func (h *presenceHub) add(ch string, uid string, info *ClientInfo) error {
h.Lock()
defer h.Unlock()
_, ok := h.presence[ch]
if !ok {
h.presence[ch] = make(map[string]*ClientInfo)
}
h.presence[ch][uid] = info
return nil
}
func (h *presenceHub) remove(ch string, uid string) error {
h.Lock()
defer h.Unlock()
if _, ok := h.presence[ch]; !ok {
return nil
}
if _, ok := h.presence[ch][uid]; !ok {
return nil
}
delete(h.presence[ch], uid)
// clean up map if needed
if len(h.presence[ch]) == 0 {
delete(h.presence, ch)
}
return nil
}
func (h *presenceHub) get(ch string) (map[string]*ClientInfo, error) {
h.RLock()
defer h.RUnlock()
presence, ok := h.presence[ch]
if !ok {
// return empty map
return nil, nil
}
data := make(map[string]*ClientInfo, len(presence))
for k, v := range presence {
data[k] = v
}
return data, nil
}
func (h *presenceHub) getStats(ch string) (PresenceStats, error) {
h.RLock()
defer h.RUnlock()
presence, ok := h.presence[ch]
if !ok {
// return empty map
return PresenceStats{}, nil
}
numClients := len(presence)
numUsers := 0
uniqueUsers := map[string]struct{}{}
for _, info := range presence {
userID := info.UserID
if _, ok := uniqueUsers[userID]; !ok {
uniqueUsers[userID] = struct{}{}
numUsers++
}
}
return PresenceStats{
NumClients: numClients,
NumUsers: numUsers,
}, nil
}