sharmaaryan commited on
Commit
283b271
Β·
1 Parent(s): 9ce68ca

guardrails: treat rate_limit_per_min<=0 as disabled, fix empty-deque IndexError

Browse files
Files changed (1) hide show
  1. backend/src/finrag/guardrails.py +6 -1
backend/src/finrag/guardrails.py CHANGED
@@ -91,11 +91,16 @@ def enforce(request: Request) -> None:
91
  )
92
 
93
  # ── per-IP sliding window ──
 
 
 
 
 
94
  bucket = _hits[ip]
95
  cutoff = now - _WINDOW_SECONDS
96
  while bucket and bucket[0] < cutoff:
97
  bucket.popleft()
98
- if len(bucket) >= settings.rate_limit_per_min:
99
  retry = max(1, int(_WINDOW_SECONDS - (now - bucket[0])))
100
  raise HTTPException(
101
  status_code=429,
 
91
  )
92
 
93
  # ── per-IP sliding window ──
94
+ # A limit <= 0 means "no per-IP cap" (the daily cap is the real circuit
95
+ # breaker). Guarding here also keeps bucket[0] below safe: we only reach
96
+ # it when limit > 0 and the bucket is already at/over that limit, so it
97
+ # can never be empty.
98
+ limit = settings.rate_limit_per_min
99
  bucket = _hits[ip]
100
  cutoff = now - _WINDOW_SECONDS
101
  while bucket and bucket[0] < cutoff:
102
  bucket.popleft()
103
+ if limit > 0 and len(bucket) >= limit:
104
  retry = max(1, int(_WINDOW_SECONDS - (now - bucket[0])))
105
  raise HTTPException(
106
  status_code=429,