import time from fastapi import HTTPException # Request memory RATE_LOG = {} def rate_limiter(key: str, limit: int, window: int = 60): """ key = api key limit = requests allowed window = time window (seconds) """ now = int(time.time()) if key not in RATE_LOG: RATE_LOG[key] = [] # Remove old requests RATE_LOG[key] = [t for t in RATE_LOG[key] if now - t < window] if len(RATE_LOG[key]) >= limit: raise HTTPException( status_code=429, detail="Rate limit exceeded. Upgrade plan." ) RATE_LOG[key].append(now)