long2ice / fastapi-limiter

A request rate limiter for fastapi
https://github.com/long2ice/fastapi-limiter
Apache License 2.0
487 stars 53 forks source link

User customized rate limit ? #26

Closed harshraj22 closed 1 year ago

harshraj22 commented 1 year ago

I want to have a subscription based api service. there are 3 types of subscription: Free: 10 api calls per 24hour Basic: 30 api calls per 24hour Advanced: 80 api calls per 24hour.

Along with the request body, suppose the detaiils about the subscription is also available. Can such scenario be handled by your library ? I am using fastapi, and I would prefer using a library than writing my own rate limiter using reddis.

long2ice commented 1 year ago

No, which need your own customization

ghost commented 9 months ago

I opted for something like this

async def dynamic_rate_limiter(request: Request, response: Response):
    # logic here, if free, else if premium ...
    await RateLimiter(times=1, seconds=60)(request, response)

@router.get(
    "/v1/dummy",
    dependencies=[Depends(dynamic_rate_limiter)],
)
def get_dummy():
    return {"dummy": "dummy"}