Skip to content

Commit ce927e5

Browse files
Add withRequestConfig to CommonsHttpClient.Builder
Allow users to pass a custom Apache RequestConfig for fine-grained timeout control (e.g. short connect timeout with long socket timeout). The existing withTimeoutSeconds remains for the common case. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a6044f4 commit ce927e5

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

databricks-sdk-java/src/main/java/com/databricks/sdk/core/commons/CommonsHttpClient.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class CommonsHttpClient implements HttpClient {
4747
public static class Builder {
4848
private DatabricksConfig databricksConfig;
4949
private Integer timeoutSeconds;
50+
private RequestConfig requestConfig;
5051
private ProxyConfig proxyConfig;
5152
private SSLConnectionSocketFactory sslSocketFactory;
5253
private PoolingHttpClientConnectionManager connectionManager;
@@ -65,14 +66,27 @@ public Builder withDatabricksConfig(DatabricksConfig databricksConfig) {
6566

6667
/**
6768
* @param timeoutSeconds The timeout in seconds to use for the HttpClient. This will override
68-
* any timeout set in the DatabricksConfig.
69+
* any timeout set in the DatabricksConfig. Sets all three Apache HttpClient timeouts
70+
* (connect, socket, connection request) to the same value. For fine-grained control, use
71+
* {@link #withRequestConfig(RequestConfig)} instead.
6972
* @return This builder.
7073
*/
7174
public Builder withTimeoutSeconds(int timeoutSeconds) {
7275
this.timeoutSeconds = timeoutSeconds;
7376
return this;
7477
}
7578

79+
/**
80+
* @param requestConfig The Apache {@link RequestConfig} to use for the HttpClient. When set,
81+
* this takes precedence over {@link #withTimeoutSeconds(int)} and any timeout from {@link
82+
* DatabricksConfig}.
83+
* @return This builder.
84+
*/
85+
public Builder withRequestConfig(RequestConfig requestConfig) {
86+
this.requestConfig = requestConfig;
87+
return this;
88+
}
89+
7690
/**
7791
* @param proxyConfig the proxy configuration to use for the HttpClient.
7892
* @return This builder.
@@ -121,17 +135,12 @@ public CommonsHttpClient build() {
121135
private final CloseableHttpClient hc;
122136

123137
private CommonsHttpClient(Builder builder) {
124-
int timeoutSeconds = 300;
125-
if (builder.databricksConfig != null
126-
&& builder.databricksConfig.getHttpTimeoutSeconds() != null) {
127-
timeoutSeconds = builder.databricksConfig.getHttpTimeoutSeconds();
128-
}
129-
if (builder.timeoutSeconds != null) {
130-
timeoutSeconds = builder.timeoutSeconds;
131-
}
132-
int timeout = timeoutSeconds * 1000;
138+
RequestConfig requestConfig =
139+
builder.requestConfig != null
140+
? builder.requestConfig
141+
: makeDefaultRequestConfig(builder.databricksConfig, builder.timeoutSeconds);
133142
HttpClientBuilder httpClientBuilder =
134-
HttpClientBuilder.create().setDefaultRequestConfig(makeRequestConfig(timeout));
143+
HttpClientBuilder.create().setDefaultRequestConfig(requestConfig);
135144
if (builder.proxyConfig != null) {
136145
ProxyUtils.setupProxy(builder.proxyConfig, httpClientBuilder);
137146
}
@@ -156,7 +165,16 @@ private CommonsHttpClient(Builder builder) {
156165
hc = httpClientBuilder.build();
157166
}
158167

159-
private RequestConfig makeRequestConfig(int timeout) {
168+
private static RequestConfig makeDefaultRequestConfig(
169+
DatabricksConfig databricksConfig, Integer timeoutSecondsOverride) {
170+
int timeoutSeconds = 300;
171+
if (databricksConfig != null && databricksConfig.getHttpTimeoutSeconds() != null) {
172+
timeoutSeconds = databricksConfig.getHttpTimeoutSeconds();
173+
}
174+
if (timeoutSecondsOverride != null) {
175+
timeoutSeconds = timeoutSecondsOverride;
176+
}
177+
int timeout = timeoutSeconds * 1000;
160178
return RequestConfig.custom()
161179
.setConnectionRequestTimeout(timeout)
162180
.setConnectTimeout(timeout)

databricks-sdk-java/src/test/java/com/databricks/sdk/core/commons/CommonsHttpClientTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.io.IOException;
1010
import java.net.HttpURLConnection;
1111
import java.util.*;
12+
import org.apache.http.client.config.RequestConfig;
1213
import org.apache.commons.io.IOUtils;
1314
import org.junit.jupiter.api.Test;
1415

@@ -88,6 +89,23 @@ public void testNoRedirection() throws IOException {
8889
}
8990
}
9091

92+
@Test
93+
public void testCustomRequestConfig() throws IOException {
94+
try (FixtureServer fixtures = new FixtureServer().with("GET", "/foo", "ok", 200)) {
95+
RequestConfig requestConfig =
96+
RequestConfig.custom()
97+
.setConnectTimeout(10_000)
98+
.setSocketTimeout(900_000)
99+
.setConnectionRequestTimeout(300_000)
100+
.build();
101+
HttpClient httpClient =
102+
new CommonsHttpClient.Builder().withRequestConfig(requestConfig).build();
103+
Request in = new Request("GET", fixtures.getUrl() + "/foo");
104+
Response out = httpClient.execute(in);
105+
assertEquals("ok", out.getDebugBody().trim());
106+
}
107+
}
108+
91109
@Test
92110
public void testRedirection() throws IOException {
93111
List<FixtureServer.FixtureMapping> fixtures =

0 commit comments

Comments
 (0)