-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic rate limit based on user type #13
Comments
Hi @sdklab007, you should be able to use a callable to pick the limit, like:
and if you want some users to be exempted from the limit, you should also be able to do:
I hope this helps! |
@laurentS Thank you so much. |
@laurentS One more query, how do I get the request object in get_limit_for_user to identify the user. |
@sdklab007 sorry for the lag. I'm afraid I don't have a good solution for your last question. This is a use case which I don't think has been needed so far. The code was ported from |
@laurentS Thanks for your kind update. I need to hack a bit as per the link you've shared. Sure, I will see if I can contribute :) |
I was able to solve this by the link you had shared, below is the way if someone needs it:
Cheers!! @laurentS |
This post help me a lot! Thanks |
Could someone give an example, of how to use @sdklab007's code in practice? My endpoint is the following: @app.post("/v1/chat/completions")
@limiter.limit("2/second")
@limiter.limit("10/minute")
@limiter.limit("100/hour")
@limiter.limit("2000/day")
async def chat_completion(request: Request, data: dict = Body(...)):
model = data.get("model", None) I want to check if the model equals @limiter.limit("1/second")
@limiter.limit("5/minute")
@limiter.limit("50/hour")
@limiter.limit("1000/day") |
You can also use a double-wrapper (a whopper 😃) to get access to the request. def condition_func(request: Request, func, *args, **kwargs):
if no_limit:
return func.__wrapped__(request=request, *args, **kwargs) # call unlimited func
return func(request=request, *args, **kwargs)
def ratelimit(*decor_args, **decor_kwargs):
def decorate(func):
condition_func = decor_kwargs.pop('condition_func')
func = decor_kwargs.pop('limiter').limit(*decor_args, **decor_kwargs)(func)
@functools.wraps(func)
def wrapper(request: Request, *args, **kwargs):
return condition_func(request, func, *args, **kwargs)
return wrapper
return decorate Use it like the original: @ratelimit('10/day', limiter=limiter, condition_func=condition_func) |
@gellnerm Could you show me a complete example in fastapi? Also on discord if you want (username: fredipy) |
Like I want to change the rate limit based on the data that gets sent.
|
any news here? i have an async func that calls my PSQL database and returns a str containing a ratelimit, i want to use this. |
@laurentS Can we reopen this issue in the meantime? A few seem to be asking for it. Personally I have a It might be hard to implement, but I'm thinking of a use case like this: @limiter.limit("4/second")
@limiter.limit("20/minute")
@limiter.limit("500/hour")
@limiter.limit("10000/day")
@limiter.limit(custom=...)
@router.post("/get-some-data")
async def get_some_data(data: dict = Body(...), user_jwt: dict = Depends(guard_login)): Where, the first 4 use the IP address as key, and the second 4 are by user_id using the user_id as key and the JWT's rate limit. Perhaps some or all of that is not possible, I'm not really sure what's under the hood or how this is implemented. |
With FastAPI becoming more and more popular, this is a common usecase now. Either request should be made accessible or async dynamic limit Callable must be supported. Async support would probably be easier and help with most cases? @laurentS What are your thoughts on this? |
I would also like support for this as @anu-kailash proposes, though it may be complicated. |
I need a way to dynamically set the rate limit based on the user type.
For example, I want to limit users without access token & have unlimited access to users with the access token.
What I am currently using:
I am trying to find a way to conditionally limit 2/minute. Basically I want to increase the limit based on the user type.
The text was updated successfully, but these errors were encountered: