Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ public JdkHttpTransport(HttpClient client, HttpTransportConfig config) {
}

private static HttpClient buildClient(HttpTransportConfig config) {
// set connection pool for jdk http client
// refer:
// https://docs.oracle.com/en/java/javase/25/docs/api/java.net.http/module-summary.html
if (System.getProperty("jdk.httpclient.connectionPoolSize") == null) {
System.setProperty(
"jdk.httpclient.connectionPoolSize",
String.valueOf(config.getMaxIdleConnections()));
Comment on lines +109 to +111
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit concerned that this might affect other parts of the user's application that use the JDK HTTP Client.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, maybe it will be better that the properties be configured by user.

}

if (System.getProperty("jdk.httpclient.keepalive.timeout") == null) {
System.setProperty(
"jdk.httpclient.keepalive.timeout",
String.valueOf(config.getKeepAliveDuration().getSeconds()));
}

HttpClient.Builder builder =
HttpClient.newBuilder()
.version(Version.HTTP_2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,9 @@ private OkHttpClient buildClient(HttpTransportConfig config) {
log.warn(
"SSL certificate verification is disabled. This is not recommended for"
+ " production.");
builder =
builder.sslSocketFactory(
createTrustAllSslSocketFactory(), createTrustAllTrustManager())
.hostnameVerifier((hostname, session) -> true);

builder.sslSocketFactory(createTrustAllSslSocketFactory(), createTrustAllTrustManager())
.hostnameVerifier((hostname, session) -> true);
}

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1047,4 +1047,35 @@ void testStreamNdJsonFormatWithoutHeaderDefaultsToSse() {
"{\"id\":\"2\",\"text\":\"World\"}") // This is sent before [DONE] marker
.verifyComplete();
}

@Test
void testConnectionPoolSystemProperties() {
System.clearProperty("jdk.httpclient.connectionPoolSize");
System.clearProperty("jdk.httpclient.keepalive.timeout");
JdkHttpTransport.builder()
.config(
HttpTransportConfig.builder()
.maxIdleConnections(10)
.keepAliveDuration(Duration.ofMinutes(5))
.build())
.build();
assertEquals("10", System.getProperty("jdk.httpclient.connectionPoolSize"));
assertEquals("300", System.getProperty("jdk.httpclient.keepalive.timeout"));
}

@Test
void testConnectionPoolSystemPropertiesAlreadySet() {
System.setProperty("jdk.httpclient.connectionPoolSize", "5");
System.setProperty("jdk.httpclient.keepalive.timeout", "180");

JdkHttpTransport.builder()
.config(
HttpTransportConfig.builder()
.maxIdleConnections(10)
.keepAliveDuration(Duration.ofMinutes(5))
.build())
.build();
assertEquals("5", System.getProperty("jdk.httpclient.connectionPoolSize"));
assertEquals("180", System.getProperty("jdk.httpclient.keepalive.timeout"));
}
}
Loading