Can global limits make this in FastAPI?
stilleshan opened this issue · 4 comments
stilleshan commented
If I set the global limit to "10/minute" with default_limits, can some API endpoints receive the global limit even if they don't have a limit decorator, while other endpoints with limit decorators receive the limits set by the decorators?
Here's an example: I want to limit access to the /test endpoint to 3 times, and then limit access to both the / and /home endpoints to a total of 7 times. I don't want to add decorators to / and /home separately.
limiter = Limiter(key_func=get_remote_address, default_limits=["10/minute"])
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
app.add_middleware(SlowAPIMiddleware)
@app.get("/")
async def root_page(request: Request):
return 'root'
@app.get("/home")
async def homepage(request: Request):
return 'home'
@app.get("/test")
@limiter.limit("3/minute")
async def test(request: Request):
return 'test'
laurentS commented
Hi @stilleshan yes, this should work. See this example in the docs.
If your code above does not work, then I believe you've found a bug.