Skip to main content

High-performance async rate limiter for FastAPI with Redis or native in-memory backend

Project description

fastapi-easylimiter

Simple async rate-limiting middleware for FastAPI with Redis or in-memory caching.

Simple design

  • Async rate limiting
  • Redis or in-memory cache
  • Easy rules per URL paths.
  • Optional rate limit headers (X-RateLimit-Limit, X-RateLimit-Remaining, Retry-After)

Installation

pip install fastapi-easylimiter

Usage

from fastapi import FastAPI
from fastapi_easylimiter import AsyncRedisBackend, InMemoryBackend, RateLimiterMiddleware
import redis.asyncio as redis_async

app = FastAPI()

REDIS_URL = "redis://localhost:6379/0"

# Redis backend (recommended for multi-instance deployments)
redis_client = redis_async.from_url(REDIS_URL, decode_responses=True)
backend = AsyncRedisBackend(redis_client)

# Or for single-instance/local development:
# backend = InMemoryBackend()

rules = {
    "/api/users": {"limit": 1, "period": 2},
    "/api/": {"limit": 60, "period": 60}, # GLOBAL ONES GO LAST
}

app.add_middleware(
    RateLimiterMiddleware,
    rules=rules,
    backend=backend,
    backoff_threshold=3,
    max_backoff=60
)

Contributing

Contributions and forks are always welcome! Feel free to adapt, improve, or extend this middleware for your own needs. This was purely made out of personal necessity.

Support

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.1.8.tar.gz (4.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.1.8-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_easylimiter-0.1.8.tar.gz
  • Upload date:
  • Size: 4.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.1.8.tar.gz
Algorithm Hash digest
SHA256 a1e5c05716d0d1ae4cea4d61da5d075dc9a2f1f30e203d72638b9ef7213a7a65
MD5 0507426ac8def1fb0de5fdfc22bc267b
BLAKE2b-256 e5349551c93be5c2a75823d787a7235aa90739ae1656cf90b0d9ca3fc51cef33

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_easylimiter-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 a5d35f68d754a835edb05efac8aadaa67f14474549f1c3ed4439b08a82215b20
MD5 38624b568fbf2aac9470d2c2929f9c01
BLAKE2b-256 48230fc6697d6ceba96c3ece3fb1f9f95f0676e0904e6829e01b678440c3a9ec

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