fastapi-limiter icon indicating copy to clipboard operation
fastapi-limiter copied to clipboard

What is the behavior if Redis is unavailable?

Open stephen-lazarionok opened this issue 11 months ago • 3 comments

stephen-lazarionok avatar Mar 06 '24 17:03 stephen-lazarionok

From experience, any calls to your endpoints fail

lib/python3.11/site-packages/redis/_parsers/base.py", line 221, in _readline
    raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)
redis.exceptions.ConnectionError: Connection closed by server.

zulrang avatar Mar 19 '24 22:03 zulrang

Is there any plan to fix this?

benglewis avatar Jun 27 '24 12:06 benglewis

what i did is just add an exception that return 0 for pexpire if redis is unavailable in depends:RateLimiter class:

    async def _check(self, key):
        redis = FastAPILimiter.redis
        try:
            pexpire = await redis.evalsha(
                FastAPILimiter.lua_sha, 1, key, str(self.times), str(self.milliseconds)
            )
        except:
            logger.exception("Redis is unavaible - ratelimiter disabled")
            return 0
        return pexpire

prolly not the best way, it was a 5minutes craft for when I have maintenance to do, hope that helps,

bashtian-fr avatar Sep 22 '24 22:09 bashtian-fr