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.dev32.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.dev32.tar.gz.

File metadata

  • Download URL: swarmauri_middleware_ratelimit-0.7.0.dev32.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

Hashes for swarmauri_middleware_ratelimit-0.7.0.dev32.tar.gz
Algorithm Hash digest
SHA256 de0d611c7577c17345fd32f1faf32bda126049f6df354d1fe36abdbc239e7947
MD5 fa274d270cea14dd693aef09173ed06a
BLAKE2b-256 8b1df66ec4264d3a5e27d3a2a30ce27b28eb1f5b466a814345ac4a19a1b12ec3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: swarmauri_middleware_ratelimit-0.7.0.dev32-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

Hashes for swarmauri_middleware_ratelimit-0.7.0.dev32-py3-none-any.whl
Algorithm Hash digest
SHA256 87e175316434339724d0f5cd489179a98f4d483f78bf566dc42de4de70af5f85
MD5 d8bfe6f089fd6b07119f91f2b9c99e64
BLAKE2b-256 736ef4c7d1ebb81bdc8d8e5ba9f237f2910b88c2c023227fcaa3123532f75703

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