|
@@ -26,6 +26,7 @@ import org.apache.http.client.methods.HttpRequestWrapper;
|
|
|
import org.apache.http.client.protocol.HttpClientContext;
|
|
|
import org.apache.http.client.utils.URIBuilder;
|
|
|
import org.apache.http.client.utils.URIUtils;
|
|
|
+import org.apache.http.config.SocketConfig;
|
|
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
|
import org.apache.http.entity.ByteArrayEntity;
|
|
|
import org.apache.http.entity.ContentType;
|
|
@@ -74,6 +75,7 @@ import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
|
public class HttpClient implements Closeable {
|
|
@@ -90,6 +92,8 @@ public class HttpClient implements Closeable {
|
|
|
private final HttpProxy settingsProxy;
|
|
|
private final TimeValue defaultConnectionTimeout;
|
|
|
private final TimeValue defaultReadTimeout;
|
|
|
+ private final boolean tcpKeepaliveEnabled;
|
|
|
+ private final TimeValue connectionPoolTtl;
|
|
|
private final ByteSizeValue maxResponseSize;
|
|
|
private final CryptoService cryptoService;
|
|
|
private final SSLService sslService;
|
|
@@ -97,6 +101,8 @@ public class HttpClient implements Closeable {
|
|
|
public HttpClient(Settings settings, SSLService sslService, CryptoService cryptoService, ClusterService clusterService) {
|
|
|
this.defaultConnectionTimeout = HttpSettings.CONNECTION_TIMEOUT.get(settings);
|
|
|
this.defaultReadTimeout = HttpSettings.READ_TIMEOUT.get(settings);
|
|
|
+ this.tcpKeepaliveEnabled = HttpSettings.TCP_KEEPALIVE.get(settings);
|
|
|
+ this.connectionPoolTtl = HttpSettings.CONNECTION_POOL_TTL.get(settings);
|
|
|
this.maxResponseSize = HttpSettings.MAX_HTTP_RESPONSE_SIZE.get(settings);
|
|
|
this.settingsProxy = getProxyFromSettings(settings);
|
|
|
this.cryptoService = cryptoService;
|
|
@@ -116,6 +122,16 @@ public class HttpClient implements Closeable {
|
|
|
SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslService.sslSocketFactory(sslConfiguration), verifier);
|
|
|
clientBuilder.setSSLSocketFactory(factory);
|
|
|
|
|
|
+ final SocketConfig.Builder socketConfigBuilder = SocketConfig.custom();
|
|
|
+ if (tcpKeepaliveEnabled) {
|
|
|
+ socketConfigBuilder.setSoKeepAlive(true);
|
|
|
+ }
|
|
|
+ clientBuilder.setDefaultSocketConfig(socketConfigBuilder.build());
|
|
|
+
|
|
|
+ if (connectionPoolTtl.millis() > 0) {
|
|
|
+ clientBuilder.setConnectionTimeToLive(connectionPoolTtl.millis(), TimeUnit.MILLISECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
clientBuilder.evictExpiredConnections();
|
|
|
clientBuilder.setMaxConnPerRoute(MAX_CONNECTIONS);
|
|
|
clientBuilder.setMaxConnTotal(MAX_CONNECTIONS);
|