-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrate_limits.py
More file actions
22 lines (20 loc) · 940 Bytes
/
Copy pathrate_limits.py
File metadata and controls
22 lines (20 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def classify_upstream_429(response) -> tuple[str, int | None, object]:
"""Preserve upstream limit semantics; a 429 is not evidence of an IP block."""
retry_after = response.headers.get("retry-after")
try:
retry_seconds = max(0, int(float(retry_after))) if retry_after else None
except (TypeError, ValueError):
retry_seconds = None
try:
payload = response.json()
except Exception:
payload = {"error": {"type": "upstream_rate_limit", "message": response.text}}
error = payload.get("error", {}) if isinstance(payload, dict) else {}
error_type = str(error.get("type", "upstream_rate_limit"))
if error_type in {"FreeUsageLimitError", "GoUsageLimitError", "BlackUsageLimitError"}:
category = "quota"
elif error_type == "RateLimitError":
category = "rate_limit"
else:
category = "upstream_rate_limit"
return category, retry_seconds, payload