optorigbench-tools / app /limits.py
imaan3's picture
Bench: loosen rate limits; fix T1 heading spacing
56ba4d7
Raw History Blame Contribute Delete
4.9 kB
"""Request limits for the launcher: who the client is behind the Space's proxy, sliding-window rate limits, and a
request body size cap.
"""
from __future__ import annotations
import ipaddress
import os
import threading
import time
from collections import deque
# The Space's proxy adds the address it received the request from to X-Forwarded-For; anything to the left of the
# entries the trusted proxies added was sent by the client and can be anything. PROXY_HOPS is how many entries, from
# the right, the trusted proxies add (1: the rightmost entry is the client as the edge saw it).
PROXY_HOPS = max(0, int(os.environ.get("PROXY_HOPS", "1")))
def _norm(value: str) -> str | None:
try:
ip = ipaddress.ip_address(value.strip())
except ValueError:
return None
if ip.version == 6: # one IPv6 visitor usually holds a whole /64
return str(ipaddress.ip_network(f"{ip}/64", strict=False))
return str(ip)
def client_ip(headers, peer: str | None) -> str:
"""The client's address: the PROXY_HOPS-th X-Forwarded-For entry from the right, else the socket peer."""
if PROXY_HOPS:
hops = [h.strip() for v in headers.getlist("x-forwarded-for") for h in v.split(",") if h.strip()]
if hops:
ip = _norm(hops[-PROXY_HOPS] if len(hops) >= PROXY_HOPS else hops[0])
if ip:
return ip
return (_norm(peer) if peer else None) or "unknown"
def xff_entries(headers) -> int:
"""How many X-Forwarded-For entries a request carried (logged once, so the owner can check PROXY_HOPS)."""
return sum(1 for v in headers.getlist("x-forwarded-for") for h in v.split(",") if h.strip())
class RateLimit:
"""At most `limit` events per `window_s` seconds per key (a sliding window)."""
MAX_KEYS = 20_000
def __init__(self, limit: int, window_s: float):
self.limit, self.window = int(limit), float(window_s)
self.hits: dict[str, deque] = {}
self.lock = threading.Lock()
def allow(self, key: str) -> bool:
now = time.monotonic()
with self.lock:
q = self.hits.get(key)
if q is None:
if len(self.hits) >= self.MAX_KEYS:
self._prune(now)
q = self.hits[key] = deque()
while q and now - q[0] > self.window:
q.popleft()
if len(q) >= self.limit:
return False
q.append(now)
return True
def retry_after(self, key: str) -> int:
with self.lock:
q = self.hits.get(key)
return max(1, int(self.window - (time.monotonic() - q[0])) + 1) if q else 1
def _prune(self, now: float):
for k in [k for k, q in self.hits.items() if not q or now - q[-1] > self.window]:
del self.hits[k]
while len(self.hits) >= self.MAX_KEYS: # still full: drop the oldest keys
del self.hits[next(iter(self.hits))]
class BodyLimit:
"""ASGI middleware: a request body over max_bytes gets 413, whether it declares its length or is chunked."""
def __init__(self, app, max_bytes: int):
self.app, self.max = app, int(max_bytes)
async def __call__(self, scope, receive, send):
if scope["type"] != "http":
return await self.app(scope, receive, send)
headers = dict(scope.get("headers") or [])
cl = headers.get(b"content-length")
if cl is not None:
try:
n = int(cl)
except ValueError:
return await self._reply(send, 400, b'{"error": "bad Content-Length"}')
if n > self.max:
return await self._reply(send, 413, self._msg())
body, more = bytearray(), True
while more:
msg = await receive()
if msg["type"] == "http.disconnect":
return
body += msg.get("body", b"")
more = msg.get("more_body", False)
if len(body) > self.max:
return await self._reply(send, 413, self._msg())
done = False
async def replay():
nonlocal done
if not done:
done = True
return {"type": "http.request", "body": bytes(body), "more_body": False}
return await receive()
return await self.app(scope, replay, send)
def _msg(self) -> bytes:
return b'{"error": "The request body is too large (at most %d KB)."}' % (self.max // 1024)
@staticmethod
async def _reply(send, status: int, body: bytes):
await send({"type": "http.response.start", "status": status,
"headers": [(b"content-type", b"application/json"), (b"content-length", str(len(body)).encode()),
(b"connection", b"close")]})
await send({"type": "http.response.body", "body": body})