Skip to main content

Bulkhead middleware implementation for Swarmauri

Project description

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_middleware_bulkhead


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.

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_bulkhead-0.8.0.dev44.tar.gz (7.9 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_bulkhead-0.8.0.dev44.tar.gz.

File metadata

  • Download URL: swarmauri_middleware_bulkhead-0.8.0.dev44.tar.gz
  • Upload date:
  • Size: 7.9 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_bulkhead-0.8.0.dev44.tar.gz
Algorithm Hash digest
SHA256 353d84e3dcb1c93c4fe3d3b7d5283106e71467b4dbc7080e0299d93699d83fbd
MD5 0a77c6c2f8deef970722e87187f52865
BLAKE2b-256 4368b737b6ec15885c3823151cd7d198f1f19a6ae4101b50db59a47ab4ed4b07

See more details on using hashes here.

File details

Details for the file swarmauri_middleware_bulkhead-0.8.0.dev44-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_middleware_bulkhead-0.8.0.dev44-py3-none-any.whl
  • Upload date:
  • Size: 9.0 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_bulkhead-0.8.0.dev44-py3-none-any.whl
Algorithm Hash digest
SHA256 d363b0cce7e6546a9187644e86edd2b2cea5a10514296dc5718bf693fd66c10c
MD5 f8620dd89d5129c1c434cacae44faf04
BLAKE2b-256 8d0e00f79af88a17dda6bf5b2272c1c122955c6f29c9b34b4b3adcf090eb095c

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