From 46626a9895ca4bf5de05f74d84d64b3f57657fa5 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Mon, 31 Aug 2020 13:29:42 +0200 Subject: Actually close connection --- .../client/core/communication/ApacheGatewayConnection.java | 12 ++++++++++-- .../yahoo/vespa/http/client/core/communication/IOThread.java | 7 +++++-- .../core/communication/ApacheGatewayConnectionTest.java | 9 +++++---- 3 files changed, 20 insertions(+), 8 deletions(-) (limited to 'vespa-http-client/src') 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); -- cgit v1.2.3