-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage_redis_fixed_window.go
154 lines (127 loc) · 2.91 KB
/
storage_redis_fixed_window.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
package pacemaker
import (
"context"
"errors"
"strconv"
"time"
redis "github.com/go-redis/redis/v8"
)
type (
FixedWindowRedisStorageOpts struct {
Prefix string
}
FixedWindowIncArgs struct {
Window time.Time
TTL time.Duration
Tokens int64
Capacity int64
}
FixedWindowRedisStorage struct {
cli *redis.Client
opts FixedWindowRedisStorageOpts
keyGenerator func(time.Time) string
}
)
const (
keySep = "|"
script = `
local counter = tonumber(redis.call('GET', KEYS[1])) or 0
local tokens = tonumber(ARGV[1])
local capacity = tonumber(ARGV[2])
if counter + tokens <= capacity then
counter = tonumber(redis.call('INCRBY', KEYS[1], tokens))
else
counter = counter + tokens
end
redis.call('PEXPIRE', KEYS[1], ARGV[3])
return counter
`
)
var (
ScriptHash = Sha1Hash(script)
)
// Load will prepare this storage to be ready for usage, such as
// load into redis needed lua scripts. Calling to this method is not
// mandatory, but highly recommended.
func (s FixedWindowRedisStorage) Load(ctx context.Context) error {
if err := s.cli.ScriptLoad(ctx, script).Err(); err != nil {
return ErrCannotLoadScript
}
return nil
}
// Inc will increase, if there is room to, the rate limiting counter for the bucket
// specified by window argument.
func (s FixedWindowRedisStorage) Inc(
ctx context.Context,
args FixedWindowIncArgs,
) (counter int64, err error) {
key := s.keyGenerator(args.Window)
cmd := s.cli.EvalSha(
ctx,
ScriptHash,
[]string{key},
[]any{args.Tokens, args.Capacity, args.TTL.Milliseconds()},
)
if err = cmd.Err(); err != nil {
if errIsRedisNoScript(err) {
if err = s.cli.ScriptLoad(ctx, script).Err(); err != nil {
err = ErrCannotLoadScript
return
}
return s.Inc(ctx, args)
}
return
}
counter, err = cmd.Int64()
return
}
func (s FixedWindowRedisStorage) Keys(ctx context.Context) (res []string, err error) {
cmd := s.cli.Keys(ctx, s.opts.Prefix+"*")
if err = cmd.Err(); err != nil {
return nil, err
}
res, err = cmd.Result()
return
}
func (s FixedWindowRedisStorage) LastWindow(ctx context.Context) (ts time.Time, err error) {
var keys []string
keys, err = s.Keys(ctx)
if err != nil {
return
}
le := len(keys)
if le < 1 {
err = ErrNoLastKey
return
}
ts, err = LatestTsFromKeys(keys, keySep)
return
}
func (s FixedWindowRedisStorage) Get(
ctx context.Context,
window time.Time,
) (counter int64, err error) {
cmd := s.cli.Get(ctx, s.keyGenerator(window))
if err = cmd.Err(); err != nil {
// key does not exist
if errors.Is(err, redis.Nil) {
counter = 0
err = nil
}
return
}
counter, err = cmd.Int64()
return
}
func NewFixedWindowRedisStorage(
cli *redis.Client,
opts FixedWindowRedisStorageOpts,
) FixedWindowRedisStorage {
return FixedWindowRedisStorage{
cli: cli,
opts: opts,
keyGenerator: func(t time.Time) string {
return opts.Prefix + keySep + strconv.Itoa(int(t.UnixNano()))
},
}
}