slowapi icon indicating copy to clipboard operation
slowapi copied to clipboard

[QUESTION] redis asyncio-compatible?

Open larsclaussen opened this issue 1 year ago • 2 comments

The docs state

Use redis as backend for the limiter limiter = Limiter(key_func=get_remote_address, storage_uri="redis://:/n")

The limits async docs, however, say that you have to construct the uri like so

from limits.storage import storage_from_string
redis = storage_from_string("async+redis://localhost:6379")

I can't really tell if the implementation is asyncio-compatible currently, that is, changing the uri to async+redis will make it possible to decorate my async endpoint without blockking the event loop.

    @app.get("/mars")
    @limiter.limit("5/minute")
    async def homepage(request: Request, response: Response):
        return {"key": "value"}

larsclaussen avatar Mar 06 '23 10:03 larsclaussen

Hi @larsclaussen at the moment, the implementation is fully sync, so even if the async backend url works, slowapi won't be able to fully take advantage of it at the moment. The code you're after is probably this line. This is the oldest issue with the code #3 and nobody's gotten around to tackling it entirely yet (including myself :sweat_smile: )

laurentS avatar Mar 06 '23 12:03 laurentS

Thanks for the clarification. Maybe a good idea to state that clearly in the readme. It is misleading because most examples are async examples and using a backend like redis (or any other of the backends stated) will result a in the whole event loop being blocked with every limit check.

larsclaussen avatar Mar 08 '23 15:03 larsclaussen