Skip to content

Commit 3e7025e

Browse files
box-sdk-buildbox-sdk-build
andauthored
docs: Improve documentation for retry strategies (box/box-codegen#925) (#1358)
Co-authored-by: box-sdk-build <[email protected]>
1 parent fca811e commit 3e7025e

File tree

2 files changed

+136
-13
lines changed

2 files changed

+136
-13
lines changed

.codegen.json

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

docs/configuration.md

Lines changed: 135 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,160 @@
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
1522

16-
```js
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+
- **`shouldRetry`** — Determines whether a failed request should be retried based on the HTTP status code, response headers, attempt count, and authentication state.
28+
- **`retryAfter`** — 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+
| `maxAttempts` | `5` | Maximum number of retry attempts for HTTP error responses (status 4xx/5xx). |
35+
| `retryBaseInterval` | `1` (second) | Base interval used in the exponential backoff calculation. |
36+
| `retryRandomizationFactor` | `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+
| `maxRetriesOnException` | `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.shouldRetry` decides whether to retry a request:
42+
43+
```
44+
shouldRetry(fetchOptions, fetchResponse, attemptNumber)
45+
|
46+
v
47+
+-----------------------+
48+
| status == 0 | Yes
49+
| (network exception)? |----------> attemptNumber <= maxRetriesOnException?
50+
+-----------------------+ | |
51+
| No Yes No
52+
v | |
53+
+-----------------------+ [RETRY] [NO RETRY]
54+
| attemptNumber >= |
55+
| maxAttempts? |
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^attemptNumber * retryBaseInterval * random(1 - factor, 1 + factor)
94+
```
95+
96+
Where:
97+
98+
- `attemptNumber` is the current attempt (1-based)
99+
- `retryBaseInterval` defaults to `1` second
100+
- `factor` is `retryRandomizationFactor` (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** (`maxRetriesOnException`, default `2`) from the regular HTTP error retry counter (`maxAttempts`).
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 `maxRetriesOnException` 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 `maxAttempts`.
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`:
134+
135+
```ts
17136
const auth = new BoxDeveloperTokenAuth({ token: 'DEVELOPER_TOKEN_GOES_HERE' });
18137
const networkSession = new NetworkSession({
19-
retryStrategy: new BoxRetryStrategy({ maxAttempts: 6 }),
138+
retryStrategy: new BoxRetryStrategy({
139+
maxAttempts: 3,
140+
retryBaseInterval: 2,
141+
retryRandomizationFactor: 0.3,
142+
maxRetriesOnException: 1,
143+
}),
20144
});
21145
const client = new BoxClient({ auth, networkSession });
22146
```
23147

24-
## Custom retry strategy
148+
### Custom Retry Strategy
25149

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

29152
```ts
30-
export class CustomRetryStrategy implements RetryStrategy {
153+
class CustomRetryStrategy implements RetryStrategy {
31154
async shouldRetry(
32155
fetchOptions: FetchOptions,
33156
fetchResponse: FetchResponse,
34157
attemptNumber: number,
35158
): Promise<boolean> {
36-
return false;
159+
return fetchResponse.status >= 500 && attemptNumber < 3;
37160
}
38161

39162
retryAfter(

0 commit comments

Comments
 (0)