This release is a pre-release and may not be stable for production use.
Swarmauri Middleware Bulkhead
Concurrency isolation middleware for FastAPI applications. Limit the number of simultaneous requests to protect resources from overload and ensure reliable service operation.
Features
- Concurrency control restricts the number of in-flight requests using an
asyncio.Semaphore. - Configurable limits let you tune
max_concurrency(default: 10) to match service capacity. - Fail-fast validation guards against non-positive concurrency limits at initialization.
- Structured logging surfaces request flow and failure details for easier diagnostics.
- FastAPI compatibility enables seamless integration into ASGI middleware stacks.
Installation
Choose the tool that matches your workflow:
# pip
pip install swarmauri_middleware_bulkhead
# Poetry
poetry add swarmauri_middleware_bulkhead
# uv
uv add swarmauri_middleware_bulkhead
Quickstart
The middleware wraps a call_next handler and ensures that no more than max_concurrency requests execute at once. Run the example below with python quickstart.py to see the concurrency ceiling in action.
import asyncio
from fastapi import Request
from swarmauri_middleware_bulkhead import BulkheadMiddleware
async def main() -> None:
bulkhead = BulkheadMiddleware(max_concurrency=2)
active_requests = 0
peak_active_requests = 0
lock = asyncio.Lock()
async def call_next(request: Request):
nonlocal active_requests, peak_active_requests
async with lock:
active_requests += 1
peak_active_requests = max(peak_active_requests, active_requests)
try:
await asyncio.sleep(0.05)
return {"path": request.scope.get("path"), "handled": True}
finally:
async with lock:
active_requests -= 1
async def simulate_request(idx: int):
request = Request(scope={"type": "http", "path": f"/task/{idx}"})
return await bulkhead.dispatch(request, call_next)
responses = await asyncio.gather(*(simulate_request(i) for i in range(5)))
assert peak_active_requests <= bulkhead.max_concurrency
print("Peak concurrent requests:", peak_active_requests)
print("Responses:", responses)
if __name__ == "__main__":
asyncio.run(main())
Want to help?
If you want to contribute to swarmauri-sdk, read up on our guidelines for contributing that will help you get started.
Metadata
Release files for swarmauri_middleware_bulkhead 0.11.0.dev1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| swarmauri_middleware_bulkhead-0.11.0.dev1.tar.gz | 8.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| swarmauri_middleware_bulkhead-0.11.0.dev1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.0 kB
Release files / swarmauri_middleware_bulkhead-0.11.0.dev1.tar.gz
| Download URL | swarmauri_middleware_bulkhead-0.11.0.dev1.tar.gz |
|---|---|
| Size | 8.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
81adf54f63735984ca9062a2be5c393ebb41587abefcd35c65665c72db306f41
|
|
BLAKE2b-256 checksum How to use checksums |
a53ab3b3e94f30a9d1848276f0b1b2f44bd602f873ac9719080ad194c7532c0b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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}
|
Release files / swarmauri_middleware_bulkhead-0.11.0.dev1-py3-none-any.whl
| Download URL | swarmauri_middleware_bulkhead-0.11.0.dev1-py3-none-any.whl |
|---|---|
| Size | 9.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8a97c50498c66e0022c0176e2da711e3d3054c1028c96a580f0061e48b30ca03
|
|
BLAKE2b-256 checksum How to use checksums |
80829a6487b37e66e00eb05bd3c4887640e839800d69faf4fb6b948c2ec4a722
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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}
|