Skip to main content

Synchronous retry-policy middleware for Swarmauri with bounded attempts and exponential backoff.

Project description

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_middleware_ratepolicy Discord

Swarmauri Middleware Rate Policy

swarmauri_middleware_ratepolicy is the Swarmauri retry-policy middleware for wrapping request-like call flows with bounded retry attempts and exponential backoff. It provides a synchronous dispatch(request, call_next) surface that retries failing operations before finally re-raising the exception.

Why Use Swarmauri Middleware Rate Policy

  • Retry flaky upstream operations with a consistent middleware surface.
  • Bound retry counts while still applying exponential backoff.
  • Add basic resilience to synchronous request-processing or job-execution pipelines.
  • Combine with circuit breakers and logging to build layered fault handling.

FAQ

What parameters does this middleware expose?
max_retries and initial_wait.

How is the wait interval calculated?
The middleware sleeps for initial_wait * 2**attempts between retries.

Is the middleware async?
No. The current implementation is synchronous.

What happens after the retry limit is reached?
The last exception is re-raised.

Features

  • Bounded retry loops over a synchronous call_next callable.
  • Exponential backoff using initial_wait.
  • Log messages for processing, retry attempts, and success.
  • Swarmauri MiddlewareBase integration for reusable resilience flows.
  • Supports Python 3.10, 3.11, 3.12, 3.13, and 3.14.

Installation

uv add swarmauri_middleware_ratepolicy
pip install swarmauri_middleware_ratepolicy

Usage

from swarmauri_middleware_ratepolicy import RetryPolicyMiddleware

retry = 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):
    raise RuntimeError("Simulated upstream failure")

retry.dispatch(request, call_next)

Examples

Retry a failing operation

import logging
from swarmauri_middleware_ratepolicy import RetryPolicyMiddleware

logging.basicConfig(level=logging.INFO)

retry = RetryPolicyMiddleware(max_retries=3, initial_wait=0.25)

def unstable(_request):
    raise RuntimeError("Temporary failure")

retry.dispatch({"task": "sync"}, unstable)

Wrap an external API call

import httpx
from swarmauri_middleware_ratepolicy import RetryPolicyMiddleware

retry = RetryPolicyMiddleware(max_retries=4, initial_wait=0.25)

class RequestWrapper:
    def __init__(self, url: str):
        self.url = url

response = retry.dispatch(
    RequestWrapper("https://api.example.com/data"),
    lambda req: httpx.get(req.url, timeout=5),
)

print(response.status_code)

Register in a larger middleware stack

from swarmauri_middleware_ratepolicy import RetryPolicyMiddleware

middleware = RetryPolicyMiddleware(max_retries=2, initial_wait=1.0)
print(middleware.type)

Related Packages

Swarmauri Foundations

More Documentation

Best Practices

  • Keep retry counts small for latency-sensitive user requests.
  • Use circuit breakers when a dependency is consistently unhealthy instead of only increasing retries.
  • Apply retries around idempotent operations whenever possible.
  • Convert async flows deliberately if you need to wrap them with this synchronous middleware surface.

License

This project is licensed under the Apache-2.0 License.

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

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_ratepolicy-0.11.0.dev1.tar.gz.

File metadata

  • Download URL: swarmauri_middleware_ratepolicy-0.11.0.dev1.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for swarmauri_middleware_ratepolicy-0.11.0.dev1.tar.gz
Algorithm Hash digest
SHA256 3f9c59b75a5ec4b02f246a21284358ace25267bc06c9013bdb22628dde0f81bb
MD5 3223f3771ff064d836691713d59af89c
BLAKE2b-256 f821c8a2c04f88cd50cb3b043a72af281cbf356e1dde70e2d6f3cdb65b3ff3ef

See more details on using hashes here.

File details

Details for the file swarmauri_middleware_ratepolicy-0.11.0.dev1-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_middleware_ratepolicy-0.11.0.dev1-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for swarmauri_middleware_ratepolicy-0.11.0.dev1-py3-none-any.whl
Algorithm Hash digest
SHA256 bfa1026a476ea5a58d329046b6c137669bbd8b2c4d2607c9b7f50f775eadb176
MD5 26a38ba89c715ea08b2d3542e837735b
BLAKE2b-256 4ad6cce25c44610cd7d16708af67ab7e9cd8848da48503541a1f644d756109f0

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