aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-http-client
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@gmail.com>2020-08-31 13:29:42 +0200
committerJon Bratseth <bratseth@gmail.com>2020-08-31 13:29:42 +0200
commit46626a9895ca4bf5de05f74d84d64b3f57657fa5 (patch)
treebc28996220530f92b43e8bdf8eea79e5ea81dab1 /vespa-http-client
parent136b145ab697400b6fa100bdedb5d78f15e2b2ac (diff)
Actually close connection
Diffstat (limited to 'vespa-http-client')
-rw-r--r--vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnection.java12
-rw-r--r--vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/communication/IOThread.java7
-rw-r--r--vespa-http-client/src/test/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnectionTest.java9
3 files changed, 20 insertions, 8 deletions
diff --git a/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnection.java b/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnection.java
index b51c0dd30a1..a46b2e67fe1 100644
--- a/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnection.java
+++ b/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnection.java
@@ -20,6 +20,7 @@ import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
@@ -64,7 +65,7 @@ class ApacheGatewayConnection implements GatewayConnection {
private final FeedParams feedParams;
private final String clusterSpecificRoute;
private final ConnectionParams connectionParams;
- private HttpClient httpClient;
+ private CloseableHttpClient httpClient;
private Instant connectionTime = null;
private Instant lastPollTime = null;
private String sessionId;
@@ -378,6 +379,13 @@ class ApacheGatewayConnection implements GatewayConnection {
@Override
public void close() {
+ try {
+ if (httpClient != null)
+ httpClient.close();
+ }
+ catch (IOException e) {
+ log.log(Level.WARNING, "Failed closing HTTP client", e);
+ }
httpClient = null;
}
@@ -394,7 +402,7 @@ class ApacheGatewayConnection implements GatewayConnection {
this.useSsl = useSsl;
}
- public HttpClient createClient() {
+ public CloseableHttpClient createClient() {
HttpClientBuilder clientBuilder;
if (connectionParams.useTlsConfigFromEnvironment()) {
clientBuilder = VespaHttpClientBuilder.create();
diff --git a/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/communication/IOThread.java b/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/communication/IOThread.java
index c7c94587640..2417208fba3 100644
--- a/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/communication/IOThread.java
+++ b/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/communication/IOThread.java
@@ -153,6 +153,8 @@ class IOThread implements Runnable, AutoCloseable {
if (size > 0) {
log.info("We have outstanding operations (" + size + ") , trying to fetch responses.");
try {
+ for (GatewayConnection oldConnection : oldConnections)
+ processResponse(oldConnection.drain());
processResponse(currentConnection.drain());
} catch (Throwable e) {
log.log(Level.SEVERE, "Some failures while trying to get latest responses from vespa.", e);
@@ -160,11 +162,12 @@ class IOThread implements Runnable, AutoCloseable {
}
try {
+ for (GatewayConnection oldConnection : oldConnections)
+ oldConnection.close();
currentConnection.close();
} finally {
// If there is still documents in the queue, fail them.
- drainDocumentQueueWhenFailingPermanently(new Exception(
- "Closed call, did not manage to process everything so failing this document."));
+ drainDocumentQueueWhenFailingPermanently(new Exception("Closed call, did not manage to process everything so failing this document."));
}
log.fine("Session to " + endpoint + " closed.");
diff --git a/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnectionTest.java b/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnectionTest.java
index 2ee1a146dfa..511e40c1c88 100644
--- a/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnectionTest.java
+++ b/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnectionTest.java
@@ -14,9 +14,10 @@ import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.StatusLine;
-import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicHeader;
import org.junit.Rule;
import org.junit.Test;
@@ -278,7 +279,7 @@ public class ApacheGatewayConnectionTest {
private static ApacheGatewayConnection.HttpClientFactory mockHttpClientFactory(HttpExecuteMock httpExecuteMock) throws IOException {
ApacheGatewayConnection.HttpClientFactory mockFactory =
mock(ApacheGatewayConnection.HttpClientFactory.class);
- HttpClient httpClientMock = mock(HttpClient.class);
+ CloseableHttpClient httpClientMock = mock(CloseableHttpClient.class);
when(mockFactory.createClient()).thenReturn(httpClientMock);
when(httpClientMock.execute(any())).thenAnswer((Answer) invocation -> {
Object[] args = invocation.getArguments();
@@ -315,7 +316,7 @@ public class ApacheGatewayConnectionTest {
}
private HttpResponse httpResponse(String sessionIdInResult, String version) throws IOException {
- HttpResponse httpResponseMock = mock(HttpResponse.class);
+ CloseableHttpResponse httpResponseMock = mock(CloseableHttpResponse.class);
StatusLine statusLineMock = mock(StatusLine.class);
when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
@@ -336,7 +337,7 @@ public class ApacheGatewayConnectionTest {
}
private static HttpResponse createErrorHttpResponse(int statusCode, String reasonPhrase, String message) throws IOException {
- HttpResponse response = mock(HttpResponse.class);
+ CloseableHttpResponse response = mock(CloseableHttpResponse.class);
StatusLine statusLine = mock(StatusLine.class);
when(statusLine.getStatusCode()).thenReturn(statusCode);