Spaces:
Running
Running
stabilize rate limit client identity
Browse files
api.py
CHANGED
|
@@ -41,7 +41,8 @@ def _rate_limit(request: Request) -> None:
|
|
| 41 |
if limit <= 0:
|
| 42 |
return
|
| 43 |
now = datetime.now(timezone.utc).timestamp()
|
| 44 |
-
|
|
|
|
| 45 |
with _rate_lock:
|
| 46 |
window = [stamp for stamp in _rate_windows.get(key, []) if now - stamp < 60]
|
| 47 |
if len(window) >= limit:
|
|
|
|
| 41 |
if limit <= 0:
|
| 42 |
return
|
| 43 |
now = datetime.now(timezone.utc).timestamp()
|
| 44 |
+
forwarded = request.headers.get("x-forwarded-for") or request.headers.get("x-real-ip")
|
| 45 |
+
key = (forwarded.split(",", 1)[0].strip() if forwarded else None) or "global"
|
| 46 |
with _rate_lock:
|
| 47 |
window = [stamp for stamp in _rate_windows.get(key, []) if now - stamp < 60]
|
| 48 |
if len(window) >= limit:
|