Skip to content

Commit 2620fc3

Browse files
authored
add: Doc for Smart Retry Middleware (#460)
1 parent 27b63e0 commit 2620fc3

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

docs/available-components/middlewares.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,57 @@ async def test():
3434

3535
`retry_on_error` enables retries for a task. `max_retries` is the maximum number of times,.
3636

37+
## Smart Retry Middleware
38+
39+
The `SmartRetryMiddleware` automatically retries tasks with flexible delay settings and retry strategies when errors occur. This is particularly useful when tasks fail due to temporary issues, such as network errors or temporary unavailability of external services.
40+
41+
### Key Features:
42+
43+
* **Retry Limits**: Set the maximum number of retry attempts (`max_retries`).
44+
* **Delay Before Retry**: Define a fixed delay or use additional strategies.
45+
* **Jitter**: Adds random delay variations to distribute retries evenly and prevent simultaneous task execution.
46+
* **Exponential Backoff**: Increases the delay for each subsequent retry, useful for gradually recovering system performance.
47+
* **Custom Schedule Source (`schedule_source`)**: Enables using a custom schedule source to manage delayed task execution.
48+
49+
### Middleware Integration
50+
51+
To use `SmartRetryMiddleware`, add it to the list of middlewares in your broker:
52+
53+
```python
54+
from taskiq import ZeroMQBroker
55+
from taskiq.middlewares import SmartRetryMiddleware
56+
57+
broker = ZeroMQBroker().with_middlewares(
58+
SmartRetryMiddleware(
59+
default_retry_count=5,
60+
default_delay=10,
61+
use_jitter=True,
62+
use_delay_exponent=True,
63+
max_delay_exponent=120
64+
),
65+
)
66+
```
67+
68+
### Using Middleware with Tasks
69+
70+
To enable retries for a specific task, specify the following parameters:
71+
72+
```python
73+
@broker.task(retry_on_error=True, max_retries=10, delay=15)
74+
async def my_task():
75+
raise Exception("Error, retrying!")
76+
```
77+
78+
* `retry_on_error`: Enables the retry mechanism for the specific task.
79+
* `max_retries`: Maximum number of retries (overrides middleware default).
80+
* `delay`: Initial delay before retrying the task, in seconds.
81+
82+
### Usage Recommendations
83+
84+
Use jitter and exponential backoff to avoid repetitive load peaks, especially in high-load systems. Choose appropriate `max_delay_exponent` values to prevent excessively long intervals between retries if your task execution is time-sensitive.
85+
86+
87+
3788
### Prometheus middleware
3889

3990
You can enable prometheus metrics for workers by adding PrometheusMiddleware.

0 commit comments

Comments
 (0)