Fastapi: [QUESTION] Easy way for Throttling / Limiting request

Created on 24 Apr 2019  路  6Comments  路  Source: tiangolo/fastapi

Question

Is there an easy way to implement throttling or limiting on routes like e.g. Flask-Limiter (https://github.com/alisaifee/flask-limiter)?

Thanx for the reply!

question

Most helpful comment

@Ben-Bock I've put together a basic port of flask-limiter for FastAPI. See https://github.com/tiangolo/fastapi/issues/448#issuecomment-589673133

All 6 comments

If you use Traefik (which I can heartily recommend), it has built in rate limiting: https://docs.traefik.io/configuration/commons/#rate-limiting

@Ben-Bock the easiest way would probably be with @wshayes recommendation, using Traefik.

If you want something more advanced, for example, selective throttling (let's say, for anonymous/free-tier users) you could easily do that with a dependency that checks the current user and stores the number of requests in a counter in a DB with TTL (Time-To-Live) records, like Redis or Couchbase.

Being a counter (instead of just an int that you update in code) allows you to handle it even when that same user is making requests faster than what your DB can update that int.

By having a TTL you make sure that after enough time, the throttled client/user can access the API again.

@Ben-Bock I've put together a basic port of flask-limiter for FastAPI. See https://github.com/tiangolo/fastapi/issues/448#issuecomment-589673133

Thanks for writing https://github.com/laurents/slowapi @laurentS !

Is that what you were looking for @Ben-Bock ? May we close this issue?

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

If you use Traefik (which I can heartily recommend), it has built in rate limiting: https://docs.traefik.io/configuration/commons/#rate-limiting

Broken link! Working link here for Traefik documentation: https://doc.traefik.io/traefik/middlewares/ratelimit/

Was this page helpful?
0 / 5 - 0 ratings