diff options
author | Valerij Fredriksen <valerijf@oath.com> | 2018-01-05 14:26:56 +0100 |
---|---|---|
committer | Valerij Fredriksen <valerijf@oath.com> | 2018-01-09 15:03:05 +0100 |
commit | e152b313428c6acc2ccefa00f8d01013c9b4f7f2 (patch) | |
tree | f4971594a73138dfa41f0bd9364eebfd1e0c72c2 /node-admin | |
parent | f7d95b78b2e89b1658783517d21318be3fd9a315 (diff) |
Create self-closeable HTTP client
Diffstat (limited to 'node-admin')
-rw-r--r-- | node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/util/SelfCloseableHttpClient.java | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/util/SelfCloseableHttpClient.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/util/SelfCloseableHttpClient.java new file mode 100644 index 00000000000..dd1421ca853 --- /dev/null +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/util/SelfCloseableHttpClient.java @@ -0,0 +1,72 @@ +package com.yahoo.vespa.hosted.node.admin.util; + +import com.yahoo.log.LogLevel; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.config.Registry; +import org.apache.http.config.RegistryBuilder; +import org.apache.http.conn.socket.ConnectionSocketFactory; +import org.apache.http.conn.socket.PlainConnectionSocketFactory; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; + +import java.io.IOException; +import java.util.logging.Logger; + +/** + * @author freva + */ +class SelfCloseableHttpClient { + + private static final Logger log = Logger.getLogger(SelfCloseableHttpClient.class.getName()); + + private final CloseableHttpClient httpClient; + + SelfCloseableHttpClient() { + this(SSLConnectionSocketFactory.getSocketFactory()); + } + + SelfCloseableHttpClient(SSLConnectionSocketFactory sslConnectionSocketFactory) { + Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() + .register("http", PlainConnectionSocketFactory.getSocketFactory()) + .register("https", sslConnectionSocketFactory) + .build(); + + PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry); + cm.setMaxTotal(200); // Increase max total connections to 200, which should be enough + + // Have experienced hang in socket read, which may have been because of + // system defaults, therefore set explicit timeouts. Set arbitrarily to + // 15s > 10s used by Orchestrator lock timeout. + int timeoutMs = 15_000; + RequestConfig requestBuilder = RequestConfig.custom() + .setConnectTimeout(timeoutMs) // establishment of connection + .setConnectionRequestTimeout(timeoutMs) // connection from connection manager + .setSocketTimeout(timeoutMs) // waiting for data + .build(); + + this.httpClient = HttpClientBuilder.create() + .setDefaultRequestConfig(requestBuilder) + .disableAutomaticRetries() + .setUserAgent("node-admin") + .setConnectionManager(cm).build(); + } + + public CloseableHttpResponse execute(HttpUriRequest request) throws IOException { + return httpClient.execute(request); + } + + @Override + public void finalize() throws Throwable { + try { + httpClient.close(); + } catch (Exception e) { + log.log(LogLevel.WARNING, "Ignoring exception thrown when closing http client", e); + } + + super.finalize(); + } +} |