-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
presence_redis.go
353 lines (297 loc) · 10.7 KB
/
presence_redis.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package centrifuge
import (
"context"
"errors"
"fmt"
"strconv"
"time"
_ "embed"
"github.com/centrifugal/centrifuge/internal/convert"
"github.com/centrifugal/protocol"
"github.com/redis/rueidis"
)
var _ PresenceManager = (*RedisPresenceManager)(nil)
// RedisPresenceManager keeps presence in Redis thus allows scaling nodes.
type RedisPresenceManager struct {
node *Node
config RedisPresenceManagerConfig
shards []*RedisShard
sharding bool
addPresenceScript *rueidis.Lua
remPresenceScript *rueidis.Lua
presenceScript *rueidis.Lua
presenceStatsScript *rueidis.Lua
}
// RedisPresenceManagerConfig is a config for RedisPresenceManager.
type RedisPresenceManagerConfig struct {
// Prefix to use before every channel name and key in Redis. By default,
// "centrifuge" prefix will be used.
Prefix string
// PresenceTTL is an interval how long to consider presence info
// valid after receiving presence update. This allows to automatically
// clean up unnecessary presence entries after TTL passed. Zero value
// means 60 seconds.
PresenceTTL time.Duration
// Shards is a slice of RedisShard to use. At least one shard must be provided.
// Data will be consistently sharded by channel over provided Redis shards.
Shards []*RedisShard
// EnableUserMapping when returns true tells RedisPresenceManager to additionally store
// user to num client connections hash map and sorted set with unique users in Redis.
// This increases Redis memory usage since additional structures are used, but provides
// a way to optimize presence stats retrieving as we can calculate stats quickly on
// Redis side instead of loading the entire presence information. By default, user mapping
// is not maintained.
EnableUserMapping func(channel string) bool
// UseHashFieldTTL allows using HEXPIRE command to set TTL for hash field. It's only available
// since Redis 7.4.0 thus disabled by default. Using hash field TTL can be useful to avoid
// maintaining expiration index in ZSET – so both useful from the throughput and memory usage
// perspective.
UseHashFieldTTL bool
}
var (
//go:embed internal/redis_lua/presence_add.lua
addPresenceScriptSource string
//go:embed internal/redis_lua/presence_rem.lua
remPresenceScriptSource string
//go:embed internal/redis_lua/presence_get.lua
presenceScriptSource string
//go:embed internal/redis_lua/presence_stats_get.lua
presenceStatsScriptSource string
)
// NewRedisPresenceManager creates new RedisPresenceManager.
func NewRedisPresenceManager(n *Node, config RedisPresenceManagerConfig) (*RedisPresenceManager, error) {
if len(config.Shards) == 0 {
return nil, errors.New("presence: no Redis shards provided in configuration")
}
if len(config.Shards) > 1 {
n.Log(NewLogEntry(LogLevelInfo, fmt.Sprintf("presence: Redis sharding enabled: %d shards", len(config.Shards))))
}
if config.Prefix == "" {
config.Prefix = "centrifuge"
}
if config.PresenceTTL == 0 {
config.PresenceTTL = 60 * time.Second
}
m := &RedisPresenceManager{
node: n,
shards: config.Shards,
config: config,
sharding: len(config.Shards) > 1,
addPresenceScript: rueidis.NewLuaScript(addPresenceScriptSource),
remPresenceScript: rueidis.NewLuaScript(remPresenceScriptSource),
presenceScript: rueidis.NewLuaScript(presenceScriptSource),
presenceStatsScript: rueidis.NewLuaScript(presenceStatsScriptSource),
}
return m, nil
}
func (m *RedisPresenceManager) Close(_ context.Context) error {
return nil
}
func (m *RedisPresenceManager) getShard(channel string) *RedisShard {
if !m.sharding {
return m.shards[0]
}
return m.shards[consistentIndex(channel, len(m.shards))]
}
// AddPresence - see PresenceManager interface description.
func (m *RedisPresenceManager) AddPresence(ch string, uid string, info *ClientInfo) error {
return m.addPresence(m.getShard(ch), ch, uid, info)
}
func (m *RedisPresenceManager) addPresenceScriptKeysArgs(s *RedisShard, ch string, uid string, info *ClientInfo) ([]string, []string, error) {
expire := int(m.config.PresenceTTL.Seconds())
infoBytes, err := infoToProto(info).MarshalVT()
if err != nil {
return nil, nil, err
}
setKey := m.presenceSetKey(s, ch)
hashKey := m.presenceHashKey(s, ch)
userSetKey := m.userSetKey(s, ch)
userHashKey := m.userHashKey(s, ch)
keys := []string{string(setKey), string(hashKey), string(userSetKey), string(userHashKey)}
expireAt := time.Now().Unix() + int64(expire)
useUserMapping := m.useUserMappingArg(ch)
args := []string{
strconv.Itoa(expire), strconv.FormatInt(expireAt, 10), uid,
convert.BytesToString(infoBytes), info.UserID, useUserMapping, m.useHashFieldTTLArg(),
}
return keys, args, nil
}
func (m *RedisPresenceManager) useUserMappingArg(ch string) string {
useUserMapping := "0"
if m.config.EnableUserMapping != nil && m.config.EnableUserMapping(ch) {
useUserMapping = "1"
}
return useUserMapping
}
func (m *RedisPresenceManager) useHashFieldTTLArg() string {
useHashFieldTTL := "0"
if m.config.UseHashFieldTTL {
useHashFieldTTL = "1"
}
return useHashFieldTTL
}
func (m *RedisPresenceManager) addPresence(s *RedisShard, ch string, uid string, info *ClientInfo) error {
keys, args, err := m.addPresenceScriptKeysArgs(s, ch, uid, info)
if err != nil {
return err
}
resp := m.addPresenceScript.Exec(context.Background(), s.client, keys, args)
if rueidis.IsRedisNil(resp.Error()) {
return nil
}
return resp.Error()
}
// RemovePresence - see PresenceManager interface description.
func (m *RedisPresenceManager) RemovePresence(ch string, clientID string, userID string) error {
return m.removePresence(m.getShard(ch), ch, clientID, userID)
}
func (m *RedisPresenceManager) removePresenceScriptKeysArgs(s *RedisShard, ch string, uid string, userID string) ([]string, []string, error) {
setKey := m.presenceSetKey(s, ch)
hashKey := m.presenceHashKey(s, ch)
userSetKey := m.userSetKey(s, ch)
userHashKey := m.userHashKey(s, ch)
keys := []string{string(setKey), string(hashKey), string(userSetKey), string(userHashKey)}
useUserMapping := m.useUserMappingArg(ch)
args := []string{uid, userID, useUserMapping, m.useHashFieldTTLArg()}
return keys, args, nil
}
func (m *RedisPresenceManager) removePresence(s *RedisShard, ch string, clientID string, userID string) error {
keys, args, err := m.removePresenceScriptKeysArgs(s, ch, clientID, userID)
if err != nil {
return err
}
resp := m.remPresenceScript.Exec(context.Background(), s.client, keys, args)
if rueidis.IsRedisNil(resp.Error()) {
return nil
}
return resp.Error()
}
// Presence - see PresenceManager interface description.
func (m *RedisPresenceManager) Presence(ch string) (map[string]*ClientInfo, error) {
return m.presence(m.getShard(ch), ch)
}
func (m *RedisPresenceManager) presenceScriptKeysArgs(s *RedisShard, ch string) ([]string, []string, error) {
setKey := m.presenceSetKey(s, ch)
hashKey := m.presenceHashKey(s, ch)
keys := []string{string(setKey), string(hashKey)}
now := int(time.Now().Unix())
args := []string{strconv.Itoa(now), m.useHashFieldTTLArg()}
return keys, args, nil
}
func (m *RedisPresenceManager) presenceStatsScriptKeysArgs(s *RedisShard, ch string) ([]string, []string, error) {
setKey := m.presenceSetKey(s, ch)
hashKey := m.presenceHashKey(s, ch)
userSetKey := m.userSetKey(s, ch)
userHashKey := m.userHashKey(s, ch)
keys := []string{string(setKey), string(hashKey), string(userSetKey), string(userHashKey)}
now := int(time.Now().Unix())
args := []string{strconv.Itoa(now), m.useHashFieldTTLArg()}
return keys, args, nil
}
func (m *RedisPresenceManager) presence(s *RedisShard, ch string) (map[string]*ClientInfo, error) {
keys, args, err := m.presenceScriptKeysArgs(s, ch)
if err != nil {
return nil, err
}
resp, err := m.presenceScript.Exec(context.Background(), s.client, keys, args).ToArray()
if err != nil {
return nil, err
}
return mapStringClientInfo(resp)
}
func mapStringClientInfo(result []rueidis.RedisMessage) (map[string]*ClientInfo, error) {
if len(result)%2 != 0 {
return nil, errors.New("mapStringClientInfo expects even number of values result")
}
m := make(map[string]*ClientInfo, len(result)/2)
for i := 0; i < len(result); i += 2 {
key, err := result[i].ToString()
if err != nil {
return nil, errors.New("key is not string")
}
value, err := result[i+1].ToString()
if err != nil {
return nil, errors.New("value is not string")
}
var f protocol.ClientInfo
err = f.UnmarshalVT(convert.StringToBytes(value))
if err != nil {
return nil, errors.New("can not unmarshal value to ClientInfo")
}
m[key] = infoFromProto(&f)
}
return m, nil
}
func (m *RedisPresenceManager) presenceStats(s *RedisShard, ch string) (PresenceStats, error) {
keys, args, err := m.presenceStatsScriptKeysArgs(s, ch)
if err != nil {
return PresenceStats{}, err
}
replies, err := m.presenceStatsScript.Exec(context.Background(), s.client, keys, args).ToArray()
if err != nil {
return PresenceStats{}, err
}
if len(replies) != 2 {
return PresenceStats{}, errors.New("wrong Redis reply: must have two values")
}
numClients, err := replies[0].AsInt64()
if err != nil {
return PresenceStats{}, errors.New("wrong Redis reply num clients")
}
numUsers, err := replies[1].AsInt64()
if err != nil {
return PresenceStats{}, errors.New("wrong Redis reply num users")
}
return PresenceStats{
NumClients: int(numClients),
NumUsers: int(numUsers),
}, nil
}
// PresenceStats - see PresenceManager interface description.
func (m *RedisPresenceManager) PresenceStats(ch string) (PresenceStats, error) {
if m.config.EnableUserMapping != nil && m.config.EnableUserMapping(ch) {
return m.presenceStats(m.getShard(ch), ch)
}
presence, err := m.Presence(ch)
if err != nil {
return PresenceStats{}, err
}
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
}
func (m *RedisPresenceManager) presenceHashKey(s *RedisShard, ch string) channelID {
if s.useCluster {
ch = "{" + ch + "}"
}
return channelID(m.config.Prefix + ".presence.data." + ch)
}
func (m *RedisPresenceManager) presenceSetKey(s *RedisShard, ch string) channelID {
if s.useCluster {
ch = "{" + ch + "}"
}
return channelID(m.config.Prefix + ".presence.expire." + ch)
}
func (m *RedisPresenceManager) userSetKey(s *RedisShard, ch string) channelID {
if s.useCluster {
ch = "{" + ch + "}"
}
return channelID(m.config.Prefix + ".presence.user.expire." + ch)
}
func (m *RedisPresenceManager) userHashKey(s *RedisShard, ch string) channelID {
if s.useCluster {
ch = "{" + ch + "}"
}
return channelID(m.config.Prefix + ".presence.user.clients." + ch)
}