Skip to content

Commit 2f5dba1

Browse files
authored
Create limits.py
1 parent 11822ba commit 2f5dba1

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

‎app/routes/limits.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Rate-limit dependency for the cost-incurring run endpoint (per user).
2+
3+
The per-IP auth limiter lives in ``auth.py`` instead — putting it here
4+
would form an import cycle (this module needs ``authenticate_user`` from
5+
``auth``). Both share the process-local ``limiter`` and raise
6+
``TooManyRequests`` (429 + Retry-After) when a window is exceeded.
7+
"""
8+
9+
from __future__ import annotations
10+
11+
from fastapi import Depends
12+
13+
from ..controllers.errors import TooManyRequests
14+
from ..infra.config import get_settings
15+
from ..infra.ratelimit import limiter
16+
from ..models import User
17+
from .auth import authenticate_user
18+
19+
20+
def limit_runs(user: User = Depends(authenticate_user)) -> None:
21+
"""Per-user limit on run submissions (each spends LLM tokens)."""
22+
s = get_settings()
23+
allowed, retry_after = limiter.check(
24+
f"runs:{user.id}", s.rate_limit_runs, s.rate_limit_window_s
25+
)
26+
if not allowed:
27+
secs = int(retry_after) + 1
28+
raise TooManyRequests(f"rate limit exceeded; retry in {secs}s", retry_after=secs)

0 commit comments

Comments
 (0)