Sdm0 commited on
Commit
9ec682e
·
verified ·
1 Parent(s): 1dfb5e1

stabilize rate limit client identity

Browse files
Files changed (1) hide show
  1. api.py +2 -1
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
- key = request.client.host if request.client else "unknown"
 
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: