Spaces:
Sleeping
Sleeping
File size: 500 Bytes
b2150c7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import time
from fastapi import Request, HTTPException
REQUEST_LOG = {}
RATE_LIMIT = 30
async def rate_limiter(request: Request):
ip = request.client.host
now = time.time()
REQUEST_LOG.setdefault(ip, [])
REQUEST_LOG[ip] = [t for t in REQUEST_LOG[ip] if now - t < 60]
if len(REQUEST_LOG[ip]) >= RATE_LIMIT:
raise HTTPException(
status_code=429,
detail = "Too many request, slow down"
)
REQUEST_LOG[ip].append(now) |