Rate limiting middleware for Swarmauri applications
Project description
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
100requests per60seconds. - 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
Responseobjects with HTTP429status 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file swarmauri_middleware_ratelimit-0.7.0.dev33.tar.gz.
File metadata
- Download URL: swarmauri_middleware_ratelimit-0.7.0.dev33.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
52da6f5705ae751bb923fa409aa938d12db0d1e04060acf03697e09be04f0069
|
|
| MD5 |
742f6fd5422c738c95cf02c8eb5a2b23
|
|
| BLAKE2b-256 |
d29b3af181bc89b2310de93139a4af833ef49b49fcb0e43c54306fee224dd78e
|
File details
Details for the file swarmauri_middleware_ratelimit-0.7.0.dev33-py3-none-any.whl.
File metadata
- Download URL: swarmauri_middleware_ratelimit-0.7.0.dev33-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ee0a66106cca19230e1db33e6888b83250587f998ff2b5642b526cbeece6aa9
|
|
| MD5 |
8c4f43689963379d73771fac8b05c812
|
|
| BLAKE2b-256 |
7805761dec224a1c89c38597cacf1325177c264505b3d31ebb5f3e014153adaa
|