-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathratelimit.go
More file actions
263 lines (227 loc) · 5.74 KB
/
Copy pathratelimit.go
File metadata and controls
263 lines (227 loc) · 5.74 KB
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
package auth
import (
"context"
"fmt"
"sync"
"time"
"github.com/redis/go-redis/v9"
)
/*
RateLimiterConfig holds the configuration for the sliding-window rate limiter.
MaxRequests is the maximum number of requests allowed within the Window duration.
*/
type RateLimiterConfig struct {
MaxRequests int
Window time.Duration
}
/*
RateLimiter implements an in-memory, per-key sliding-window rate limiter.
It is safe for concurrent use. Keys are typically user IDs, IP addresses, or API keys.
*/
type RateLimiter struct {
mu sync.Mutex
config RateLimiterConfig
buckets map[string][]time.Time
stopOnce sync.Once
done chan struct{}
}
/*
NewRateLimiter creates and returns a new RateLimiter with the given config.
It starts a background goroutine that periodically evicts stale entries
to prevent unbounded memory growth.
*/
func NewRateLimiter(cfg RateLimiterConfig) (*RateLimiter, error) {
if cfg.MaxRequests <= 0 {
return nil, ErrInvalidInput
}
if cfg.Window <= 0 {
return nil, ErrInvalidInput
}
rl := &RateLimiter{
config: cfg,
buckets: make(map[string][]time.Time),
done: make(chan struct{}),
}
go rl.cleanup()
return rl, nil
}
/*
Allow checks whether a request identified by key should be allowed.
It records the current timestamp and returns nil if the request is within
the configured limit, or ErrRateLimitExceeded otherwise.
*/
func (rl *RateLimiter) Allow(ctx context.Context, key string) error {
if key == "" {
return ErrEmptyInput
}
rl.mu.Lock()
defer rl.mu.Unlock()
now := time.Now()
cutoff := now.Add(-rl.config.Window)
/* Prune expired timestamps for this key */
timestamps := rl.buckets[key]
valid := timestamps[:0]
for _, ts := range timestamps {
if ts.After(cutoff) {
valid = append(valid, ts)
}
}
if len(valid) >= rl.config.MaxRequests {
rl.buckets[key] = valid
return ErrRateLimitExceeded
}
rl.buckets[key] = append(valid, now)
return nil
}
/*
Remaining returns how many requests the key has left in the current window.
*/
func (rl *RateLimiter) Remaining(ctx context.Context, key string) (int, error) {
rl.mu.Lock()
defer rl.mu.Unlock()
now := time.Now()
cutoff := now.Add(-rl.config.Window)
timestamps := rl.buckets[key]
count := 0
for _, ts := range timestamps {
if ts.After(cutoff) {
count++
}
}
remaining := rl.config.MaxRequests - count
if remaining < 0 {
return 0, nil
}
return remaining, nil
}
/*
Reset clears the rate limit state for a specific key.
Useful when a user successfully authenticates and you want to clear failed-attempt counters.
*/
func (rl *RateLimiter) Reset(ctx context.Context, key string) error {
rl.mu.Lock()
defer rl.mu.Unlock()
delete(rl.buckets, key)
return nil
}
/*
Stop shuts down the background cleanup goroutine.
Call this when the RateLimiter is no longer needed.
*/
func (rl *RateLimiter) Stop() {
rl.stopOnce.Do(func() {
close(rl.done)
})
}
/* cleanup periodically evicts expired timestamps to prevent memory leaks. */
func (rl *RateLimiter) cleanup() {
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
for {
select {
case <-ticker.C:
rl.cleanPass()
case <-rl.done:
return
}
}
}
func (rl *RateLimiter) cleanPass() {
rl.mu.Lock()
keys := make([]string, 0, len(rl.buckets))
for k := range rl.buckets {
keys = append(keys, k)
}
rl.mu.Unlock()
cutoff := time.Now().Add(-rl.config.Window)
for _, key := range keys {
rl.mu.Lock()
if timestamps, ok := rl.buckets[key]; ok {
valid := timestamps[:0]
for _, ts := range timestamps {
if ts.After(cutoff) {
valid = append(valid, ts)
}
}
if len(valid) == 0 {
delete(rl.buckets, key)
} else {
rl.buckets[key] = valid
}
}
rl.mu.Unlock()
}
}
/*
RedisRateLimiter implements a Redis-backed sliding-window rate limiter.
It relies on a pre-loaded Lua script managed by the Auth instance.
*/
type RedisRateLimiter struct {
client *redis.Client
config RateLimiterConfig
scriptSHA string
}
/*
NewRedisRateLimiter creates and returns a new RedisRateLimiter.
It requires an initialized Auth instance to use its Redis client and script SHA.
*/
func (a *Auth) NewRedisRateLimiter(cfg RateLimiterConfig) (*RedisRateLimiter, error) {
if cfg.MaxRequests <= 0 || cfg.Window <= 0 {
return nil, ErrInvalidInput
}
if a.redisClient == nil || a.rateLimitSHA == "" {
return nil, ErrRedisUnavailable
}
return &RedisRateLimiter{
client: a.redisClient,
config: cfg,
scriptSHA: a.rateLimitSHA,
}, nil
}
func (rl *RedisRateLimiter) Allow(ctx context.Context, key string) error {
if key == "" {
return ErrEmptyInput
}
now := time.Now()
windowMs := rl.config.Window.Milliseconds()
nowMs := now.UnixMilli()
member := now.UnixNano()
res, err := rl.client.EvalSha(ctx, rl.scriptSHA, []string{"ratelimit:" + key}, windowMs, rl.config.MaxRequests, nowMs, member).Result()
if err != nil {
return ErrRateLimitBackendDown
}
if allowed, ok := res.(int64); ok && allowed == 1 {
return nil
}
return ErrRateLimitExceeded
}
func (rl *RedisRateLimiter) Remaining(ctx context.Context, key string) (int, error) {
if key == "" {
return 0, ErrEmptyInput
}
now := time.Now()
cutoffMs := now.UnixMilli() - rl.config.Window.Milliseconds()
pipe := rl.client.Pipeline()
pipe.ZRemRangeByScore(ctx, "ratelimit:"+key, "0", fmt.Sprintf("%d", cutoffMs))
countCmd := pipe.ZCard(ctx, "ratelimit:"+key)
_, err := pipe.Exec(ctx)
if err != nil {
return 0, ErrRateLimitBackendDown
}
count := countCmd.Val()
remaining := rl.config.MaxRequests - int(count)
if remaining < 0 {
return 0, nil
}
return remaining, nil
}
func (rl *RedisRateLimiter) Reset(ctx context.Context, key string) error {
if key == "" {
return ErrEmptyInput
}
err := rl.client.Del(ctx, "ratelimit:"+key).Err()
if err != nil {
return ErrRateLimitBackendDown
}
return nil
}