This commit is contained in:
2026-07-17 15:57:36 +03:00
commit 8f2d798e1a
72 changed files with 6227 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
from __future__ import annotations
import time
from collections import defaultdict, deque
from threading import Lock
class RateLimiter:
"""In-memory sliding window limiter (per process). Good enough for single-replica / dev."""
def __init__(self) -> None:
self._hits: dict[str, deque[float]] = defaultdict(deque)
self._lock = Lock()
def allow(self, key: str, limit: int, window_seconds: int = 60) -> bool:
if limit <= 0:
return True
now = time.monotonic()
with self._lock:
q = self._hits[key]
while q and now - q[0] > window_seconds:
q.popleft()
if len(q) >= limit:
return False
q.append(now)
return True
rate_limiter = RateLimiter()