paste-bin/paste_bin/ratelimit.py
Jarian 985e070757 port: FastAPI rewrite built by the arch-llm spec pipeline
Spec-first port of the Flask app (still on main):
- schema.arch: signed arch-llm spec (single source of truth)
- paste_bin/: generated FastAPI backend, verified 51/51 frozen owner
  tests, 92.5% coverage (build r3, 2026-11-18)
- ui/: SPA + emitted OpenAPI contract (openapi.json + generated
  client.js); the app calls only window.PasteBinAPI
- 7 old bugs fixed (file raw 500, silent expiry, AVI-as-webp,
  stripped content, limiter leak, trusted mimetype, stale tmp)
- 62/62 live probe green; /health + JSON index added; JSON API

Full report on the wiki: 'FastAPI port (arch-llm pipeline)'.
2026-11-18 23:00:23 +00:00

26 lines
817 B
Python

import time
class UploadRateLimiter:
def __init__(self, max_attempts: int = 10, window_seconds: int = 60):
self.max_attempts = max_attempts
self.window_seconds = window_seconds
self.attempts: dict[str, list[float]] = {}
def allow(self, client_ip: str) -> bool:
now = time.monotonic()
cutoff = now - self.window_seconds
for ip in list(self.attempts):
ts = self.attempts[ip]
if not ts or ts[-1] < cutoff:
del self.attempts[ip]
ts = self.attempts.get(client_ip)
if ts is None:
self.attempts[client_ip] = [now]
return True
ts[:] = [t for t in ts if t >= cutoff]
if len(ts) < self.max_attempts:
ts.append(now)
return True
return False