laurentS / slowapi

A rate limiter for Starlette and FastAPI
https://pypi.org/project/slowapi/
MIT License
1.21k stars 76 forks source link

[QUESTION] redis asyncio-compatible? #130

Open larsclaussen opened 1 year ago

larsclaussen commented 1 year ago

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"}
laurentS commented 1 year ago

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: )

larsclaussen commented 1 year ago

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.