Skip to main content

A middleware implementing rate policy and retry logic for Swarmauri

Project description

Swarmauri Logo

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


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 MiddlewareBase contract; wrap any callable sequence (FastAPI routes, job runners, etc.).
  • Configurable max_retries and initial_wait seconds. 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_retries small 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.run or anyio.from_thread to 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.

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_ratepolicy-0.8.2.dev7.tar.gz (7.5 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_ratepolicy-0.8.2.dev7.tar.gz.

File metadata

  • Download URL: swarmauri_middleware_ratepolicy-0.8.2.dev7.tar.gz
  • Upload date:
  • Size: 7.5 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_ratepolicy-0.8.2.dev7.tar.gz
Algorithm Hash digest
SHA256 d6f7e5a4b279d4a5a3b93dfec7fe68357a1e843a1699d6d4cdd83d6e1c3fe573
MD5 56d56a1a749799d3caded7deb9e865cb
BLAKE2b-256 8d5010f3519cf6723d0b168009bffd0d635f4df25e6e3c01965fe05f55452d94

See more details on using hashes here.

File details

Details for the file swarmauri_middleware_ratepolicy-0.8.2.dev7-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_middleware_ratepolicy-0.8.2.dev7-py3-none-any.whl
  • Upload date:
  • Size: 8.6 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_ratepolicy-0.8.2.dev7-py3-none-any.whl
Algorithm Hash digest
SHA256 5dc86a1b1ef2d95a35511fc9e1d27c54ee56fa5b73e0a6ffbb779fe8938c71ba
MD5 7c8b7516b2797b2326bccf7fc0b505c6
BLAKE2b-256 03ecf8b0356da33ed727ee1ccfbedf57eec28307beecd3ae8e3b6309b409bef0

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