swarmauri_middleware_ratepolicy 0.8.4.dev2__tar.gz → 0.11.0.dev1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,194 @@
1
+ Metadata-Version: 2.4
2
+ Name: swarmauri_middleware_ratepolicy
3
+ Version: 0.11.0.dev1
4
+ Summary: Synchronous retry-policy middleware for Swarmauri with bounded attempts and exponential backoff.
5
+ License-Expression: Apache-2.0
6
+ License-File: LICENSE
7
+ Keywords: swarmauri,middleware,ratepolicy,retry,tenacity,backoff,resilience,fault tolerance,request middleware,retry policy
8
+ Author: Jacob Stewart
9
+ Author-email: jacob@swarmauri.com
10
+ Requires-Python: >=3.10,<3.15
11
+ Classifier: License :: OSI Approved :: Apache Software License
12
+ Classifier: Natural Language :: English
13
+ Classifier: Development Status :: 3 - Alpha
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Intended Audience :: System Administrators
16
+ Classifier: Operating System :: OS Independent
17
+ Classifier: Topic :: Internet :: WWW/HTTP
18
+ Classifier: Topic :: Software Development :: Libraries
19
+ Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
20
+ Classifier: Programming Language :: Python
21
+ Classifier: Programming Language :: Python :: 3
22
+ Classifier: Programming Language :: Python :: 3 :: Only
23
+ Classifier: Programming Language :: Python :: 3.10
24
+ Classifier: Programming Language :: Python :: 3.11
25
+ Classifier: Programming Language :: Python :: 3.12
26
+ Classifier: Programming Language :: Python :: 3.13
27
+ Classifier: Programming Language :: Python :: 3.14
28
+ Requires-Dist: swarmauri_base
29
+ Requires-Dist: swarmauri_core
30
+ Requires-Dist: swarmauri_standard
31
+ Requires-Dist: tenacity
32
+ Description-Content-Type: text/markdown
33
+
34
+ ![Swarmauri Logo](https://raw.githubusercontent.com/swarmauri/swarmauri-sdk/master/assets/swarmauri_sdk_brand.png)
35
+
36
+ <p align="center">
37
+ <a href="https://pepy.tech/project/swarmauri_middleware_ratepolicy/">
38
+ <img src="https://static.pepy.tech/badge/swarmauri_middleware_ratepolicy/month" alt="PyPI - Downloads"/></a>
39
+ <a href="https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/community/swarmauri_middleware_ratepolicy/">
40
+ <img alt="Hits" src="https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/community/swarmauri_middleware_ratepolicy.svg"/></a>
41
+ <a href="https://pypi.org/project/swarmauri_middleware_ratepolicy/">
42
+ <img src="https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12%20%7C%203.13%20%7C%203.14-blue" alt="PyPI - Python Version"/></a>
43
+ <a href="https://pypi.org/project/swarmauri_middleware_ratepolicy/">
44
+ <img src="https://img.shields.io/pypi/l/swarmauri_middleware_ratepolicy" alt="PyPI - License"/></a>
45
+ <a href="https://pypi.org/project/swarmauri_middleware_ratepolicy/">
46
+ <img src="https://img.shields.io/pypi/v/swarmauri_middleware_ratepolicy?label=swarmauri_middleware_ratepolicy&color=green" alt="PyPI - swarmauri_middleware_ratepolicy"/></a>
47
+ <a href="https://discord.gg/N4UpBuQv8T">
48
+ <img src="https://img.shields.io/badge/Discord-Join%20Chat-5865F2?logo=discord&logoColor=white" alt="Discord"/></a></p>
49
+
50
+ # Swarmauri Middleware Rate Policy
51
+
52
+ `swarmauri_middleware_ratepolicy` is the Swarmauri retry-policy middleware for
53
+ wrapping request-like call flows with bounded retry attempts and exponential
54
+ backoff. It provides a synchronous `dispatch(request, call_next)` surface that
55
+ retries failing operations before finally re-raising the exception.
56
+
57
+ ## Why Use Swarmauri Middleware Rate Policy
58
+
59
+ - Retry flaky upstream operations with a consistent middleware surface.
60
+ - Bound retry counts while still applying exponential backoff.
61
+ - Add basic resilience to synchronous request-processing or job-execution
62
+ pipelines.
63
+ - Combine with circuit breakers and logging to build layered fault handling.
64
+
65
+ ## FAQ
66
+
67
+ > **What parameters does this middleware expose?**
68
+ > `max_retries` and `initial_wait`.
69
+
70
+ > **How is the wait interval calculated?**
71
+ > The middleware sleeps for `initial_wait * 2**attempts` between retries.
72
+
73
+ > **Is the middleware async?**
74
+ > No. The current implementation is synchronous.
75
+
76
+ > **What happens after the retry limit is reached?**
77
+ > The last exception is re-raised.
78
+
79
+ ## Features
80
+
81
+ - Bounded retry loops over a synchronous `call_next` callable.
82
+ - Exponential backoff using `initial_wait`.
83
+ - Log messages for processing, retry attempts, and success.
84
+ - Swarmauri `MiddlewareBase` integration for reusable resilience flows.
85
+ - Supports Python 3.10, 3.11, 3.12, 3.13, and 3.14.
86
+
87
+ ## Installation
88
+
89
+ ```bash
90
+ uv add swarmauri_middleware_ratepolicy
91
+ ```
92
+
93
+ ```bash
94
+ pip install swarmauri_middleware_ratepolicy
95
+ ```
96
+
97
+ ## Usage
98
+
99
+ ```python
100
+ from swarmauri_middleware_ratepolicy import RetryPolicyMiddleware
101
+
102
+ retry = RetryPolicyMiddleware(max_retries=3, initial_wait=0.5)
103
+
104
+ class RequestEnvelope:
105
+ def __init__(self, payload: str):
106
+ self.payload = payload
107
+
108
+ request = RequestEnvelope("work-item-123")
109
+
110
+ def call_next(req):
111
+ raise RuntimeError("Simulated upstream failure")
112
+
113
+ retry.dispatch(request, call_next)
114
+ ```
115
+
116
+ ## Examples
117
+
118
+ ### Retry a failing operation
119
+
120
+ ```python
121
+ import logging
122
+ from swarmauri_middleware_ratepolicy import RetryPolicyMiddleware
123
+
124
+ logging.basicConfig(level=logging.INFO)
125
+
126
+ retry = RetryPolicyMiddleware(max_retries=3, initial_wait=0.25)
127
+
128
+ def unstable(_request):
129
+ raise RuntimeError("Temporary failure")
130
+
131
+ retry.dispatch({"task": "sync"}, unstable)
132
+ ```
133
+
134
+ ### Wrap an external API call
135
+
136
+ ```python
137
+ import httpx
138
+ from swarmauri_middleware_ratepolicy import RetryPolicyMiddleware
139
+
140
+ retry = RetryPolicyMiddleware(max_retries=4, initial_wait=0.25)
141
+
142
+ class RequestWrapper:
143
+ def __init__(self, url: str):
144
+ self.url = url
145
+
146
+ response = retry.dispatch(
147
+ RequestWrapper("https://api.example.com/data"),
148
+ lambda req: httpx.get(req.url, timeout=5),
149
+ )
150
+
151
+ print(response.status_code)
152
+ ```
153
+
154
+ ### Register in a larger middleware stack
155
+
156
+ ```python
157
+ from swarmauri_middleware_ratepolicy import RetryPolicyMiddleware
158
+
159
+ middleware = RetryPolicyMiddleware(max_retries=2, initial_wait=1.0)
160
+ print(middleware.type)
161
+ ```
162
+
163
+ ## Related Packages
164
+
165
+ - [swarmauri_middleware_circuitbreaker](https://pypi.org/project/swarmauri_middleware_circuitbreaker/)
166
+ - [swarmauri_state_clipboard](https://pypi.org/project/swarmauri_state_clipboard/)
167
+ - [swarmauri_middleware_ratepolicy](https://pypi.org/project/swarmauri_middleware_ratepolicy/)
168
+
169
+ ## Swarmauri Foundations
170
+
171
+ - [swarmauri](https://pypi.org/project/swarmauri/)
172
+ - [swarmauri_core](https://pypi.org/project/swarmauri_core/)
173
+ - [swarmauri_base](https://pypi.org/project/swarmauri_base/)
174
+ - [swarmauri_standard](https://pypi.org/project/swarmauri_standard/)
175
+
176
+ ## More Documentation
177
+
178
+ - [Tenacity documentation](https://tenacity.readthedocs.io/en/stable/index.html)
179
+
180
+ ## Best Practices
181
+
182
+ - Keep retry counts small for latency-sensitive user requests.
183
+ - Use circuit breakers when a dependency is consistently unhealthy instead of
184
+ only increasing retries.
185
+ - Apply retries around idempotent operations whenever possible.
186
+ - Convert async flows deliberately if you need to wrap them with this
187
+ synchronous middleware surface.
188
+
189
+ ## License
190
+
191
+ This project is licensed under the Apache-2.0 License.
192
+
193
+
194
+
@@ -0,0 +1,160 @@
1
+ ![Swarmauri Logo](https://raw.githubusercontent.com/swarmauri/swarmauri-sdk/master/assets/swarmauri_sdk_brand.png)
2
+
3
+ <p align="center">
4
+ <a href="https://pepy.tech/project/swarmauri_middleware_ratepolicy/">
5
+ <img src="https://static.pepy.tech/badge/swarmauri_middleware_ratepolicy/month" alt="PyPI - Downloads"/></a>
6
+ <a href="https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/community/swarmauri_middleware_ratepolicy/">
7
+ <img alt="Hits" src="https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/community/swarmauri_middleware_ratepolicy.svg"/></a>
8
+ <a href="https://pypi.org/project/swarmauri_middleware_ratepolicy/">
9
+ <img src="https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12%20%7C%203.13%20%7C%203.14-blue" alt="PyPI - Python Version"/></a>
10
+ <a href="https://pypi.org/project/swarmauri_middleware_ratepolicy/">
11
+ <img src="https://img.shields.io/pypi/l/swarmauri_middleware_ratepolicy" alt="PyPI - License"/></a>
12
+ <a href="https://pypi.org/project/swarmauri_middleware_ratepolicy/">
13
+ <img src="https://img.shields.io/pypi/v/swarmauri_middleware_ratepolicy?label=swarmauri_middleware_ratepolicy&color=green" alt="PyPI - swarmauri_middleware_ratepolicy"/></a>
14
+ <a href="https://discord.gg/N4UpBuQv8T">
15
+ <img src="https://img.shields.io/badge/Discord-Join%20Chat-5865F2?logo=discord&logoColor=white" alt="Discord"/></a></p>
16
+
17
+ # Swarmauri Middleware Rate Policy
18
+
19
+ `swarmauri_middleware_ratepolicy` is the Swarmauri retry-policy middleware for
20
+ wrapping request-like call flows with bounded retry attempts and exponential
21
+ backoff. It provides a synchronous `dispatch(request, call_next)` surface that
22
+ retries failing operations before finally re-raising the exception.
23
+
24
+ ## Why Use Swarmauri Middleware Rate Policy
25
+
26
+ - Retry flaky upstream operations with a consistent middleware surface.
27
+ - Bound retry counts while still applying exponential backoff.
28
+ - Add basic resilience to synchronous request-processing or job-execution
29
+ pipelines.
30
+ - Combine with circuit breakers and logging to build layered fault handling.
31
+
32
+ ## FAQ
33
+
34
+ > **What parameters does this middleware expose?**
35
+ > `max_retries` and `initial_wait`.
36
+
37
+ > **How is the wait interval calculated?**
38
+ > The middleware sleeps for `initial_wait * 2**attempts` between retries.
39
+
40
+ > **Is the middleware async?**
41
+ > No. The current implementation is synchronous.
42
+
43
+ > **What happens after the retry limit is reached?**
44
+ > The last exception is re-raised.
45
+
46
+ ## Features
47
+
48
+ - Bounded retry loops over a synchronous `call_next` callable.
49
+ - Exponential backoff using `initial_wait`.
50
+ - Log messages for processing, retry attempts, and success.
51
+ - Swarmauri `MiddlewareBase` integration for reusable resilience flows.
52
+ - Supports Python 3.10, 3.11, 3.12, 3.13, and 3.14.
53
+
54
+ ## Installation
55
+
56
+ ```bash
57
+ uv add swarmauri_middleware_ratepolicy
58
+ ```
59
+
60
+ ```bash
61
+ pip install swarmauri_middleware_ratepolicy
62
+ ```
63
+
64
+ ## Usage
65
+
66
+ ```python
67
+ from swarmauri_middleware_ratepolicy import RetryPolicyMiddleware
68
+
69
+ retry = RetryPolicyMiddleware(max_retries=3, initial_wait=0.5)
70
+
71
+ class RequestEnvelope:
72
+ def __init__(self, payload: str):
73
+ self.payload = payload
74
+
75
+ request = RequestEnvelope("work-item-123")
76
+
77
+ def call_next(req):
78
+ raise RuntimeError("Simulated upstream failure")
79
+
80
+ retry.dispatch(request, call_next)
81
+ ```
82
+
83
+ ## Examples
84
+
85
+ ### Retry a failing operation
86
+
87
+ ```python
88
+ import logging
89
+ from swarmauri_middleware_ratepolicy import RetryPolicyMiddleware
90
+
91
+ logging.basicConfig(level=logging.INFO)
92
+
93
+ retry = RetryPolicyMiddleware(max_retries=3, initial_wait=0.25)
94
+
95
+ def unstable(_request):
96
+ raise RuntimeError("Temporary failure")
97
+
98
+ retry.dispatch({"task": "sync"}, unstable)
99
+ ```
100
+
101
+ ### Wrap an external API call
102
+
103
+ ```python
104
+ import httpx
105
+ from swarmauri_middleware_ratepolicy import RetryPolicyMiddleware
106
+
107
+ retry = RetryPolicyMiddleware(max_retries=4, initial_wait=0.25)
108
+
109
+ class RequestWrapper:
110
+ def __init__(self, url: str):
111
+ self.url = url
112
+
113
+ response = retry.dispatch(
114
+ RequestWrapper("https://api.example.com/data"),
115
+ lambda req: httpx.get(req.url, timeout=5),
116
+ )
117
+
118
+ print(response.status_code)
119
+ ```
120
+
121
+ ### Register in a larger middleware stack
122
+
123
+ ```python
124
+ from swarmauri_middleware_ratepolicy import RetryPolicyMiddleware
125
+
126
+ middleware = RetryPolicyMiddleware(max_retries=2, initial_wait=1.0)
127
+ print(middleware.type)
128
+ ```
129
+
130
+ ## Related Packages
131
+
132
+ - [swarmauri_middleware_circuitbreaker](https://pypi.org/project/swarmauri_middleware_circuitbreaker/)
133
+ - [swarmauri_state_clipboard](https://pypi.org/project/swarmauri_state_clipboard/)
134
+ - [swarmauri_middleware_ratepolicy](https://pypi.org/project/swarmauri_middleware_ratepolicy/)
135
+
136
+ ## Swarmauri Foundations
137
+
138
+ - [swarmauri](https://pypi.org/project/swarmauri/)
139
+ - [swarmauri_core](https://pypi.org/project/swarmauri_core/)
140
+ - [swarmauri_base](https://pypi.org/project/swarmauri_base/)
141
+ - [swarmauri_standard](https://pypi.org/project/swarmauri_standard/)
142
+
143
+ ## More Documentation
144
+
145
+ - [Tenacity documentation](https://tenacity.readthedocs.io/en/stable/index.html)
146
+
147
+ ## Best Practices
148
+
149
+ - Keep retry counts small for latency-sensitive user requests.
150
+ - Use circuit breakers when a dependency is consistently unhealthy instead of
151
+ only increasing retries.
152
+ - Apply retries around idempotent operations whenever possible.
153
+ - Convert async flows deliberately if you need to wrap them with this
154
+ synchronous middleware surface.
155
+
156
+ ## License
157
+
158
+ This project is licensed under the Apache-2.0 License.
159
+
160
+
@@ -1,16 +1,20 @@
1
1
  [project]
2
2
  name = "swarmauri_middleware_ratepolicy"
3
- version = "0.8.4.dev2"
4
- description = "A middleware implementing rate policy and retry logic for Swarmauri"
3
+ version = "0.11.0.dev1"
4
+ description = "Synchronous retry-policy middleware for Swarmauri with bounded attempts and exponential backoff."
5
5
  license = "Apache-2.0"
6
6
  readme = "README.md"
7
- repository = "http://github.com/swarmauri/swarmauri-sdk/pkgs/pkgs/swarmauri_middleware_ratepolicy/"
7
+ repository = "http://github.com/swarmauri/swarmauri-sdk"
8
8
  requires-python = ">=3.10,<3.15"
9
9
  classifiers = [
10
10
  "License :: OSI Approved :: Apache Software License",
11
11
  "Natural Language :: English",
12
12
  "Development Status :: 3 - Alpha",
13
13
  "Intended Audience :: Developers",
14
+ "Intended Audience :: System Administrators",
15
+ "Operating System :: OS Independent",
16
+ "Topic :: Internet :: WWW/HTTP",
17
+ "Topic :: Software Development :: Libraries",
14
18
  "Topic :: Software Development :: Libraries :: Application Frameworks",
15
19
  "Programming Language :: Python",
16
20
  "Programming Language :: Python :: 3",
@@ -34,13 +38,11 @@ keywords = [
34
38
  "ratepolicy",
35
39
  "retry",
36
40
  "tenacity",
37
- "async",
38
- "api",
39
- "framework",
40
- "implementing",
41
- "rate",
42
- "policy",
43
- "logic",
41
+ "backoff",
42
+ "resilience",
43
+ "fault tolerance",
44
+ "request middleware",
45
+ "retry policy",
44
46
  ]
45
47
 
46
48
  [tool.uv.sources]
@@ -80,7 +82,7 @@ dev = [
80
82
  "pytest-mock>=3.12.0",
81
83
  "pytest-json-report>=1.5.0",
82
84
  "python-dotenv",
83
- "requests>=2.32.3",
85
+ "httpx>=0.27",
84
86
  "flake8>=7.0",
85
87
  "pytest-timeout>=2.3.1",
86
88
  "ruff>=0.9.9",
@@ -88,4 +90,4 @@ dev = [
88
90
  ]
89
91
 
90
92
  [project.entry-points.'swarmauri.middlewares']
91
- RetryPolicyMiddleware = "swarmauri_middleware_ratepolicy:RetryPolicyMiddleware"
93
+ RetryPolicyMiddleware = "swarmauri_middleware_ratepolicy:RetryPolicyMiddleware"
@@ -1,144 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: swarmauri_middleware_ratepolicy
3
- Version: 0.8.4.dev2
4
- Summary: A middleware implementing rate policy and retry logic for Swarmauri
5
- License-Expression: Apache-2.0
6
- License-File: LICENSE
7
- Keywords: swarmauri,middleware,ratepolicy,retry,tenacity,async,api,framework,implementing,rate,policy,logic
8
- Author: Jacob Stewart
9
- Author-email: jacob@swarmauri.com
10
- Requires-Python: >=3.10,<3.15
11
- Classifier: License :: OSI Approved :: Apache Software License
12
- Classifier: Natural Language :: English
13
- Classifier: Development Status :: 3 - Alpha
14
- Classifier: Intended Audience :: Developers
15
- Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
16
- Classifier: Programming Language :: Python
17
- Classifier: Programming Language :: Python :: 3
18
- Classifier: Programming Language :: Python :: 3 :: Only
19
- Classifier: Programming Language :: Python :: 3.10
20
- Classifier: Programming Language :: Python :: 3.11
21
- Classifier: Programming Language :: Python :: 3.12
22
- Classifier: Programming Language :: Python :: 3.13
23
- Classifier: Programming Language :: Python :: 3.14
24
- Requires-Dist: swarmauri_base
25
- Requires-Dist: swarmauri_core
26
- Requires-Dist: swarmauri_standard
27
- Requires-Dist: tenacity
28
- Description-Content-Type: text/markdown
29
-
30
- ![Swarmauri Logo](https://raw.githubusercontent.com/swarmauri/swarmauri-sdk/master/assets/swarmauri_sdk_brand.png)
31
-
32
- <p align="center">
33
- <a href="https://pepy.tech/project/swarmauri_middleware_ratepolicy/">
34
- <img src="https://static.pepy.tech/badge/swarmauri_middleware_ratepolicy/month" alt="PyPI - Downloads"/></a>
35
- <a href="https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/community/swarmauri_middleware_ratepolicy/">
36
- <img alt="Hits" src="https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/community/swarmauri_middleware_ratepolicy.svg"/></a>
37
- <a href="https://pypi.org/project/swarmauri_middleware_ratepolicy/">
38
- <img src="https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12%20%7C%203.13%20%7C%203.14-blue" alt="PyPI - Python Version"/></a>
39
- <a href="https://pypi.org/project/swarmauri_middleware_ratepolicy/">
40
- <img src="https://img.shields.io/pypi/l/swarmauri_middleware_ratepolicy" alt="PyPI - License"/></a>
41
- <a href="https://pypi.org/project/swarmauri_middleware_ratepolicy/">
42
- <img src="https://img.shields.io/pypi/v/swarmauri_middleware_ratepolicy?label=swarmauri_middleware_ratepolicy&color=green" alt="PyPI - swarmauri_middleware_ratepolicy"/></a>
43
- </p>
44
- ---
45
-
46
- # Swarmauri Middleware Ratepolicy
47
-
48
- Retry-policy middleware for Swarmauri services. Provides exponential backoff with configurable retry attempts and wait intervals so unreliable upstream calls can be retried transparently.
49
-
50
- ## Features
51
-
52
- - Implements Swarmauri's `MiddlewareBase` contract; wrap any callable sequence (FastAPI routes, job runners, etc.).
53
- - Configurable `max_retries` and `initial_wait` seconds. Wait time doubles on each retry (`initial_wait * 2**attempt`).
54
- - Emits structured logs on retry attempts and successes for observability.
55
- - Simple synchronous dispatch; wrap async callables by providing a sync shim that executes the coroutine (see example below).
56
-
57
- ## Prerequisites
58
-
59
- - Python 3.10 or newer.
60
- - A Swarmauri application or FastAPI project that supports middleware registration.
61
-
62
- ## Installation
63
-
64
- ```bash
65
- # pip
66
- pip install swarmauri_middleware_ratepolicy
67
-
68
- # poetry
69
- poetry add swarmauri_middleware_ratepolicy
70
-
71
- # uv (pyproject-based projects)
72
- uv add swarmauri_middleware_ratepolicy
73
- ```
74
-
75
- ## Quickstart
76
-
77
- ```python
78
- import logging
79
- from swarmauri_middleware_ratepolicy import RetryPolicyMiddleware
80
-
81
- logging.basicConfig(level=logging.INFO)
82
-
83
- retry_middleware = RetryPolicyMiddleware(max_retries=3, initial_wait=0.5)
84
-
85
- class RequestEnvelope:
86
- def __init__(self, payload: str):
87
- self.payload = payload
88
-
89
- request = RequestEnvelope("work-item-123")
90
-
91
- def call_next(req: RequestEnvelope):
92
- raise RuntimeError("Simulated upstream failure")
93
-
94
- retry_middleware.dispatch(request, call_next)
95
- ```
96
-
97
- With Swarmauri's middleware stack (or FastAPI), register it just like other Swarmauri middleware:
98
-
99
- ```python
100
- from swarmauri_app.middleware import middleware_stack
101
- from swarmauri_middleware_ratepolicy import RetryPolicyMiddleware
102
-
103
- middleware_stack.add_middleware(
104
- RetryPolicyMiddleware,
105
- max_retries=4,
106
- initial_wait=0.25,
107
- )
108
- ```
109
-
110
- ## Example: Wrapping an External API Call
111
-
112
- ```python
113
- import logging
114
- import requests
115
- from swarmauri_middleware_ratepolicy import RetryPolicyMiddleware
116
-
117
- logging.basicConfig(level=logging.INFO)
118
-
119
- retry = RetryPolicyMiddleware(max_retries=4, initial_wait=0.25)
120
-
121
- class RequestWrapper:
122
- def __init__(self, url: str):
123
- self.url = url
124
-
125
- wrapper = RequestWrapper("https://api.example.com/data")
126
-
127
- response = retry.dispatch(
128
- wrapper,
129
- lambda req: requests.get(req.url, timeout=5),
130
- )
131
- print(response.status_code)
132
- ```
133
-
134
- ## Tips
135
-
136
- - Keep `max_retries` small for user-facing endpoints to avoid long wait chains; rely on background queues for bulk retries.
137
- - Combine with the circuit breaker middleware for layered resilience (circuit breaker opens when repeated retries fail).
138
- - When wrapping async callables, convert them to sync functions using `asyncio.run` or `anyio.from_thread` to fit the middleware signature.
139
- - Capture logs at INFO level to trace retry attempts in production.
140
-
141
- ## Want to help?
142
-
143
- If you want to contribute to swarmauri-sdk, read up on our [guidelines for contributing](https://github.com/swarmauri/swarmauri-sdk/blob/master/contributing.md) that will help you get started.
144
-
@@ -1,114 +0,0 @@
1
- ![Swarmauri Logo](https://raw.githubusercontent.com/swarmauri/swarmauri-sdk/master/assets/swarmauri_sdk_brand.png)
2
-
3
- <p align="center">
4
- <a href="https://pepy.tech/project/swarmauri_middleware_ratepolicy/">
5
- <img src="https://static.pepy.tech/badge/swarmauri_middleware_ratepolicy/month" alt="PyPI - Downloads"/></a>
6
- <a href="https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/community/swarmauri_middleware_ratepolicy/">
7
- <img alt="Hits" src="https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/community/swarmauri_middleware_ratepolicy.svg"/></a>
8
- <a href="https://pypi.org/project/swarmauri_middleware_ratepolicy/">
9
- <img src="https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12%20%7C%203.13%20%7C%203.14-blue" alt="PyPI - Python Version"/></a>
10
- <a href="https://pypi.org/project/swarmauri_middleware_ratepolicy/">
11
- <img src="https://img.shields.io/pypi/l/swarmauri_middleware_ratepolicy" alt="PyPI - License"/></a>
12
- <a href="https://pypi.org/project/swarmauri_middleware_ratepolicy/">
13
- <img src="https://img.shields.io/pypi/v/swarmauri_middleware_ratepolicy?label=swarmauri_middleware_ratepolicy&color=green" alt="PyPI - swarmauri_middleware_ratepolicy"/></a>
14
- </p>
15
- ---
16
-
17
- # Swarmauri Middleware Ratepolicy
18
-
19
- Retry-policy middleware for Swarmauri services. Provides exponential backoff with configurable retry attempts and wait intervals so unreliable upstream calls can be retried transparently.
20
-
21
- ## Features
22
-
23
- - Implements Swarmauri's `MiddlewareBase` contract; wrap any callable sequence (FastAPI routes, job runners, etc.).
24
- - Configurable `max_retries` and `initial_wait` seconds. Wait time doubles on each retry (`initial_wait * 2**attempt`).
25
- - Emits structured logs on retry attempts and successes for observability.
26
- - Simple synchronous dispatch; wrap async callables by providing a sync shim that executes the coroutine (see example below).
27
-
28
- ## Prerequisites
29
-
30
- - Python 3.10 or newer.
31
- - A Swarmauri application or FastAPI project that supports middleware registration.
32
-
33
- ## Installation
34
-
35
- ```bash
36
- # pip
37
- pip install swarmauri_middleware_ratepolicy
38
-
39
- # poetry
40
- poetry add swarmauri_middleware_ratepolicy
41
-
42
- # uv (pyproject-based projects)
43
- uv add swarmauri_middleware_ratepolicy
44
- ```
45
-
46
- ## Quickstart
47
-
48
- ```python
49
- import logging
50
- from swarmauri_middleware_ratepolicy import RetryPolicyMiddleware
51
-
52
- logging.basicConfig(level=logging.INFO)
53
-
54
- retry_middleware = RetryPolicyMiddleware(max_retries=3, initial_wait=0.5)
55
-
56
- class RequestEnvelope:
57
- def __init__(self, payload: str):
58
- self.payload = payload
59
-
60
- request = RequestEnvelope("work-item-123")
61
-
62
- def call_next(req: RequestEnvelope):
63
- raise RuntimeError("Simulated upstream failure")
64
-
65
- retry_middleware.dispatch(request, call_next)
66
- ```
67
-
68
- With Swarmauri's middleware stack (or FastAPI), register it just like other Swarmauri middleware:
69
-
70
- ```python
71
- from swarmauri_app.middleware import middleware_stack
72
- from swarmauri_middleware_ratepolicy import RetryPolicyMiddleware
73
-
74
- middleware_stack.add_middleware(
75
- RetryPolicyMiddleware,
76
- max_retries=4,
77
- initial_wait=0.25,
78
- )
79
- ```
80
-
81
- ## Example: Wrapping an External API Call
82
-
83
- ```python
84
- import logging
85
- import requests
86
- from swarmauri_middleware_ratepolicy import RetryPolicyMiddleware
87
-
88
- logging.basicConfig(level=logging.INFO)
89
-
90
- retry = RetryPolicyMiddleware(max_retries=4, initial_wait=0.25)
91
-
92
- class RequestWrapper:
93
- def __init__(self, url: str):
94
- self.url = url
95
-
96
- wrapper = RequestWrapper("https://api.example.com/data")
97
-
98
- response = retry.dispatch(
99
- wrapper,
100
- lambda req: requests.get(req.url, timeout=5),
101
- )
102
- print(response.status_code)
103
- ```
104
-
105
- ## Tips
106
-
107
- - Keep `max_retries` small for user-facing endpoints to avoid long wait chains; rely on background queues for bulk retries.
108
- - Combine with the circuit breaker middleware for layered resilience (circuit breaker opens when repeated retries fail).
109
- - When wrapping async callables, convert them to sync functions using `asyncio.run` or `anyio.from_thread` to fit the middleware signature.
110
- - Capture logs at INFO level to trace retry attempts in production.
111
-
112
- ## Want to help?
113
-
114
- If you want to contribute to swarmauri-sdk, read up on our [guidelines for contributing](https://github.com/swarmauri/swarmauri-sdk/blob/master/contributing.md) that will help you get started.