-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis_rate_limiter.py
More file actions
394 lines (312 loc) · 12.3 KB
/
redis_rate_limiter.py
File metadata and controls
394 lines (312 loc) · 12.3 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
"""
Distributed Rate Limiting using Redis
Implements rate limiting algorithms that work across multiple application instances.
Uses Redis for shared state and Lua scripts for atomic operations.
"""
import time
import redis
from typing import Optional, Dict
from dataclasses import dataclass
@dataclass
class RateLimitResult:
allowed: bool
limit: int
remaining: int
reset_at: float
retry_after: Optional[float] = None
class RedisRateLimiter:
"""Base class for Redis-based rate limiters"""
def __init__(self, redis_client: redis.Redis, key_prefix: str = "rate_limit"):
self.redis = redis_client
self.key_prefix = key_prefix
def _make_key(self, identifier: str, suffix: str = "") -> str:
"""Create Redis key for identifier"""
key = f"{self.key_prefix}:{identifier}"
if suffix:
key = f"{key}:{suffix}"
return key
class RedisTokenBucket(RedisRateLimiter):
"""
Token Bucket implementation using Redis
Uses Lua script for atomic operations.
"""
# Lua script for atomic token bucket operations
TOKEN_BUCKET_SCRIPT = """
local key = KEYS[1]
local capacity = tonumber(ARGV[1])
local refill_rate = tonumber(ARGV[2])
local tokens_requested = tonumber(ARGV[3])
local now = tonumber(ARGV[4])
-- Get current state
local bucket = redis.call('HMGET', key, 'tokens', 'last_refill')
local tokens = tonumber(bucket[1]) or capacity
local last_refill = tonumber(bucket[2]) or now
-- Refill tokens
local elapsed = math.max(0, now - last_refill)
local tokens_to_add = elapsed * refill_rate
tokens = math.min(capacity, tokens + tokens_to_add)
-- Try to consume
local allowed = 0
if tokens >= tokens_requested then
tokens = tokens - tokens_requested
allowed = 1
end
-- Update state
redis.call('HMSET', key, 'tokens', tokens, 'last_refill', now)
redis.call('EXPIRE', key, 3600) -- 1 hour expiry
-- Calculate retry_after if rejected
local retry_after = 0
if allowed == 0 then
local tokens_needed = tokens_requested - tokens
retry_after = tokens_needed / refill_rate
end
return {allowed, math.floor(tokens), retry_after}
"""
def __init__(self, redis_client: redis.Redis, capacity: int, refill_rate: float):
super().__init__(redis_client)
self.capacity = capacity
self.refill_rate = refill_rate
self.script = self.redis.register_script(self.TOKEN_BUCKET_SCRIPT)
def allow_request(self, identifier: str, tokens: int = 1) -> RateLimitResult:
"""Check if request is allowed for identifier"""
key = self._make_key(identifier)
now = time.time()
result = self.script(
keys=[key],
args=[self.capacity, self.refill_rate, tokens, now]
)
allowed, remaining, retry_after = result
return RateLimitResult(
allowed=bool(allowed),
limit=self.capacity,
remaining=int(remaining),
reset_at=now + (self.capacity - remaining) / self.refill_rate if remaining < self.capacity else now,
retry_after=float(retry_after) if retry_after > 0 else None
)
class RedisFixedWindow(RedisRateLimiter):
"""
Fixed Window Counter using Redis
Simple and efficient implementation.
"""
def __init__(self, redis_client: redis.Redis, window_size: int, limit: int):
super().__init__(redis_client)
self.window_size = window_size
self.limit = limit
def allow_request(self, identifier: str, tokens: int = 1) -> RateLimitResult:
"""Check if request is allowed"""
now = time.time()
window_start = int(now // self.window_size) * self.window_size
key = self._make_key(identifier, str(window_start))
pipe = self.redis.pipeline()
pipe.get(key)
pipe.expire(key, self.window_size * 2)
current_count = pipe.execute()[0]
current_count = int(current_count) if current_count else 0
if current_count + tokens <= self.limit:
self.redis.incrby(key, tokens)
return RateLimitResult(
allowed=True,
limit=self.limit,
remaining=self.limit - current_count - tokens,
reset_at=window_start + self.window_size
)
else:
return RateLimitResult(
allowed=False,
limit=self.limit,
remaining=0,
reset_at=window_start + self.window_size,
retry_after=window_start + self.window_size - now
)
class RedisSlidingWindowCounter(RedisRateLimiter):
"""
Sliding Window Counter using Redis
Most practical for production use.
"""
SLIDING_WINDOW_SCRIPT = """
local current_key = KEYS[1]
local previous_key = KEYS[2]
local window_size = tonumber(ARGV[1])
local limit = tonumber(ARGV[2])
local tokens = tonumber(ARGV[3])
local now = tonumber(ARGV[4])
local window_start = tonumber(ARGV[5])
-- Get counts
local current = tonumber(redis.call('GET', current_key) or 0)
local previous = tonumber(redis.call('GET', previous_key) or 0)
-- Calculate estimated count
local elapsed = now - window_start
local overlap_pct = (window_size - elapsed) / window_size
local estimated_count = math.floor(previous * overlap_pct) + current
local allowed = 0
local remaining = limit
if estimated_count + tokens <= limit then
-- Increment and set expiry
redis.call('INCRBY', current_key, tokens)
redis.call('EXPIRE', current_key, window_size * 2)
allowed = 1
remaining = limit - estimated_count - tokens
else
remaining = 0
end
return {allowed, remaining, estimated_count}
"""
def __init__(self, redis_client: redis.Redis, window_size: int, limit: int):
super().__init__(redis_client)
self.window_size = window_size
self.limit = limit
self.script = self.redis.register_script(self.SLIDING_WINDOW_SCRIPT)
def allow_request(self, identifier: str, tokens: int = 1) -> RateLimitResult:
"""Check if request is allowed"""
now = time.time()
window_start = int(now // self.window_size) * self.window_size
prev_window_start = window_start - self.window_size
current_key = self._make_key(identifier, str(window_start))
previous_key = self._make_key(identifier, str(prev_window_start))
result = self.script(
keys=[current_key, previous_key],
args=[self.window_size, self.limit, tokens, now, window_start]
)
allowed, remaining, estimated_count = result
return RateLimitResult(
allowed=bool(allowed),
limit=self.limit,
remaining=max(0, int(remaining)),
reset_at=window_start + self.window_size,
retry_after=window_start + self.window_size - now if not allowed else None
)
class RedisSlidingWindowLog(RedisRateLimiter):
"""
Sliding Window Log using Redis Sorted Set
Most accurate but memory intensive.
"""
def __init__(self, redis_client: redis.Redis, window_size: int, limit: int):
super().__init__(redis_client)
self.window_size = window_size
self.limit = limit
def allow_request(self, identifier: str, tokens: int = 1) -> RateLimitResult:
"""Check if request is allowed"""
now = time.time()
key = self._make_key(identifier)
cutoff = now - self.window_size
pipe = self.redis.pipeline()
# Remove old entries
pipe.zremrangebyscore(key, 0, cutoff)
# Count current entries
pipe.zcard(key)
results = pipe.execute()
current_count = results[1]
if current_count + tokens <= self.limit:
# Add new entries
pipe = self.redis.pipeline()
for i in range(tokens):
pipe.zadd(key, {f"{now}:{i}": now})
pipe.expire(key, self.window_size)
pipe.execute()
return RateLimitResult(
allowed=True,
limit=self.limit,
remaining=self.limit - current_count - tokens,
reset_at=now + self.window_size
)
else:
# Get oldest entry to calculate retry_after
oldest = self.redis.zrange(key, 0, 0, withscores=True)
retry_after = (oldest[0][1] + self.window_size - now) if oldest else 0
return RateLimitResult(
allowed=False,
limit=self.limit,
remaining=0,
reset_at=now + retry_after,
retry_after=max(0, retry_after)
)
class RedisRateLimiterFactory:
"""Factory for creating Redis-based rate limiters"""
@staticmethod
def create_token_bucket(
redis_url: str,
capacity: int,
refill_rate: float
) -> RedisTokenBucket:
client = redis.from_url(redis_url)
return RedisTokenBucket(client, capacity, refill_rate)
@staticmethod
def create_fixed_window(
redis_url: str,
window_size: int,
limit: int
) -> RedisFixedWindow:
client = redis.from_url(redis_url)
return RedisFixedWindow(client, window_size, limit)
@staticmethod
def create_sliding_window_counter(
redis_url: str,
window_size: int,
limit: int
) -> RedisSlidingWindowCounter:
client = redis.from_url(redis_url)
return RedisSlidingWindowCounter(client, window_size, limit)
@staticmethod
def create_sliding_window_log(
redis_url: str,
window_size: int,
limit: int
) -> RedisSlidingWindowLog:
client = redis.from_url(redis_url)
return RedisSlidingWindowLog(client, window_size, limit)
# Decorator for easy rate limiting
def rate_limit(
limiter: RedisRateLimiter,
get_identifier=lambda: "default"
):
"""
Decorator to rate limit function calls
Usage:
@rate_limit(limiter, get_identifier=lambda: request.user.id)
def api_endpoint():
...
"""
def decorator(func):
def wrapper(*args, **kwargs):
identifier = get_identifier()
result = limiter.allow_request(identifier)
if not result.allowed:
raise RateLimitExceeded(
f"Rate limit exceeded. Retry after {result.retry_after}s",
result
)
return func(*args, **kwargs)
return wrapper
return decorator
class RateLimitExceeded(Exception):
"""Exception raised when rate limit is exceeded"""
def __init__(self, message: str, result: RateLimitResult):
super().__init__(message)
self.result = result
# Example usage
if __name__ == "__main__":
# Connect to Redis
redis_client = redis.Redis(host='localhost', port=6379, decode_responses=True)
print("Redis-based Rate Limiting Examples")
print("=" * 50)
# Example 1: Token Bucket
print("\n1. Token Bucket (capacity=10, rate=2/sec)")
tb = RedisTokenBucket(redis_client, capacity=10, refill_rate=2)
for i in range(12):
result = tb.allow_request("user:123")
print(f"Request {i+1}: {'✓' if result.allowed else '✗'} "
f"(remaining: {result.remaining})")
# Example 2: Sliding Window Counter
print("\n2. Sliding Window Counter (60s window, 10 req)")
swc = RedisSlidingWindowCounter(redis_client, window_size=60, limit=10)
for i in range(12):
result = swc.allow_request("user:456")
print(f"Request {i+1}: {'✓' if result.allowed else '✗'} "
f"(remaining: {result.remaining})")
# Example 3: Multiple identifiers
print("\n3. Multiple Users")
fw = RedisFixedWindow(redis_client, window_size=60, limit=5)
users = ["alice", "bob", "charlie"]
for user in users:
result = fw.allow_request(f"user:{user}")
print(f"User {user}: {'✓' if result.allowed else '✗'}")