Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Configure ResponseTimeout for a Request, move off deprecated API by always configuring a PoolingHttpClientConnectionManager.

See gh-861
Original pull request: gh-866
  • Loading branch information
mp911de committed May 16, 2024
1 parent 579b8d6 commit 3cf4e01
Showing 1 changed file with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@
import okhttp3.OkHttpClient.Builder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.client5.http.impl.routing.SystemDefaultRoutePlanner;
import org.apache.hc.client5.http.ssl.HttpsSupport;
Expand Down Expand Up @@ -298,6 +298,28 @@ public static HttpClientBuilder getHttpClientBuilder(ClientOptions options, SslC
httpClientBuilder.setRoutePlanner(
new SystemDefaultRoutePlanner(DefaultSchemePortResolver.INSTANCE, ProxySelector.getDefault()));

Timeout readTimeout = Timeout.ofMilliseconds(options.getReadTimeout().toMillis());
Timeout connectTimeout = Timeout.ofMilliseconds(options.getConnectionTimeout().toMillis());

ConnectionConfig connectionConfig = ConnectionConfig.custom()
.setConnectTimeout(connectTimeout) //
.setSocketTimeout(readTimeout) //
.build();

RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(connectTimeout)
.setResponseTimeout(readTimeout)
.setAuthenticationEnabled(true) //
.setRedirectsEnabled(true)
.build();

PoolingHttpClientConnectionManagerBuilder connectionManagerBuilder = PoolingHttpClientConnectionManagerBuilder //
.create()
.setDefaultConnectionConfig(connectionConfig) //
.setDefaultSocketConfig(SocketConfig.custom() //
.setSoTimeout(readTimeout)
.build());

if (hasSslConfiguration(sslConfiguration)) {

SSLContext sslContext = getSSLContext(sslConfiguration);
Expand All @@ -316,24 +338,12 @@ public static HttpClientBuilder getHttpClientBuilder(ClientOptions options, SslC

SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext,
enabledProtocols, enabledCipherSuites, HttpsSupport.getDefaultHostnameVerifier());
PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder //
.create()
.setSSLSocketFactory(sslSocketFactory) //
.setDefaultSocketConfig(SocketConfig.custom() //
.setSoTimeout(Timeout.ofMilliseconds(options.getReadTimeout().toMillis()))
.build()) //
.build(); //
httpClientBuilder.setConnectionManager(connectionManager);
connectionManagerBuilder.setSSLSocketFactory(sslSocketFactory);
}

RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(Timeout.ofMilliseconds(options.getConnectionTimeout().toMillis()))
.setConnectionRequestTimeout(Timeout.ofMilliseconds(options.getReadTimeout().toMillis()))
.setAuthenticationEnabled(true) //
.setRedirectsEnabled(true)
.build();

httpClientBuilder.setDefaultRequestConfig(requestConfig);
httpClientBuilder.setConnectionManager(connectionManagerBuilder.build());

return httpClientBuilder;
}

Expand Down

0 comments on commit 3cf4e01

Please sign in to comment.