aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-http-client
diff options
context:
space:
mode:
authorBjørn Christian Seime <bjorncs@verizonmedia.com>2020-09-29 17:46:21 +0200
committerBjørn Christian Seime <bjorncs@verizonmedia.com>2020-09-29 17:48:42 +0200
commit3733bebace9418a0bac3f74d5a3a82b16ec7922e (patch)
tree57446c6cc91546674636e224d6caf2510e2ea0fb /vespa-http-client
parentcae8ba3bfc5573828a6e4a28d2616694b09cb616 (diff)
Ensure content is consumed to allow connection reuse
Diffstat (limited to 'vespa-http-client')
-rw-r--r--vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnection.java20
1 files changed, 9 insertions, 11 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 bd5cf761024..735ca0beb40 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
@@ -23,6 +23,7 @@ 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;
+import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import java.io.ByteArrayInputStream;
@@ -246,24 +247,21 @@ class ApacheGatewayConnection implements GatewayConnection {
}
private InputStream executePost(HttpPost httpPost) throws ServerResponseException, IOException {
- HttpResponse response;
- try {
- if (httpClient == null)
- throw new IOException("Trying to executePost while not having a connection/http client");
- response = httpClient.execute(httpPost);
- } catch (Exception e) {
- httpPost.abort();
- throw e;
- }
+ if (httpClient == null)
+ throw new IOException("Trying to executePost while not having a connection/http client");
+ HttpResponse response = httpClient.execute(httpPost);
try {
verifyServerResponseCode(response);
verifyServerVersion(response.getFirstHeader(Headers.VERSION));
verifySessionHeader(response.getFirstHeader(Headers.SESSION_ID));
} catch (ServerResponseException e) {
- httpPost.abort();
+ // Ensure response is consumed to allow connection reuse later on
+ EntityUtils.consumeQuietly(response.getEntity());
throw e;
}
- return response.getEntity().getContent();
+ // Consume response now to allow connection to be reused immediately
+ byte[] responseData = EntityUtils.toByteArray(response.getEntity());
+ return responseData == null ? null : new ByteArrayInputStream(responseData);
}
private void verifyServerResponseCode(HttpResponse response) throws ServerResponseException {