https://github.com/iunary/fastapi-redis-rate-limiter
fastapi-redis-rate-limiter
https://github.com/iunary/fastapi-redis-rate-limiter
fastapi python3 rate-limiter redis
Last synced: 5 months ago
JSON representation
fastapi-redis-rate-limiter
- Host: GitHub
- URL: https://github.com/iunary/fastapi-redis-rate-limiter
- Owner: iunary
- License: mit
- Created: 2023-05-13T23:55:56.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-05-15T15:03:24.000Z (about 2 years ago)
- Last Synced: 2024-12-16T04:16:57.301Z (5 months ago)
- Topics: fastapi, python3, rate-limiter, redis
- Language: Python
- Homepage:
- Size: 15.6 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/iunary/fastapi-redis-rate-limiter/actions/workflows/app.yml)
# Fastapi redis rate limiter middlewareRedis Rate Limiter Middleware is a Python module that provides rate limiting functionality for FastAPI applications using Redis as the storage backend. It allows you to limit the number of requests a client can make within a specified time window.
## Features
- Simple integration with FastAPI applications
- Customizable rate limit and time window
- Uses Redis as the storage backend for efficient rate limiting
- Easy to configure and use## Installation
Install the Redis Rate Limiter Middleware module using `pip`:
```shell
pip install fastapi_redis_rate_limiter
```## Usage
Here's an example of how to use the Redis Rate Limiter Middleware in a FastAPI application:
```python
from fastapi import FastAPI
from fastapi_redis_rate_limiter import RedisRateLimiterMiddleware, RedisClientapp = FastAPI()
# Initialize the Redis client
redis_client = RedisClient(host="localhost", port=6379, db=0)# Apply the rate limiter middleware to the app
app.add_middleware(RedisRateLimiterMiddleware, redis_client=redis_client, limit=40, window=60)@app.get("/limited")
async def limited():
return {"message": "This is a protected endpoint."}# Run the FastAPI application
if __name__ == "__main__":
import uvicornuvicorn.run(app, host="0.0.0.0", port=8000)
```In this example, we create a FastAPI application and initialize a Redis client using `RedisClient`. Then, we add `RedisRateLimiterMiddleware` to the app middleware using `add_middleware` and by passing in Redis client, rate limit, and time window.
Make sure to adjust the Redis connection parameters (host, port, and db) according to your Redis server configuration.
## Configuration
The RedisRateLimiterMiddleware accepts the following parameters:
- `app` (FastAPI): The FastAPI application instance.
- `redis_client` (RedisClient): The Redis client instance for interacting with Redis.
- `limit` (int): The maximum number of requests allowed within the time window.
- `window` (int): The time window in seconds within which the requests are limited (default: 60 seconds).Adjust the `limit` and `window` values according to your desired rate limiting requirements.
## Contributions
Contributions, issues, and feature requests are welcome! Feel free to open a new issue or submit a pull request on GitHub.
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.