slowapi icon indicating copy to clipboard operation
slowapi copied to clipboard

Support for aioredis

Open thearchitector opened this issue 2 years ago • 5 comments

Since FastAPI and Starlette are ASGI and are built on asyncio, it would be nice for slowapi to support aioredis as a storage backend. I imagine that it might require some playing around with the limits library, but would be a cool feature.

thearchitector avatar Jul 20 '21 19:07 thearchitector

@thearchitector thanks for opening this issue. I think it relates to #3 What you're suggesting makes complete sense in my mind, I initially wanted to make slowapi fully async, just haven't found the time to do so. There's a PR on limits that I opened a while back to make it support async code, and I believe converting the slowapi code to async wouldn't be a huge endeavour. If you feel like you can help with this, please don't hesitate!

laurentS avatar Jul 20 '21 20:07 laurentS

@laurentS cool! if you cannot manage to get your PR merged, and its been a year so that might not be likely, another option would just be to just include that custom storage backend here or via a different package?

thearchitector avatar Jul 20 '21 21:07 thearchitector

@laurentS hey, i threw together a draft PR for migrating the library over to async/await. Obviously limits is still blocking, but its wrapped in a threaded executor so it doesn't block the event loop (same style as starlette).

there's a note about the decorator wrapper that may be a bit contentious, so let me know what you think. in the future, hopefully whenever limits implements your PR, the threaded call to limits can be changed to be conditional on the storage backend used.

thearchitector avatar Jul 23 '21 02:07 thearchitector

@laurentS @thearchitector what is the current status?

10ourto avatar Aug 15 '22 15:08 10ourto

I've bumped version of limits up to 2.1 and have tried to use async+redis://localhost:6379/0 with limits[async-redis] extension, but seems it doesn't work out of the box Error relates to this assertion image

10ourto avatar Aug 15 '22 15:08 10ourto