slowapi icon indicating copy to clipboard operation
slowapi copied to clipboard

Change rate limit after the app has started

Open sandeepkumar-one opened this issue 4 years ago • 7 comments

Is it possible to change the rate limit of app or particular route after the app has started?

sandeepkumar-one avatar Sep 13 '21 19:09 sandeepkumar-one

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.

laurentS avatar Sep 14 '21 09:09 laurentS

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.

sandeepkumar-one avatar Sep 15 '21 05:09 sandeepkumar-one

Sounds good! Feel free to send a PR for this, or to add an example in the docs.

laurentS avatar Sep 15 '21 07:09 laurentS

Thanks, I will raise a PR. Could you please give me access to push a branch.

sandeepkumar-one avatar Sep 15 '21 08:09 sandeepkumar-one

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.

laurentS avatar Sep 15 '21 09:09 laurentS

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.

sandeepkumar-one avatar Sep 22 '21 17:09 sandeepkumar-one

Hi @laurentS, could you please review above mentioned PR.

sandeepkumar-one avatar Oct 06 '21 14:10 sandeepkumar-one