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