forked from pewdiepie-archdaemon/odysseus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrate_limiter.py
More file actions
49 lines (42 loc) · 1.65 KB
/
Copy pathrate_limiter.py
File metadata and controls
49 lines (42 loc) · 1.65 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
# src/rate_limiter.py
"""Generic in-memory rate limiter — sliding window, keyed by IP."""
import threading
import time
from typing import Dict, List
class RateLimiter:
"""Sliding-window rate limiter.
Usage:
limiter = RateLimiter(max_requests=5, window_seconds=60)
if not limiter.check(ip):
raise HTTPException(429, "Too many requests")
"""
def __init__(self, max_requests: int, window_seconds: int):
self.max_requests = max_requests
self.window = window_seconds
self._log: Dict[str, List[float]] = {}
self._lock = threading.Lock()
self._last_cleanup = time.monotonic()
self._cleanup_interval = max(window_seconds * 2, 120)
def check(self, key: str) -> bool:
"""Return True if the request is allowed, False if rate-limited."""
now = time.monotonic()
with self._lock:
self._maybe_cleanup(now)
timestamps = self._log.get(key, [])
cutoff = now - self.window
timestamps = [t for t in timestamps if t > cutoff]
if len(timestamps) >= self.max_requests:
self._log[key] = timestamps
return False
timestamps.append(now)
self._log[key] = timestamps
return True
def _maybe_cleanup(self, now: float) -> None:
"""Periodically purge stale entries."""
if now - self._last_cleanup < self._cleanup_interval:
return
self._last_cleanup = now
cutoff = now - self.window
stale = [k for k, v in self._log.items() if not v or v[-1] <= cutoff]
for k in stale:
del self._log[k]