diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2021-04-07 22:29:16 +0200 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2021-04-07 22:29:16 +0200 |
commit | 9d9d835752c5d35808276754ad7467b6ec64242c (patch) | |
tree | d4b4b93b8b3f8b490c64fbd997401bf2361fb323 /metrics-proxy/src | |
parent | 0f8387d7f21d392f9d7ab5ee60b10acce1dff4d8 (diff) |
Use async http the simple way.
Diffstat (limited to 'metrics-proxy/src')
-rw-r--r-- | metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/HttpMetricFetcher.java | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/HttpMetricFetcher.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/HttpMetricFetcher.java index bfbadc00628..81f2cce5558 100644 --- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/HttpMetricFetcher.java +++ b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/service/HttpMetricFetcher.java @@ -1,14 +1,17 @@ // Copyright 2020 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package ai.vespa.metricsproxy.service; -import ai.vespa.util.http.hc5.VespaHttpClientBuilder; +import ai.vespa.util.http.hc5.VespaAsyncHttpClientBuilder; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; import java.util.logging.Level; + import com.yahoo.yolean.Exceptions; -import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; +import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; import org.apache.hc.client5.http.config.RequestConfig; -import org.apache.hc.client5.http.impl.classic.BasicHttpClientResponseHandler; -import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; import org.apache.hc.core5.util.Timeout; import java.io.IOException; @@ -30,8 +33,7 @@ public abstract class HttpMetricFetcher { private final static int SOCKET_TIMEOUT = 60000; private final URI url; protected final VespaService service; - private static final CloseableHttpClient httpClient = createHttpClient(); - + private static final CloseableHttpAsyncClient httpClient = createHttpClient(); /** * @param service the service to fetch metrics from @@ -47,7 +49,12 @@ public abstract class HttpMetricFetcher { String getJson() throws IOException { log.log(Level.FINE, "Connecting to url " + url + " for service '" + service + "'"); - return httpClient.execute(new HttpGet(url), new BasicHttpClientResponseHandler()); + Future<SimpleHttpResponse> response = httpClient.execute(new SimpleHttpRequest("GET", url), null); + try { + return response.get().getBodyText(); + } catch (InterruptedException | ExecutionException e) { + throw new IOException("Failed fetching '" + url + "': " + e); + } } public String toString() { @@ -80,14 +87,16 @@ public abstract class HttpMetricFetcher { } } - private static CloseableHttpClient createHttpClient() { - return VespaHttpClientBuilder.create() + private static CloseableHttpAsyncClient createHttpClient() { + CloseableHttpAsyncClient client = VespaAsyncHttpClientBuilder.create() .setUserAgent("metrics-proxy-http-client") .setDefaultRequestConfig(RequestConfig.custom() .setConnectTimeout(Timeout.ofMilliseconds(CONNECTION_TIMEOUT)) .setResponseTimeout(Timeout.ofMilliseconds(SOCKET_TIMEOUT)) .build()) .build(); + client.start(); + return client; } } |