From 4fbcb6950c14290ca0f3530d68fd8b906ba33bf6 Mon Sep 17 00:00:00 2001 From: box-sdk-build Date: Mon, 16 Feb 2026 02:48:54 -0800 Subject: [PATCH 1/3] test: assume that exception is thrown from delete archive (box/box-codegen#927) --- .codegen.json | 2 +- test/archives.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.codegen.json b/.codegen.json index cf002cd64..16f59e2de 100644 --- a/.codegen.json +++ b/.codegen.json @@ -1 +1 @@ -{ "engineHash": "bfb97cc", "specHash": "77eac4b", "version": "10.4.0" } +{ "engineHash": "f36ed52", "specHash": "77eac4b", "version": "10.4.0" } diff --git a/test/archives.py b/test/archives.py index 65c2e6ed1..83d3ef39f 100644 --- a/test/archives.py +++ b/test/archives.py @@ -37,6 +37,5 @@ def testArchivesCreateListDelete(): assert updated_archive.description == new_archive_description archives: ArchivesV2025R0 = client.archives.get_archives_v2025_r0(limit=100) assert len(archives.entries) > 0 - client.archives.delete_archive_by_id_v2025_r0(archive.id) with pytest.raises(Exception): client.archives.delete_archive_by_id_v2025_r0(archive.id) From 02ddd1741eb2c0c79d1fcf6fb2b6744b943b6309 Mon Sep 17 00:00:00 2001 From: box-sdk-build Date: Mon, 16 Feb 2026 06:18:59 -0800 Subject: [PATCH 2/3] chore: Update `.codegen.json` with commit hash of `codegen` and `openapi` spec [skip ci] --- .codegen.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.codegen.json b/.codegen.json index 16f59e2de..bc25f9a93 100644 --- a/.codegen.json +++ b/.codegen.json @@ -1 +1 @@ -{ "engineHash": "f36ed52", "specHash": "77eac4b", "version": "10.4.0" } +{ "engineHash": "9dcb945", "specHash": "77eac4b", "version": "10.4.0" } From 0cfcb96eb0a6d7bb65728c0ea2490bceefae746c Mon Sep 17 00:00:00 2001 From: box-sdk-build Date: Mon, 16 Feb 2026 09:19:07 -0800 Subject: [PATCH 3/3] docs: Improve documentation for retry strategies (box/box-codegen#925) --- .codegen.json | 2 +- docs/configuration.md | 145 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 136 insertions(+), 11 deletions(-) diff --git a/.codegen.json b/.codegen.json index bc25f9a93..f8eaf4e6a 100644 --- a/.codegen.json +++ b/.codegen.json @@ -1 +1 @@ -{ "engineHash": "9dcb945", "specHash": "77eac4b", "version": "10.4.0" } +{ "engineHash": "482939a", "specHash": "77eac4b", "version": "10.4.0" } diff --git a/docs/configuration.md b/docs/configuration.md index 4c10d7f8e..3ec5fa9c9 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -3,15 +3,134 @@ -- [Max retry attempts](#max-retry-attempts) -- [Custom retry strategy](#custom-retry-strategy) +- [Retry Strategy](#retry-strategy) + - [Overview](#overview) + - [Default Configuration](#default-configuration) + - [Retry Decision Flow](#retry-decision-flow) + - [Exponential Backoff Algorithm](#exponential-backoff-algorithm) + - [Example Delays (with default settings)](#example-delays-with-default-settings) + - [Retry-After Header](#retry-after-header) + - [Network Exception Handling](#network-exception-handling) + - [Customizing Retry Parameters](#customizing-retry-parameters) + - [Custom Retry Strategy](#custom-retry-strategy) -## Max retry attempts +## Retry Strategy -The default maximum number of retries in case of failed API call is 5. -To change this number you should initialize `BoxRetryStrategy` with the new value and pass it to `NetworkSession`. +### Overview + +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. + +The retry strategy exposes two methods: + +- **`should_retry`** — Determines whether a failed request should be retried based on the HTTP status code, response headers, attempt count, and authentication state. +- **`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. + +### Default Configuration + +| Parameter | Default | Description | +| ---------------------------- | ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `max_attempts` | `5` | Maximum number of retry attempts for HTTP error responses (status 4xx/5xx). | +| `retry_base_interval` | `1` (second) | Base interval used in the exponential backoff calculation. | +| `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`. | +| `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. | + +### Retry Decision Flow + +The following diagram shows how `BoxRetryStrategy.should_retry` decides whether to retry a request: + +``` + should_retry(fetch_options, fetch_response, attempt_number) + | + v + +-----------------------+ + | status == 0 | Yes + | (network exception)? |----------> attempt_number <= max_retries_on_exception? + +-----------------------+ | | + | No Yes No + v | | + +-----------------------+ [RETRY] [NO RETRY] + | attempt_number >= | + | max_attempts? | + +-----------------------+ + | | + Yes No + | | + [NO RETRY] v + +-----------------------+ + | status == 202 AND | Yes + | Retry-After header? |----------> [RETRY] + +-----------------------+ + | No + v + +-----------------------+ + | status >= 500 | Yes + | (server error)? |----------> [RETRY] + +-----------------------+ + | No + v + +-----------------------+ + | status == 429 | Yes + | (rate limited)? |----------> [RETRY] + +-----------------------+ + | No + v + +-----------------------+ + | status == 401 AND | Yes + | auth available? |----------> Refresh token, then [RETRY] + +-----------------------+ + | No + v + [NO RETRY] +``` + +### Exponential Backoff Algorithm + +When the response does not include a `Retry-After` header, the retry delay is computed using exponential backoff with randomized jitter: + +``` +delay = 2^attempt_number * retry_base_interval * random(1 - factor, 1 + factor) +``` + +Where: + +- `attempt_number` is the current attempt (1-based) +- `retry_base_interval` defaults to `1` second +- `factor` is `retry_randomization_factor` (default `0.5`) +- `random(min, max)` returns a uniformly distributed value in `[min, max]` + +#### Example Delays (with default settings) + +| Attempt | Base Delay | Min Delay (factor=0.5) | Max Delay (factor=0.5) | +| ------- | ---------- | ---------------------- | ---------------------- | +| 1 | 2s | 1.0s | 3.0s | +| 2 | 4s | 2.0s | 6.0s | +| 3 | 8s | 4.0s | 12.0s | +| 4 | 16s | 8.0s | 24.0s | + +### Retry-After Header + +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: + +- `202 Accepted` with `Retry-After` (long-running operations) +- `429 Too Many Requests` with `Retry-After` +- `5xx` server errors with `Retry-After` + +The header value is parsed as a floating-point number representing seconds. + +### Network Exception Handling + +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`). + +This means: + +- Network exception retries are tracked independently from HTTP error retries, each with their own counter and backoff progression. +- 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`. + +### Customizing Retry Parameters + +You can customize all retry parameters by initializing `BoxRetryStrategy` with the desired values and passing it to `NetworkSession`: ```python from box_sdk_gen import ( @@ -22,14 +141,20 @@ from box_sdk_gen import ( ) auth = BoxDeveloperTokenAuth(token="DEVELOPER_TOKEN_GOES_HERE") -network_session = NetworkSession(retry_strategy=BoxRetryStrategy(max_attempts=6)) +network_session = NetworkSession( + retry_strategy=BoxRetryStrategy( + max_attempts=3, + retry_base_interval=2, + retry_randomization_factor=0.3, + max_retries_on_exception=1, + ) +) client = BoxClient(auth=auth, network_session=network_session) ``` -## Custom retry strategy +### Custom Retry Strategy -You can also implement your own retry strategy by subclassing `RetryStrategy` and overriding `should_retry` and `retry_after` methods. -This example shows how to set custom strategy that retries on 5xx status codes and waits 1 second between retries. +You can implement your own retry strategy by subclassing `RetryStrategy` and overriding the `should_retry` and `retry_after` methods: ```python from box_sdk_gen import ( @@ -49,7 +174,7 @@ class CustomRetryStrategy(RetryStrategy): fetch_response: FetchResponse, attempt_number: int, ) -> bool: - return fetch_response.status_code >= 500 + return fetch_response.status >= 500 and attempt_number < 3 def retry_after( self,