-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_online_storage.go
137 lines (107 loc) · 2.85 KB
/
set_online_storage.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
package research_online_redis_go
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/redis/go-redis/v9"
)
// 10 minutes
const defaultOnlineSetGroup = 600
type SetOnlineStorage struct {
client *redis.Client
group int64
}
func NewSetOnlineStorage(client *redis.Client, group int64) *SetOnlineStorage {
if group == 0 {
group = defaultOnlineSetGroup
}
return &SetOnlineStorage{client: client, group: group}
}
func (s *SetOnlineStorage) Store(ctx context.Context, pair UserOnlinePair) error {
return s.client.SAdd(ctx, s.key(pair.Timestamp), pair.UserID).Err()
}
func (s *SetOnlineStorage) BatchStore(ctx context.Context, pairs []UserOnlinePair) error {
groups := make(map[int64][]interface{}, 1)
for _, pair := range pairs {
round := s.round(pair.Timestamp)
groups[round] = append(groups[round], pair.UserID)
}
var err error
for timestamp, userIDs := range groups {
redisErr := s.client.SAdd(ctx, s.key(timestamp), userIDs).Err()
if redisErr != nil {
err = multierror.Append(err, redisErr)
}
}
return err
}
func (s *SetOnlineStorage) Count(ctx context.Context) (int64, error) {
keys, err := s.keys(ctx)
if err != nil {
return 0, err
}
count := int64(0)
for _, key := range keys {
groupCount, err := s.client.SCard(ctx, key).Result()
if err != nil {
return 0, err
}
count += groupCount
}
return count, nil
}
func (s *SetOnlineStorage) GetAndClear(ctx context.Context) ([]UserOnlinePair, error) {
keys, err := s.keys(ctx)
if err != nil {
return nil, err
}
var result []UserOnlinePair
for _, key := range keys {
oldKey := key
newKey := "s:online:tmp"
err := s.client.Rename(ctx, oldKey, newKey).Err()
if err != nil {
return result, err
}
timestamp, err := s.parseKey(oldKey)
if err != nil {
return result, err
}
userIDs, err := s.client.SMembers(ctx, newKey).Result()
if err != nil {
return result, err
}
for _, stringUserID := range userIDs {
userID, err := strconv.ParseInt(stringUserID, 10, 64)
if err != nil {
// unreachable, ignore for article
// logging or use https://github.com/hashicorp/go-multierror
// just in case
continue
}
result = append(result, UserOnlinePair{
UserID: userID,
Timestamp: timestamp,
})
}
}
return result, nil
}
func (s *SetOnlineStorage) keys(ctx context.Context) ([]string, error) {
return s.client.Keys(ctx, "s:online:main:*").Result()
}
func (s *SetOnlineStorage) key(timestamp int64) string {
return fmt.Sprintf("s:online:main:%d", s.round(timestamp))
}
func (s *SetOnlineStorage) round(timestamp int64) int64 {
return (timestamp / s.group) * s.group
}
func (s *SetOnlineStorage) parseKey(key string) (timestamp int64, err error) {
_, err = fmt.Fscanf(strings.NewReader(key), "s:online:main:%d", ×tamp)
if err != nil {
return 0, err
}
return timestamp, nil
}