Skip to content

Commit 2a2e44e

Browse files
box-sdk-buildbox-sdk-build
andauthored
feat(boxsdkgen): Setup common default timeout (box/box-codegen#965) (#1904)
Co-authored-by: box-sdk-build <box-sdk-build@box.com>
1 parent ce54ef6 commit 2a2e44e

6 files changed

Lines changed: 47 additions & 8 deletions

File tree

‎.codegen.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "engineHash": "6f9492d", "specHash": "131c54a", "version": "5.13.0" }
1+
{ "engineHash": "ed5236c", "specHash": "131c54a", "version": "5.13.0" }

‎docs/sdkgen/client.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,13 @@ BoxClient clientWithInterceptor = client.withInterceptors(interceptors);
183183

184184
In order to configure timeout for API calls, calling the `client.withTimeouts(config)` method creates a new client with timeout settings, leaving the original client unmodified.
185185

186+
All timeout values are in milliseconds.
187+
186188
```java
187189
TimeoutConfig timeoutConfig = new TimeoutConfig.Builder()
188-
.connectionTimeoutMs(10000L)
190+
.connectionTimeoutMs(5000L)
189191
.readTimeoutMs(30000L)
192+
.requestTimeoutMs(60000L)
190193
.build();
191194
BoxClient newClient = client.withTimeouts(timeoutConfig);
192195
```

‎docs/sdkgen/configuration.md‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,20 @@ BoxClient client = new BoxClient.Builder(auth)
178178
## Timeouts
179179

180180
You can configure network timeouts with `TimeoutConfig` on `NetworkSession`.
181-
Java SDK supports separate values for connection and read timeouts, both in milliseconds.
181+
The SDK supports three timeout values, all in milliseconds:
182+
183+
| Parameter | Description |
184+
| --------------------- | -------------------------------------------------------------------------------------------------------- |
185+
| `connectionTimeoutMs` | Maximum time to wait for the TCP connection to be established. Maps to OkHttp `connectTimeout`. |
186+
| `readTimeoutMs` | Maximum idle time between data packets while reading the response. Maps to OkHttp `readTimeout`. |
187+
| `requestTimeoutMs` | Maximum total time for the entire HTTP request (connect + send + receive). Maps to OkHttp `callTimeout`. |
182188

183189
```java
184190
BoxDeveloperTokenAuth auth = new BoxDeveloperTokenAuth("DEVELOPER_TOKEN");
185191
TimeoutConfig timeoutConfig = new TimeoutConfig.Builder()
186-
.connectionTimeoutMs(10000L)
192+
.connectionTimeoutMs(5000L)
187193
.readTimeoutMs(30000L)
194+
.requestTimeoutMs(60000L)
188195
.build();
189196

190197
NetworkSession session = new NetworkSession()
@@ -199,10 +206,10 @@ How timeout handling works:
199206

200207
- `connectionTimeoutMs` controls how long the client waits to establish a connection.
201208
- `readTimeoutMs` controls how long the client waits for data while reading the response.
202-
- If timeout config is not provided, the SDK uses the OkHttp default timeout settings: connect timeout of 10 seconds, read timeout of 10 seconds, and write timeout of 10 seconds.
209+
- `requestTimeoutMs` controls the maximum total time for the entire request lifecycle, including connection, sending the request body, and reading the response.
210+
- If timeout config is not provided, the SDK uses default timeouts: `connectionTimeoutMs: 10000` (10 seconds), `readTimeoutMs: 60000` (60 seconds), and `requestTimeoutMs: 21600000` (6 hours).
203211
- Each timeout is optional. If a value is not provided, the client keeps its existing timeout for that setting.
204-
- To disable both timeouts, set `connectionTimeoutMs(0L)` and `readTimeoutMs(0L)`.
205-
- You can also disable only one timeout by setting just one of them to `0L` and leaving the other configured.
206-
- Timeout failures are handled as request exceptions, then retry behavior is controlled by the configured retry strategy
212+
- To disable a timeout, set its value to `0L`.
213+
- Timeout failures are handled as request exceptions, then retry behavior is controlled by the configured retry strategy.
207214
- If retries are exhausted after timeout failures, the SDK throws `BoxSDKError` with the underlying timeout exception as the cause.
208215
- Timeout applies to a single HTTP request attempt to the Box API (not the total time across all retries).

‎src/main/java/com/box/sdkgen/networking/boxnetworkclient/BoxNetworkClient.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ public BoxNetworkClient withTimeoutConfig(TimeoutConfig config) {
120120
}
121121
clientBuilder.readTimeout(readTimeoutMs.longValue(), TimeUnit.MILLISECONDS);
122122
}
123+
124+
Long requestTimeoutMs = config.getRequestTimeoutMs();
125+
if (requestTimeoutMs != null) {
126+
if (requestTimeoutMs < 0) {
127+
throw new IllegalArgumentException("requestTimeoutMs cannot be negative");
128+
}
129+
clientBuilder.callTimeout(requestTimeoutMs.longValue(), TimeUnit.MILLISECONDS);
130+
}
123131
return new BoxNetworkClient(clientBuilder.build());
124132
}
125133

‎src/main/java/com/box/sdkgen/networking/network/NetworkSession.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ public NetworkSession() {
3838
networkClient = new BoxNetworkClient();
3939
retryStrategy = new BoxRetryStrategy();
4040
dataSanitizer = new DataSanitizer();
41+
timeoutConfig =
42+
new TimeoutConfig.Builder()
43+
.connectionTimeoutMs(10000L)
44+
.readTimeoutMs(60000L)
45+
.requestTimeoutMs(21600000L)
46+
.build();
47+
networkClient = ((BoxNetworkClient) networkClient).withTimeoutConfig(timeoutConfig);
4148
}
4249

4350
protected NetworkSession(Builder builder) {

‎src/main/java/com/box/sdkgen/networking/timeoutconfig/TimeoutConfig.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ public class TimeoutConfig {
66

77
public Long readTimeoutMs;
88

9+
public Long requestTimeoutMs;
10+
911
public TimeoutConfig() {}
1012

1113
protected TimeoutConfig(Builder builder) {
1214
this.connectionTimeoutMs = builder.connectionTimeoutMs;
1315
this.readTimeoutMs = builder.readTimeoutMs;
16+
this.requestTimeoutMs = builder.requestTimeoutMs;
1417
}
1518

1619
public Long getConnectionTimeoutMs() {
@@ -21,12 +24,18 @@ public Long getReadTimeoutMs() {
2124
return readTimeoutMs;
2225
}
2326

27+
public Long getRequestTimeoutMs() {
28+
return requestTimeoutMs;
29+
}
30+
2431
public static class Builder {
2532

2633
protected Long connectionTimeoutMs;
2734

2835
protected Long readTimeoutMs;
2936

37+
protected Long requestTimeoutMs;
38+
3039
public Builder connectionTimeoutMs(Long connectionTimeoutMs) {
3140
this.connectionTimeoutMs = connectionTimeoutMs;
3241
return this;
@@ -37,6 +46,11 @@ public Builder readTimeoutMs(Long readTimeoutMs) {
3746
return this;
3847
}
3948

49+
public Builder requestTimeoutMs(Long requestTimeoutMs) {
50+
this.requestTimeoutMs = requestTimeoutMs;
51+
return this;
52+
}
53+
4054
public TimeoutConfig build() {
4155
return new TimeoutConfig(this);
4256
}

0 commit comments

Comments
 (0)