Skip to main content

Async ASGI rate limiter for FastAPI with Redis.

Project description

fastapi‑easylimiter

GitHub stars GitHub forks GitHub issues GitHub license PyPI


An ASGI async rate-limiting middleware for FastAPI with Redis, designed to handle auto-generated routes (e.g., FastAPI-Users) without decorators, for simplicity and ease of use.


Features

  • Path based rules (/api/*, /auth/*, exact matches)
  • Fixed, Sliding & Moving window algorithms (Lua)
  • RateLimit, RateLimit-Policy, Retry-After headers
  • Bans with back-off per IP with configurable window
  • ASGI async middleware for FastAPI/Starlette
  • Asyncio Redis support
  • Easy to configure
  • No decorators needed
  • HTML/JSON error responses
  • XFF Header Support when enabled

TODO

  • In-memory option

Installation

pip install fastapi-easylimiter

Usage

from fastapi import FastAPI
import redis.asyncio as redis
from middleware.rate import RateLimitMiddleware

app = FastAPI()

redis_client = redis.from_url("redis://localhost:6379/0")

app.add_middleware(
    RateLimitMiddleware,
    redis=redis,
    rules={
        "/*": (200, 60, "moving"),           
        "/api/*": (10, 1, "sliding"),
        "/api/auth/*": (3, 1, "sliding"),
        "/api/users/me": (3, 30, "fixed"),
    },
    exempt=[],
    enable_bans=True,
    ban_offenses=8,
    ban_window="10m",
    ban_length="5m",
    ban_max_length="1d",
    enable_xff=False,
    )

Example: /api/auth/login matches /api/auth and /api. If any rule is exceeded → 429 returned. If banned → 403 returned.


Redis Key Patterns

Key Pattern Example Type Used For
rl:Fixe:{hash}:{limit}:{window} rl:Fixe:a1b2c3d4e5f6a7b8:100:60 String Fixed-window counter
rl:Slid:{hash}:{limit}:{window} rl:Slid:a1b2c3d4e5f6a7b8:60:60 ZSET Sliding window request log
offense:{hash} offense:{a1b2c3d4e5f6a7b8} ZSET Offense tracking for ban escalation
ban:{hash} ban:{a1b2c3d4e5f6a7b8} String+TTL Active ban flag

Middleware Parameters

Parameter Type Required Description
redis redis.asyncio.Redis Yes Redis async client
rules Dict[str, Tuple[int, int, str]] Yes Path → (limit, period, strategy)
exempt Optional[List[str]] No Paths that bypass rate limits
enable_bans bool No Enable/disable ban system
ban_offenses int No Offenses before ban triggers
ban_window str No Time window for offense accumulation
ban_length str No Initial ban length
ban_max_length str No Maximum exponential ban ceiling
enable_xff bool No Enable X-Forwarded-For support

Screenshot

image image

Contributing

Contributions and forks are always welcome! Adapt, improve, or extend for your own needs.

Buy Me a Coffee

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_easylimiter-0.4.3.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_easylimiter-0.4.3-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_easylimiter-0.4.3.tar.gz.

File metadata

  • Download URL: fastapi_easylimiter-0.4.3.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for fastapi_easylimiter-0.4.3.tar.gz
Algorithm Hash digest
SHA256 8ce173d0739c82ca80b6e6dd72b7bd397ee36bf120b99766fb9a8e4200c7403e
MD5 70cf8eb49f403129106acc20775fa753
BLAKE2b-256 73d8152cdecb20bba2a0f20a9e9cb7f3aac0f5df005b03346f67f1c293d6ca10

See more details on using hashes here.

File details

Details for the file fastapi_easylimiter-0.4.3-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_easylimiter-0.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 067c8c536b07c653c3456b7ae646a49cf8477f0504f34f2be5210c5997ca2b4b
MD5 edb96a951534f00bd8536eb9289a9d95
BLAKE2b-256 95af342fd209fafb0098e3307d4606bd5ff0448bbfa93a1230878da5f4797655

See more details on using hashes here.

Supported by

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