-
Notifications
You must be signed in to change notification settings - Fork 10
/
httprateredis.go
237 lines (203 loc) · 5.8 KB
/
httprateredis.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
package httprateredis
import (
"context"
"fmt"
"strconv"
"sync/atomic"
"time"
"github.com/go-chi/httprate"
"github.com/redis/go-redis/v9"
)
func WithRedisLimitCounter(cfg *Config) httprate.Option {
if cfg.Disabled {
return httprate.WithNoop()
}
return httprate.WithLimitCounter(NewCounter(cfg))
}
func NewRedisLimitCounter(cfg *Config) (*redisCounter, error) {
c := NewCounter(cfg)
if err := c.client.Ping(context.Background()).Err(); err != nil {
return nil, fmt.Errorf("ping failed: %w", err)
}
return c, nil
}
func NewCounter(cfg *Config) *redisCounter {
if cfg == nil {
cfg = &Config{}
}
if cfg.Host == "" {
cfg.Host = "127.0.0.1"
}
if cfg.Port < 1 {
cfg.Port = 6379
}
if cfg.PrefixKey == "" {
cfg.PrefixKey = "httprate"
}
if cfg.FallbackTimeout == 0 {
if cfg.FallbackDisabled {
cfg.FallbackTimeout = time.Second
} else {
// Activate local in-memory fallback fairly quickly,
// so we don't slow down incoming requests too much.
cfg.FallbackTimeout = 250 * time.Millisecond
}
}
rc := &redisCounter{
prefixKey: cfg.PrefixKey,
onError: func(err error) {},
onFallback: func(activated bool) {},
}
if cfg.OnError != nil {
rc.onError = cfg.OnError
}
if !cfg.FallbackDisabled {
rc.fallbackCounter = httprate.NewLocalLimitCounter(cfg.WindowLength)
if cfg.OnFallbackChange != nil {
rc.onFallback = cfg.OnFallbackChange
}
}
if cfg.Client != nil {
rc.client = cfg.Client
} else {
maxIdle, maxActive := cfg.MaxIdle, cfg.MaxActive
if maxIdle < 1 {
maxIdle = 5
}
if maxActive < 1 {
maxActive = 10
}
rc.client = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
Password: cfg.Password,
DB: cfg.DBIndex,
ClientName: cfg.ClientName,
DisableIndentity: true,
DialTimeout: 2 * cfg.FallbackTimeout,
ReadTimeout: cfg.FallbackTimeout,
WriteTimeout: cfg.FallbackTimeout,
PoolSize: maxActive,
MinIdleConns: 1,
MaxIdleConns: maxIdle,
MaxRetries: -1, // -1 disables retries
})
}
return rc
}
type redisCounter struct {
client *redis.Client
windowLength time.Duration
prefixKey string
fallbackActivated atomic.Bool
fallbackCounter httprate.LimitCounter
onError func(err error)
onFallback func(activated bool)
}
var _ httprate.LimitCounter = (*redisCounter)(nil)
func (c *redisCounter) Config(requestLimit int, windowLength time.Duration) {
c.windowLength = windowLength
if c.fallbackCounter != nil {
c.fallbackCounter.Config(requestLimit, windowLength)
}
}
func (c *redisCounter) Increment(key string, currentWindow time.Time) error {
return c.IncrementBy(key, currentWindow, 1)
}
func (c *redisCounter) IncrementBy(key string, currentWindow time.Time, amount int) (err error) {
if c.fallbackCounter != nil {
if c.fallbackActivated.Load() {
return c.fallbackCounter.IncrementBy(key, currentWindow, amount)
}
defer func() {
if c.shouldFallback(err) {
err = c.fallbackCounter.IncrementBy(key, currentWindow, amount)
}
}()
}
// Note: Timeouts are set up directly on the Redis client.
ctx := context.Background()
hkey := c.limitCounterKey(key, currentWindow)
pipe := c.client.TxPipeline()
incrCmd := pipe.IncrBy(ctx, hkey, int64(amount))
expireCmd := pipe.Expire(ctx, hkey, c.windowLength*3)
_, err = pipe.Exec(ctx)
if err != nil {
return fmt.Errorf("httprateredis: redis transaction failed: %w", err)
}
if err := incrCmd.Err(); err != nil {
return fmt.Errorf("httprateredis: redis incr failed: %w", err)
}
if err := expireCmd.Err(); err != nil {
return fmt.Errorf("httprateredis: redis expire failed: %w", err)
}
return nil
}
func (c *redisCounter) Get(key string, currentWindow, previousWindow time.Time) (curr int, prev int, err error) {
if c.fallbackCounter != nil {
if c.fallbackActivated.Load() {
return c.fallbackCounter.Get(key, currentWindow, previousWindow)
}
defer func() {
if c.shouldFallback(err) {
curr, prev, err = c.fallbackCounter.Get(key, currentWindow, previousWindow)
}
}()
}
// Note: Timeouts are set up directly on the Redis client.
ctx := context.Background()
currKey := c.limitCounterKey(key, currentWindow)
prevKey := c.limitCounterKey(key, previousWindow)
values, err := c.client.MGet(ctx, currKey, prevKey).Result()
if err != nil {
return 0, 0, fmt.Errorf("httprateredis: redis mget failed: %w", err)
} else if len(values) != 2 {
return 0, 0, fmt.Errorf("httprateredis: redis mget returned wrong number of keys: %v, expected 2", len(values))
}
// MGET always returns slice with nil or "string" values, even if the values
// were created with the INCR command. Ignore error if we can't parse the number.
if values[0] != nil {
v, _ := values[0].(string)
curr, _ = strconv.Atoi(v)
}
if values[1] != nil {
v, _ := values[1].(string)
prev, _ = strconv.Atoi(v)
}
return curr, prev, nil
}
func (c *redisCounter) IsFallbackActivated() bool {
return c.fallbackActivated.Load()
}
func (c *redisCounter) Close() error {
return c.client.Close()
}
func (c *redisCounter) shouldFallback(err error) bool {
if err == nil {
return false
}
c.onError(err)
// Activate the local in-memory counter fallback, unless activated by some other goroutine.
alreadyActivated := c.fallbackActivated.Swap(true)
if !alreadyActivated {
c.onFallback(true)
go c.reconnect()
}
return true
}
func (c *redisCounter) reconnect() {
// Try to re-connect to redis every 200ms.
for {
time.Sleep(200 * time.Millisecond)
err := c.client.Ping(context.Background()).Err()
if err == nil {
c.fallbackActivated.Store(false)
if c.onFallback != nil {
c.onFallback(false)
}
return
}
}
}
func (c *redisCounter) limitCounterKey(key string, window time.Time) string {
return fmt.Sprintf("%s:%d", c.prefixKey, httprate.LimitCounterKey(key, window))
}