Skip to content

Commit 991b90b

Browse files
ed-irlcopybara-github
authored andcommitted
feat: Add read and write timeout options to Java GenAI SDK.
FUTURE_COPYBARA_INTEGRATE_REVIEW=#602 from ed-irl:ed-irl/548-add-timeouts-for-all-http-client-settings fa66abb PiperOrigin-RevId: 828551025
1 parent 659c65c commit 991b90b

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

src/main/java/com/google/genai/ApiClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ private OkHttpClient createHttpClient(
242242
Dispatcher dispatcher = new Dispatcher();
243243
options.maxConnections().ifPresent(dispatcher::setMaxRequests);
244244
options.maxConnectionsPerHost().ifPresent(dispatcher::setMaxRequestsPerHost);
245+
options
246+
.readTimeout()
247+
.ifPresent(readTimeout -> builder.readTimeout(Duration.ofMillis(readTimeout)));
248+
options
249+
.writeTimeout()
250+
.ifPresent(writeTimeout -> builder.writeTimeout(Duration.ofMillis(writeTimeout)));
245251
builder.dispatcher(dispatcher);
246252
});
247253

src/main/java/com/google/genai/types/ClientOptions.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ public abstract class ClientOptions extends JsonSerializable {
3838
@JsonProperty("maxConnectionsPerHost")
3939
public abstract Optional<Integer> maxConnectionsPerHost();
4040

41+
/** The read timeout for the request in milliseconds. */
42+
@JsonProperty("readTimeout")
43+
public abstract Optional<Integer> readTimeout();
44+
45+
/** The write timeout for the request in milliseconds. */
46+
@JsonProperty("writeTimeout")
47+
public abstract Optional<Integer> writeTimeout();
48+
4149
/** Instantiates a builder for ClientOptions. */
4250
@ExcludeFromGeneratedCoverageReport
4351
public static Builder builder() {
@@ -92,6 +100,42 @@ public Builder clearMaxConnectionsPerHost() {
92100
return maxConnectionsPerHost(Optional.empty());
93101
}
94102

103+
/**
104+
* Setter for readTimeout.
105+
*
106+
* <p>readTimeout: The read timeout for the request in milliseconds.
107+
*/
108+
@JsonProperty("readTimeout")
109+
public abstract Builder readTimeout(Integer readTimeout);
110+
111+
@ExcludeFromGeneratedCoverageReport
112+
abstract Builder readTimeout(Optional<Integer> readTimeout);
113+
114+
/** Clears the value of readTimeout field. */
115+
@ExcludeFromGeneratedCoverageReport
116+
@CanIgnoreReturnValue
117+
public Builder clearReadTimeout() {
118+
return readTimeout(Optional.empty());
119+
}
120+
121+
/**
122+
* Setter for writeTimeout.
123+
*
124+
* <p>writeTimeout: The write timeout for the request in milliseconds.
125+
*/
126+
@JsonProperty("writeTimeout")
127+
public abstract Builder writeTimeout(Integer writeTimeout);
128+
129+
@ExcludeFromGeneratedCoverageReport
130+
abstract Builder writeTimeout(Optional<Integer> writeTimeout);
131+
132+
/** Clears the value of writeTimeout field. */
133+
@ExcludeFromGeneratedCoverageReport
134+
@CanIgnoreReturnValue
135+
public Builder clearWriteTimeout() {
136+
return writeTimeout(Optional.empty());
137+
}
138+
95139
public abstract ClientOptions build();
96140
}
97141

src/test/java/com/google/genai/HttpApiClientTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -979,22 +979,25 @@ public void testHttpClientVertexWithRequestLevelRetryOptions() throws Exception
979979
@Test
980980
public void testHttpClientMldevCustomClientOptions() throws Exception {
981981
ClientOptions clientOptions =
982-
ClientOptions.builder().maxConnections(64).maxConnectionsPerHost(16).build();
982+
ClientOptions.builder().maxConnections(64).maxConnectionsPerHost(16).readTimeout(5000).writeTimeout(4000).build();
983983
HttpApiClient client =
984984
new HttpApiClient(Optional.of(API_KEY), Optional.empty(), Optional.of(clientOptions));
985985

986986
Dispatcher dispatcher = client.httpClient().dispatcher();
987+
OkHttpClient httpClient = client.httpClient();
987988

988989
assertEquals(API_KEY, client.apiKey());
989990
assertFalse(client.vertexAI());
990991
assertEquals(64, dispatcher.getMaxRequests());
991992
assertEquals(16, dispatcher.getMaxRequestsPerHost());
993+
assertEquals(5000, httpClient.readTimeoutMillis());
994+
assertEquals(4000, httpClient.writeTimeoutMillis());
992995
}
993996

994997
@Test
995998
public void testHttpClientVertexCustomClientOptions() throws Exception {
996999
ClientOptions clientOptions =
997-
ClientOptions.builder().maxConnections(64).maxConnectionsPerHost(16).build();
1000+
ClientOptions.builder().maxConnections(64).maxConnectionsPerHost(16).readTimeout(5000).writeTimeout(4000).build();
9981001
HttpApiClient client =
9991002
new HttpApiClient(
10001003
Optional.empty(),
@@ -1005,12 +1008,15 @@ public void testHttpClientVertexCustomClientOptions() throws Exception {
10051008
Optional.of(clientOptions));
10061009

10071010
Dispatcher dispatcher = client.httpClient().dispatcher();
1011+
OkHttpClient httpClient = client.httpClient();
10081012

10091013
assertEquals(PROJECT, client.project());
10101014
assertEquals(LOCATION, client.location());
10111015
assertTrue(client.vertexAI());
10121016
assertEquals(64, dispatcher.getMaxRequests());
10131017
assertEquals(16, dispatcher.getMaxRequestsPerHost());
1018+
assertEquals(5000, httpClient.readTimeoutMillis());
1019+
assertEquals(4000, httpClient.writeTimeoutMillis());
10141020
}
10151021

10161022
@Test
@@ -1019,6 +1025,7 @@ public void testHttpClientMldevDefaultClientOptions() throws Exception {
10191025
new HttpApiClient(Optional.of(API_KEY), Optional.empty(), Optional.empty());
10201026

10211027
Dispatcher dispatcher = client.httpClient().dispatcher();
1028+
OkHttpClient httpClient = client.httpClient();
10221029

10231030
// Default values for max connections and max connections per host are 64 and 5 respectively
10241031
// (set by OkHttp). When maxConnectionsPerHost is not set, the dispatcher's maxRequestsPerHost
@@ -1027,6 +1034,8 @@ public void testHttpClientMldevDefaultClientOptions() throws Exception {
10271034
assertEquals(5, dispatcher.getMaxRequestsPerHost());
10281035
assertEquals(API_KEY, client.apiKey());
10291036
assertFalse(client.vertexAI());
1037+
assertEquals(0, httpClient.readTimeoutMillis());
1038+
assertEquals(0, httpClient.writeTimeoutMillis());
10301039
}
10311040

10321041
@Test

0 commit comments

Comments
 (0)