Skip to content

Commit 1402f6c

Browse files
docs: Improve documentation for retry strategies (box/box-codegen#925) (#1347)
1 parent d33db7c commit 1402f6c

2 files changed

Lines changed: 136 additions & 11 deletions

File tree

.codegen.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "engineHash": "f36ed52", "specHash": "77eac4b", "version": "10.4.0" }
1+
{ "engineHash": "482939a", "specHash": "77eac4b", "version": "10.4.0" }

docs/configuration.md

Lines changed: 135 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,134 @@
33
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
44
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
55

6-
- [Max retry attempts](#max-retry-attempts)
7-
- [Custom retry strategy](#custom-retry-strategy)
6+
- [Retry Strategy](#retry-strategy)
7+
- [Overview](#overview)
8+
- [Default Configuration](#default-configuration)
9+
- [Retry Decision Flow](#retry-decision-flow)
10+
- [Exponential Backoff Algorithm](#exponential-backoff-algorithm)
11+
- [Example Delays (with default settings)](#example-delays-with-default-settings)
12+
- [Retry-After Header](#retry-after-header)
13+
- [Network Exception Handling](#network-exception-handling)
14+
- [Customizing Retry Parameters](#customizing-retry-parameters)
15+
- [Custom Retry Strategy](#custom-retry-strategy)
816

917
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
1018

11-
## Max retry attempts
19+
## Retry Strategy
1220

13-
The default maximum number of retries in case of failed API call is 5.
14-
To change this number you should initialize `BoxRetryStrategy` with the new value and pass it to `NetworkSession`.
21+
### Overview
22+
23+
The SDK ships with a built-in retry strategy (`BoxRetryStrategy`) that implements the `RetryStrategy` interface. The `BoxNetworkClient`, which serves as the default network client, uses this strategy to automatically retry failed API requests with exponential backoff.
24+
25+
The retry strategy exposes two methods:
26+
27+
- **`should_retry`** — Determines whether a failed request should be retried based on the HTTP status code, response headers, attempt count, and authentication state.
28+
- **`retry_after`** — Computes the delay (in seconds) before the next retry attempt, using either the server-provided `Retry-After` header or an exponential backoff formula.
29+
30+
### Default Configuration
31+
32+
| Parameter | Default | Description |
33+
| ---------------------------- | ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
34+
| `max_attempts` | `5` | Maximum number of retry attempts for HTTP error responses (status 4xx/5xx). |
35+
| `retry_base_interval` | `1` (second) | Base interval used in the exponential backoff calculation. |
36+
| `retry_randomization_factor` | `0.5` | Jitter factor applied to the backoff delay. The actual delay is multiplied by a random value between `1 - factor` and `1 + factor`. |
37+
| `max_retries_on_exception` | `2` | Maximum number of retries for network-level exceptions (connection failures, timeouts). These are tracked by a separate counter from HTTP error retries. |
38+
39+
### Retry Decision Flow
40+
41+
The following diagram shows how `BoxRetryStrategy.should_retry` decides whether to retry a request:
42+
43+
```
44+
should_retry(fetch_options, fetch_response, attempt_number)
45+
|
46+
v
47+
+-----------------------+
48+
| status == 0 | Yes
49+
| (network exception)? |----------> attempt_number <= max_retries_on_exception?
50+
+-----------------------+ | |
51+
| No Yes No
52+
v | |
53+
+-----------------------+ [RETRY] [NO RETRY]
54+
| attempt_number >= |
55+
| max_attempts? |
56+
+-----------------------+
57+
| |
58+
Yes No
59+
| |
60+
[NO RETRY] v
61+
+-----------------------+
62+
| status == 202 AND | Yes
63+
| Retry-After header? |----------> [RETRY]
64+
+-----------------------+
65+
| No
66+
v
67+
+-----------------------+
68+
| status >= 500 | Yes
69+
| (server error)? |----------> [RETRY]
70+
+-----------------------+
71+
| No
72+
v
73+
+-----------------------+
74+
| status == 429 | Yes
75+
| (rate limited)? |----------> [RETRY]
76+
+-----------------------+
77+
| No
78+
v
79+
+-----------------------+
80+
| status == 401 AND | Yes
81+
| auth available? |----------> Refresh token, then [RETRY]
82+
+-----------------------+
83+
| No
84+
v
85+
[NO RETRY]
86+
```
87+
88+
### Exponential Backoff Algorithm
89+
90+
When the response does not include a `Retry-After` header, the retry delay is computed using exponential backoff with randomized jitter:
91+
92+
```
93+
delay = 2^attempt_number * retry_base_interval * random(1 - factor, 1 + factor)
94+
```
95+
96+
Where:
97+
98+
- `attempt_number` is the current attempt (1-based)
99+
- `retry_base_interval` defaults to `1` second
100+
- `factor` is `retry_randomization_factor` (default `0.5`)
101+
- `random(min, max)` returns a uniformly distributed value in `[min, max]`
102+
103+
#### Example Delays (with default settings)
104+
105+
| Attempt | Base Delay | Min Delay (factor=0.5) | Max Delay (factor=0.5) |
106+
| ------- | ---------- | ---------------------- | ---------------------- |
107+
| 1 | 2s | 1.0s | 3.0s |
108+
| 2 | 4s | 2.0s | 6.0s |
109+
| 3 | 8s | 4.0s | 12.0s |
110+
| 4 | 16s | 8.0s | 24.0s |
111+
112+
### Retry-After Header
113+
114+
When the server includes a `Retry-After` header in the response, the SDK uses the header value directly as the delay in seconds instead of computing an exponential backoff delay. This applies to any retryable response that includes the header, including:
115+
116+
- `202 Accepted` with `Retry-After` (long-running operations)
117+
- `429 Too Many Requests` with `Retry-After`
118+
- `5xx` server errors with `Retry-After`
119+
120+
The header value is parsed as a floating-point number representing seconds.
121+
122+
### Network Exception Handling
123+
124+
Network-level failures (connection refused, DNS resolution errors, timeouts, TLS errors) are represented internally as responses with status `0`. These exceptions are tracked by a **separate counter** (`max_retries_on_exception`, default `2`) from the regular HTTP error retry counter (`max_attempts`).
125+
126+
This means:
127+
128+
- Network exception retries are tracked independently from HTTP error retries, each with their own counter and backoff progression.
129+
- A request can fail up to `max_retries_on_exception` times due to network exceptions, but each exception retry also increments the overall attempt counter, so the total number of retries across both exception and HTTP error types is bounded by `max_attempts`.
130+
131+
### Customizing Retry Parameters
132+
133+
You can customize all retry parameters by initializing `BoxRetryStrategy` with the desired values and passing it to `NetworkSession`:
15134

16135
```python
17136
from box_sdk_gen import (
@@ -22,14 +141,20 @@ from box_sdk_gen import (
22141
)
23142

24143
auth = BoxDeveloperTokenAuth(token="DEVELOPER_TOKEN_GOES_HERE")
25-
network_session = NetworkSession(retry_strategy=BoxRetryStrategy(max_attempts=6))
144+
network_session = NetworkSession(
145+
retry_strategy=BoxRetryStrategy(
146+
max_attempts=3,
147+
retry_base_interval=2,
148+
retry_randomization_factor=0.3,
149+
max_retries_on_exception=1,
150+
)
151+
)
26152
client = BoxClient(auth=auth, network_session=network_session)
27153
```
28154

29-
## Custom retry strategy
155+
### Custom Retry Strategy
30156

31-
You can also implement your own retry strategy by subclassing `RetryStrategy` and overriding `should_retry` and `retry_after` methods.
32-
This example shows how to set custom strategy that retries on 5xx status codes and waits 1 second between retries.
157+
You can implement your own retry strategy by subclassing `RetryStrategy` and overriding the `should_retry` and `retry_after` methods:
33158

34159
```python
35160
from box_sdk_gen import (
@@ -49,7 +174,7 @@ class CustomRetryStrategy(RetryStrategy):
49174
fetch_response: FetchResponse,
50175
attempt_number: int,
51176
) -> bool:
52-
return fetch_response.status_code >= 500
177+
return fetch_response.status >= 500 and attempt_number < 3
53178

54179
def retry_after(
55180
self,

0 commit comments

Comments
 (0)