Finance_AgenticRAG / api /rate_limiter.py
Atharva31's picture
Initial Commit
b2150c7
raw
history blame contribute delete
500 Bytes
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)