Skip to content

Commit 115bd12

Browse files
committed
Refactor httpResponse handling with closeQuietly to ensure consistent resource cleanup.
1 parent 190983e commit 115bd12

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

client-v2/src/main/java/com/clickhouse/client/api/Client.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,13 +1616,7 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
16161616
return new QueryResponse(httpResponse, responseFormat, finalSettings, metrics);
16171617

16181618
} catch (Exception e) {
1619-
if (httpResponse != null) {
1620-
try {
1621-
httpResponse.close();
1622-
} catch (IOException ex) {
1623-
throw new ClientException("Failed to close response", e);
1624-
}
1625-
}
1619+
httpClientHelper.closeQuietly(httpResponse);
16261620
lastException = httpClientHelper.wrapException(String.format("Query request failed (Attempt: %s/%s - Duration: %s)",
16271621
(i + 1), (retries + 1), System.nanoTime() - startTime), e);
16281622
if (httpClientHelper.shouldRetry(e, finalSettings.getAllSettings())) {

client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,9 @@ public ClassicHttpResponse executeRequest(Endpoint server, Map<String, Object> r
428428

429429
HttpClientContext context = HttpClientContext.create();
430430

431+
ClassicHttpResponse httpResponse = null;
431432
try {
432-
ClassicHttpResponse httpResponse = httpClient.executeOpen(null, req, context);
433+
httpResponse = httpClient.executeOpen(null, req, context);
433434
boolean serverCompression = ClientConfigProperties.COMPRESS_SERVER_RESPONSE.getOrDefault(requestConfig);
434435
httpResponse.setEntity(wrapResponseEntity(httpResponse.getEntity(), httpResponse.getCode(), serverCompression, useHttpCompression, lz4Factory, requestConfig));
435436

@@ -448,14 +449,26 @@ public ClassicHttpResponse executeRequest(Endpoint server, Map<String, Object> r
448449
return httpResponse;
449450

450451
} catch (UnknownHostException e) {
452+
closeQuietly(httpResponse);
451453
LOG.warn("Host '{}' unknown", server.getBaseURL());
452454
throw e;
453455
} catch (ConnectException | NoRouteToHostException e) {
456+
closeQuietly(httpResponse);
454457
LOG.warn("Failed to connect to '{}': {}", server.getBaseURL(), e.getMessage());
455458
throw e;
456459
}
457460
}
458461

462+
public void closeQuietly(ClassicHttpResponse httpResponse) {
463+
if (httpResponse != null) {
464+
try {
465+
httpResponse.close();
466+
} catch (IOException e) {
467+
LOG.warn("Failed to close response");
468+
}
469+
}
470+
}
471+
459472
private static final ContentType CONTENT_TYPE = ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), "UTF-8");
460473

461474
private void addHeaders(HttpPost req, Map<String, Object> requestConfig) {

0 commit comments

Comments
 (0)