Skip to main content

fastapi rate limiter middleware

Project description

Fastapi rate limiter

Fastapi redis rate limiter middleware

Redis 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:

pip install fastapi_redis_rate_limiter

Usage

Here's an example of how to use the Redis Rate Limiter Middleware in a FastAPI application:

from fastapi import FastAPI
from fastapi_redis_rate_limiter import RedisRateLimiterMiddleware, RedisClient

app = 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 uvicorn

    uvicorn.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 file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_redis_rate_limiter-1.0.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file fastapi_redis_rate_limiter-1.0.1.tar.gz.

File metadata

File hashes

Hashes for fastapi_redis_rate_limiter-1.0.1.tar.gz
Algorithm Hash digest
SHA256 92a751ac630253d949494dc436baa559a2eb2135f0be8ffaae42c39e1a0adb6e
MD5 b384cbb97561239dd69ea533422ae351
BLAKE2b-256 2bed7e57b7a097486b286242eb2810dcef5d0e276b9b16ba50595651fd072606

See more details on using hashes here.

File details

Details for the file fastapi_redis_rate_limiter-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_redis_rate_limiter-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6ab3db87eb046bdd420f5373576f4f0f6c5639a427fe323101a31e2de4e6623d
MD5 0f1eed213dc3f70d37583a8e9a8a3b0a
BLAKE2b-256 c47a9cf873f212d534ab6acd7392c85fa25850389e4a7317bd02d0f9ac15051b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page