Swarmauri Middleware Ratepolicy
Retry-policy middleware for Swarmauri services. Provides exponential backoff with configurable retry attempts and wait intervals so unreliable upstream calls can be retried transparently.
Features
- Implements Swarmauri's
MiddlewareBasecontract; wrap any callable sequence (FastAPI routes, job runners, etc.). - Configurable
max_retriesandinitial_waitseconds. Wait time doubles on each retry (initial_wait * 2**attempt). - Emits structured logs on retry attempts and successes for observability.
- Simple synchronous dispatch; wrap async callables by providing a sync shim that executes the coroutine (see example below).
Prerequisites
- Python 3.10 or newer.
- A Swarmauri application or FastAPI project that supports middleware registration.
Installation
# pip
pip install swarmauri_middleware_ratepolicy
# poetry
poetry add swarmauri_middleware_ratepolicy
# uv (pyproject-based projects)
uv add swarmauri_middleware_ratepolicy
Quickstart
import logging
from swarmauri_middleware_ratepolicy import RetryPolicyMiddleware
logging.basicConfig(level=logging.INFO)
retry_middleware = RetryPolicyMiddleware(max_retries=3, initial_wait=0.5)
class RequestEnvelope:
def __init__(self, payload: str):
self.payload = payload
request = RequestEnvelope("work-item-123")
def call_next(req: RequestEnvelope):
raise RuntimeError("Simulated upstream failure")
retry_middleware.dispatch(request, call_next)
With Swarmauri's middleware stack (or FastAPI), register it just like other Swarmauri middleware:
from swarmauri_app.middleware import middleware_stack
from swarmauri_middleware_ratepolicy import RetryPolicyMiddleware
middleware_stack.add_middleware(
RetryPolicyMiddleware,
max_retries=4,
initial_wait=0.25,
)
Example: Wrapping an External API Call
import logging
import requests
from swarmauri_middleware_ratepolicy import RetryPolicyMiddleware
logging.basicConfig(level=logging.INFO)
retry = RetryPolicyMiddleware(max_retries=4, initial_wait=0.25)
class RequestWrapper:
def __init__(self, url: str):
self.url = url
wrapper = RequestWrapper("https://api.example.com/data")
response = retry.dispatch(
wrapper,
lambda req: requests.get(req.url, timeout=5),
)
print(response.status_code)
Tips
- Keep
max_retriessmall for user-facing endpoints to avoid long wait chains; rely on background queues for bulk retries. - Combine with the circuit breaker middleware for layered resilience (circuit breaker opens when repeated retries fail).
- When wrapping async callables, convert them to sync functions using
asyncio.runoranyio.from_threadto fit the middleware signature. - Capture logs at INFO level to trace retry attempts in production.
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_ratepolicy 0.8.3
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_ratepolicy-0.8.3.tar.gz | 7.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| swarmauri_middleware_ratepolicy-0.8.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.1 kB
Release files / swarmauri_middleware_ratepolicy-0.8.3.tar.gz
| Download URL | swarmauri_middleware_ratepolicy-0.8.3.tar.gz |
|---|---|
| Size | 7.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c5fe02ca7da0181385ac555257449844c33fca71f910bc8a18c6bdfc1738b5c2
|
|
BLAKE2b-256 checksum How to use checksums |
a7446233c5096262a64ca3264c35c81c57032914fc258ae36f720f00f8830741
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.0 {"installer":{"name":"uv","version":"0.11.0","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_ratepolicy-0.8.3-py3-none-any.whl
| Download URL | swarmauri_middleware_ratepolicy-0.8.3-py3-none-any.whl |
|---|---|
| Size | 8.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5bef1bf39fa9d81637e7cf82c7b757b88b371f08eff655b7c43d3a73f68891ce
|
|
BLAKE2b-256 checksum How to use checksums |
760d1c810dfeff07b545aebded0d90aa989d644bb116ec31972284eca69cb095
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.0 {"installer":{"name":"uv","version":"0.11.0","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}
|