This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
/
counter.go
148 lines (135 loc) · 3.72 KB
/
counter.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
package handler
import (
"context"
"fmt"
"strings"
"sync"
"time"
"github.com/go-redis/redis/v8"
)
type counter struct {
sync.RWMutex
redisClient *redis.Client
}
type listEntry struct {
Service string
Count int64
}
func (c *counter) incr(ctx context.Context, userID, path string, delta int64, t time.Time) (int64, error) {
t = t.UTC()
key := fmt.Sprintf("%s:%s:%s:%s", prefixCounter, userID, t.Format("20060102"), path)
pipe := c.redisClient.TxPipeline()
incr := pipe.IncrBy(ctx, key, delta)
pipe.Expire(ctx, key, counterTTL) // make sure we expire the counters
_, err := pipe.Exec(ctx)
if err != nil {
return 0, err
}
return incr.Result()
}
func (c *counter) incrMonthly(ctx context.Context, userID, path string, delta int64, t time.Time) (int64, error) {
t = t.UTC()
key := fmt.Sprintf("%s:%s:%s:%s", prefixCounter, userID, t.Format("200601"), path)
pipe := c.redisClient.TxPipeline()
incr := pipe.IncrBy(ctx, key, delta)
pipe.Expire(ctx, key, counterMonthlyTTL) // make sure we expire the counters
_, err := pipe.Exec(ctx)
if err != nil {
return 0, err
}
return incr.Result()
}
func (c *counter) decr(ctx context.Context, userID, path string, delta int64, t time.Time) (int64, error) {
t = t.UTC()
key := fmt.Sprintf("%s:%s:%s:%s", prefixCounter, userID, t.Format("20060102"), path)
pipe := c.redisClient.TxPipeline()
decr := pipe.DecrBy(ctx, key, delta)
pipe.Expire(ctx, key, counterTTL) // make sure we expire counters
_, err := pipe.Exec(ctx)
if err != nil {
return 0, err
}
return decr.Result()
}
func (c *counter) read(ctx context.Context, userID, path string, t time.Time) (int64, error) {
t = t.UTC()
ret, err := c.redisClient.Get(ctx, fmt.Sprintf("%s:%s:%s:%s", prefixCounter, userID, t.Format("20060102"), path)).Int64()
if err == redis.Nil {
return 0, nil
}
return ret, err
}
func (c *counter) readMonthly(ctx context.Context, userID, path string, t time.Time) (int64, error) {
t = t.UTC()
ret, err := c.redisClient.Get(ctx, fmt.Sprintf("%s:%s:%s:%s", prefixCounter, userID, t.Format("200601"), path)).Int64()
if err == redis.Nil {
return 0, nil
}
return ret, err
}
func (c *counter) deleteUser(ctx context.Context, userID string) error {
keys, err := c.redisClient.Keys(ctx, fmt.Sprintf("%s:%s:*", prefixCounter, userID)).Result()
if err != nil {
if err == redis.Nil {
return nil
}
return err
}
if len(keys) == 0 {
return nil
}
if err := c.redisClient.Del(ctx, keys...).Err(); err != nil && err != redis.Nil {
return err
}
return nil
}
func (c *counter) listForUser(userID string, t time.Time) ([]listEntry, error) {
ctx := context.Background()
keyPrefix := fmt.Sprintf("%s:%s:%s:", prefixCounter, userID, t.Format("20060102"))
sc := c.redisClient.Scan(ctx, 0, keyPrefix+"*", 500)
if err := sc.Err(); err != nil {
return nil, err
}
iter := sc.Iterator()
res := []listEntry{}
for {
if !iter.Next(ctx) {
break
}
key := iter.Val()
i, err := c.redisClient.Get(ctx, key).Int64()
if err != nil {
return nil, err
}
res = append(res, listEntry{
Service: strings.TrimPrefix(key, keyPrefix),
Count: i,
})
}
return res, iter.Err()
}
func (c *counter) listMonthliesForUser(userID string, t time.Time) ([]listEntry, error) {
ctx := context.Background()
keyPrefix := fmt.Sprintf("%s:%s:%s:", prefixCounter, userID, t.Format("200601"))
sc := c.redisClient.Scan(ctx, 0, keyPrefix+"*", 500)
if err := sc.Err(); err != nil {
return nil, err
}
iter := sc.Iterator()
res := []listEntry{}
for {
if !iter.Next(ctx) {
break
}
key := iter.Val()
i, err := c.redisClient.Get(ctx, key).Int64()
if err != nil {
return nil, err
}
res = append(res, listEntry{
Service: strings.TrimPrefix(key, keyPrefix),
Count: i,
})
}
return res, iter.Err()
}