24 lines
664 B
Python
24 lines
664 B
Python
from src.cache.redis_client import redis_client
|
|
from src.errors.http_errors.errors import Errors
|
|
|
|
|
|
class RateLimit:
|
|
def __init__(self) -> None:
|
|
self.errors=Errors()
|
|
|
|
async def rate_limit(self, ip:str)->None:
|
|
|
|
key=f"action attempt {ip}"
|
|
attempts = await redis_client.incrby(key)
|
|
|
|
if attempts == 1:
|
|
await redis_client.expire(key, 60)
|
|
|
|
if attempts>5:
|
|
raise self.errors.rate_limit_error(detail="too many attempts", retry_after=60)
|
|
|
|
async def check_rate_limit(self, client_ip:str) -> None:
|
|
await self.rate_limit(client_ip)
|
|
|
|
|
|
rate_limiter=RateLimit() |