Skip to main content

Rate limiting middleware for Swarmauri applications

Project description

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_middleware_ratelimit


Swarmauri Middleware Ratelimit

Flexible rate limiting middleware for Swarmauri applications.

This package provides a drop-in FastAPI middleware that throttles requests based on the client IP address or an authentication token. It is useful for preventing abuse of public APIs or protecting upstream services from bursts of traffic.

Features

  • Configurable request limit and time window that defaults to 100 requests per 60 seconds.
  • Tracks callers by IP address or a custom authentication token header.
  • Maintains in-memory counters that reset automatically after the configured time window.
  • Returns FastAPI Response objects with HTTP 429 status codes when the limit is exceeded.

Installation

Choose the tool that matches your workflow:

# pip
pip install swarmauri_middleware_ratelimit

# Poetry
poetry add swarmauri_middleware_ratelimit

# uv
uv add swarmauri_middleware_ratelimit

Usage

Basic IP-based limiting

from fastapi import FastAPI
from fastapi.testclient import TestClient
from swarmauri_middleware_ratelimit import RateLimitMiddleware

app = FastAPI()
rate_limiter = RateLimitMiddleware(rate_limit=2, time_window=60)


@app.middleware("http")
async def limit_requests(request, call_next):
    return await rate_limiter.dispatch(request, call_next)


@app.get("/ping")
async def ping():
    return {"status": "ok"}


client = TestClient(app)

assert client.get("/ping").status_code == 200
assert client.get("/ping").status_code == 200
assert client.get("/ping").status_code == 429

In this example the first two requests succeed because they fall inside the allowed limit. A third request within the same 60-second window exceeds the threshold and the middleware immediately returns a 429 response with the body "Rate limit exceeded". The middleware instance keeps request counters in memory, so you should create one instance per application process.

Token-based rate limiting

# Use the value of the `X-Api-Key` header to track clients instead of IP
rate_limiter = RateLimitMiddleware(
    rate_limit=100,
    time_window=60,
    use_token=True,
    token_header="X-Api-Key",
)


@app.middleware("http")
async def limit_requests(request, call_next):
    return await rate_limiter.dispatch(request, call_next)

When use_token is True the middleware looks for the configured header on every request and raises a ValueError if it is missing. This makes it safe to enforce API key or bearer token quotas even when requests are routed through shared proxies. Reuse the same middleware instance across requests to ensure counters continue to accumulate across calls.

Want to help?

If you want to contribute to swarmauri-sdk, read up on our guidelines for contributing that will help you get started.

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

swarmauri_middleware_ratelimit-0.7.0.dev47.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file swarmauri_middleware_ratelimit-0.7.0.dev47.tar.gz.

File metadata

  • Download URL: swarmauri_middleware_ratelimit-0.7.0.dev47.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swarmauri_middleware_ratelimit-0.7.0.dev47.tar.gz
Algorithm Hash digest
SHA256 7ba533ed4db7307238cc60b52a9272d88542aa3ed449caefd2bdf198ed87a675
MD5 09c735575129136cf2af19f152b47478
BLAKE2b-256 c95331fe58f8798d665eace16413b6569909860a48610aac5159d0a504e97b24

See more details on using hashes here.

File details

Details for the file swarmauri_middleware_ratelimit-0.7.0.dev47-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_middleware_ratelimit-0.7.0.dev47-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swarmauri_middleware_ratelimit-0.7.0.dev47-py3-none-any.whl
Algorithm Hash digest
SHA256 10e99b48c1796dd23ae750b42c637ba0cee06e0ed6eebcb8c5265b29d3f511c7
MD5 a0ccdf2befd87bea1273f2c04de00803
BLAKE2b-256 14d198d0b161a41444a7eafa383456838748cfdf96a90c68bbfa2283919bf0f5

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