Change rate limit after the app has started
Is it possible to change the rate limit of app or particular route after the app has started?
You should be able to pass a callable that returns a limit string, something like 1/minute, as the limit definition in your decorator, instead of the hardcoded limit string. That function can then return whatever you need on any call. Something like:
def dynamic_limit():
# do anything you want here
return "1/minute"
@limiter.limit(dynamic_limit)
async def homepage(request: Request):
return PlainTextResponse("test")
Let me know if this works for your use case.
Thank you Laurent for explanation. I suggest we add this example in the test case (tests/test_fastapi_extension.py). It will be helpful for others as well.
Sounds good! Feel free to send a PR for this, or to add an example in the docs.
Thanks, I will raise a PR. Could you please give me access to push a branch.
Thanks, I will raise a PR. Could you please give me access to push a branch.
I don't have access to the repo settings from mobile, but you can fork the repo and open a PR from there.
Hi Laurent, I raised a PR : https://github.com/laurentS/slowapi/pull/66 as per above discussion.
I have a doubt, as seen in my PR I am using exempt in this format : @limiter.limit('exempt') rather than @limiter.exempt. If I use the latter I am getting runtime error. Could you please check it once.
Hi @laurentS, could you please review above mentioned PR.