Closed IdoKendo closed 1 year ago
Hey Ido 👋
I was thinking of adding a rate limiting option too!
I have also prepared an interface that I was expecting, which I will upload later today.
We can discuss more after that??
Sure thing! Looking forward to it.
@IdoKendo , I finally found it in my notes.
I just wanted to add the following. Create a Rate limiting function that would work well on the routers.
from robyn import RateLimiter
limiter = RateLimiter(app, "100/minute;10/second")
Few questions:
from robyn import RateLimiter
from robyn import Robyn
app = Robyn(file) limiter = RateLimiter(app, "100/minute;10/second")
@app.get("/", limiter=limiter) async def hello_world(): return "Hello world"
Or did you have some other implementation in mind? In my opinion, this is nicer, since you can have different limits on different routes. Maybe some routes need stricter limits than others etc.
2. On the other hand, do you think that this should also allow for global limiting, e.g:
```py
from robyn import RateLimiter
from robyn import Robyn
app = Robyn(__file__)
limiter = RateLimiter(app, "100/minute;10/second")
app.set_limit(limiter)
@app.get("/")
async def hello_world():
return "Hello world"
After much deliberation and back-and-forth, I've decided to make this feature an external plugin.
The repo is located at https://github.com/IdoKendo/robyn_rate_limits and it's publish to PyPI under robyn-rate-limits
.
This issue proposes to add an optional way to include rate limiting on endpoints. Example usage:
In this case, the limit will be 3 requests per minute:
In order to allow multiple workers to track the same rate limit, there will be a need to use a store such as Redis to keep the information. The usage information should be per endpoint and client.
The Redis client can be stored either as an environment variable (ROBYN_REDIS) or as a startup argument (--redis).
In case there is no Redis client defined and any endpoint has a stated rate_limit, the app would store the information in memory. This should be noted on startup in case any endpoint in the app defines a rate limit.