Skip to content

Commit aae0b76

Browse files
docs(boxsdkgen): Improve documentation for retry strategies (box/box-codegen#925) (#1346)
1 parent fc7c789 commit aae0b76

2 files changed

Lines changed: 153 additions & 14 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": "4.4.0" }
1+
{ "engineHash": "482939a", "specHash": "77eac4b", "version": "4.4.0" }

docs/box_sdk_gen/configuration.md

Lines changed: 152 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,175 @@
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 BoxClient, BoxDeveloperTokenAuth, NetworkSession, BoxRetryStrategy
18137

19138
auth = BoxDeveloperTokenAuth(token='DEVELOPER_TOKEN_GOES_HERE')
20-
network_session = NetworkSession(retry_strategy=BoxRetryStrategy(max_attempts=6))
139+
network_session = NetworkSession(
140+
retry_strategy=BoxRetryStrategy(
141+
max_attempts=3,
142+
retry_base_interval=2,
143+
retry_randomization_factor=0.3,
144+
max_retries_on_exception=1,
145+
)
146+
)
21147
client = BoxClient(auth=auth, network_session=network_session)
22148
```
23149

24-
## Custom retry strategy
150+
### Custom Retry Strategy
25151

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

29154
```python
30-
from box_sdk_gen import BoxClient, BoxDeveloperTokenAuth, NetworkSession, RetryStrategy, FetchOptions, FetchResponse
155+
from box_sdk_gen import (
156+
BoxClient, BoxDeveloperTokenAuth, NetworkSession,
157+
RetryStrategy, FetchOptions, FetchResponse,
158+
)
31159

32160
class CustomRetryStrategy(RetryStrategy):
33-
def should_retry(self, fetch_options: FetchOptions, fetch_response: FetchResponse, attempt_number: int) -> bool:
34-
return fetch_response.status_code >= 500
35-
def retry_after(self, fetch_options: FetchOptions, fetch_response: FetchResponse, attempt_number: int) -> float:
161+
def should_retry(
162+
self,
163+
fetch_options: FetchOptions,
164+
fetch_response: FetchResponse,
165+
attempt_number: int,
166+
) -> bool:
167+
return fetch_response.status >= 500 and attempt_number < 3
168+
169+
def retry_after(
170+
self,
171+
fetch_options: FetchOptions,
172+
fetch_response: FetchResponse,
173+
attempt_number: int,
174+
) -> float:
36175
return 1.0
37176

38177
auth = BoxDeveloperTokenAuth(token='DEVELOPER_TOKEN_GOES_HERE')

0 commit comments

Comments
 (0)