Rate-limiter for FastAPI with the possibility of user-based rate limits
Project description
FastAPI rate limiter
This package adds a rate limiter to FastAPI using Redis.
Installation
First install Redis, then install the package using:
pip install fastapi-user-limiter
Usage
You can use the rate_limit
function as a FastAPI Dependency to add one or several rate limiters to an endpoint:
from fastapi_user_limiter.limiter import RateLimiterConnection, rate_limiter
from fastapi import FastAPI, Depends
app = FastAPI()
# Max 2 requests per 5 seconds
@app.get("/single",
dependencies=[Depends(rate_limiter(RateLimiterConnection(), 2, 5))])
async def read_single():
return {"Hello": "World"}
# Max 1 requests per second and max 3 requests per 10 seconds
@app.get("/multi/{some_param}", dependencies=[
Depends(rate_limiter(RateLimiterConnection(), 1, 1)),
Depends(rate_limiter(RateLimiterConnection(), 3, 10))
])
async def read_multi(some_param: str):
return {"Hello": f"There {some_param}"}
The aforementioned examples and more can be found in example.py
(use uvicorn example:app --reload
to run).
Future features
The package will soon have the additional feature of allowing each user account to have a different rate limit for each endpoint.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for fastapi_user_limiter-0.3.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d89a5a7121d39547f5a29385a9de3c18f56c2ad3c554b6d65a2beea8ad90dafb |
|
MD5 | af835c9f50050cfda62e5d56328ae258 |
|
BLAKE2b-256 | bee9bcc6a84837968cea076178caef598a3d4992c904774c58b23ea5ed21ad1b |
Close
Hashes for fastapi_user_limiter-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e0013ac05ef6da1e282b842ce7facba2c59211bdf35be87a124dbe76d2a781db |
|
MD5 | a75262346539b0e70d5ca86e81d1d13b |
|
BLAKE2b-256 | 471df5e9116cdf8d91f29a6184a03134136932984d3b9cfa9d34541331b3868b |