This is a rate limiting solution designed for FastAPI applications. The project demonstrates the implementation of rate limiting using different storage options.
Fast Limiter
showcases how to implement rate limiting in a FastAPI application.
It includes support for Redis and SQLite as storage backends, allowing you to control how frequently clients can access your API endpoints.
- Rate Limiting: Controls the rate at which clients can make requests.
- Redis Storage: Scalable, fast, and suitable for production.
- SQLite Storage: Simple and file-based, ideal for testing and development.
- Python 3.11+
- Redis (if using Redis for storage)
- SQLite (if using SQLite for storage)
-
Clone the Repository
git clone https://github.com/patrickpasquini/fast_limiter.git cd fast_limiter
-
Install Dependencies
pip install -r requirements.txt
-
Set up the environment variables:
Create a
.env
file in the root directory of the project with the following content:REDIS_URL=redis://localhost DB_PATH=rtl.db
The
REDIS_URL
andDB_PATH
variables can be customized to point to your specific Redis instance or SQLite database file.
Create a Fast Limiter
from fast_limiter import FastLimiter
from fast_limiter.storages import RedisStorage # or SQLiteStorage
from fastapi import Depends, FastAPI
limiter = FastLimiter(RedisStorage(), limit=5, interval=60)
app = FastAPI()
@app.get("/limited", dependencies=[Depends(limiter)])
async def limited_endpoint():
return {"message": "This endpoint is rate limited"}
Or using decorator
from fast_limiter import FastLimiter, fast_limit
from fast_limiter.storages import RedisStorage # or SQLiteStorage
from fastapi import FastAPI, Request
limiter = FastLimiter(RedisStorage(), limit=5, interval=60)
app = FastAPI()
@app.get("/limited-decorator")
@fast_limit(limiter)
async def limited_decorator_endpoint(request: Request):
return {"message": "This endpoint is rate limited with a decorator"}