Skip to content
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

Error in the documentation #75

Closed
AntonGsv opened this issue Dec 8, 2021 · 1 comment
Closed

Error in the documentation #75

AntonGsv opened this issue Dec 8, 2021 · 1 comment

Comments

@AntonGsv
Copy link

AntonGsv commented Dec 8, 2021

In the example on the documentation page, the limit on a request to any of the endpoints adds 2 to the counter, this behavior can be corrected by making the name of the decoded functions different.
Extended version of the code from the documentation for reproduction:

from fastapi import FastAPI
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded
from fastapi import Request, Response
from fastapi.responses import PlainTextResponse

limiter = Limiter(key_func=get_remote_address)
app = FastAPI()
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)


# Note: the route decorator must be above the limit decorator, not below it
@app.get("/home")
@limiter.limit("5/minute")
async def homepage(request: Request):
    return PlainTextResponse("test")


@app.get("/mars")
@limiter.limit("5/minute")
async def homepage(request: Request, response: Response):
    return {"key": "value"}


if __name__ == "__main__":
    import uvicorn

    uvicorn.run("main:app", host="127.0.0.1", reload=False, port=int("8000"))
@laurentS
Copy link
Owner

Can you confirm that changing one of the function names, say to homepage2 fixes the error?

My guess is that the code that generates the hash keys for each request is too simplistic and should use something else than function name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants