Files
romm/backend/utils/rate_limit.py
Georges-Antoine Assi 62ab9eb772 Tighten heartbeat rate limit comments and test assertions
Trim the constant comments down to the non-obvious facts, and relax the two
exact-TTL assertions that would flake if a second elapsed mid-test.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-04 08:49:26 -04:00

36 lines
1.1 KiB
Python

"""Fixed-window, Redis-backed request rate limiting."""
from fastapi import HTTPException, Request, status
from handler.redis_handler import sync_cache
def get_client_ip(request: Request) -> str:
return request.client.host if request.client else "unknown"
def enforce_rate_limit(
key: str, *, max_requests: int, window_seconds: int, detail: str
) -> None:
"""Count one call against a fixed window, raising 429 once the cap is passed.
Args:
key: Redis key the window is counted under, already scoped to whatever
identifies the caller (usually their IP).
max_requests: Calls allowed per window.
window_seconds: Window length.
detail: Message returned with the 429.
"""
pipe = sync_cache.pipeline()
pipe.incr(key)
# NX in the same transaction as the INCR: only the call that starts a window
# sets its TTL, and no counter can be left without one.
pipe.expire(key, window_seconds, nx=True)
count, _ = pipe.execute()
if count > max_requests:
raise HTTPException(
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
detail=detail,
)