From 785e2863f407020a13743d05b0fb96d591bcccdc Mon Sep 17 00:00:00 2001 From: Piotr Findeisen Date: Thu, 11 Apr 2024 22:04:28 +0200 Subject: [PATCH] Close HTTP client owned by MinioClient MinioClient builder and MinioAsyncClient builder allocate a new OkHttpClient instance. OkHttpClient manages internal resources such as dispatcher thread pool for executing HTTP requests and should be closed to dispose of these resources. --- .../main/java/io/minio/MinioAsyncClient.java | 11 ++++++++--- api/src/main/java/io/minio/MinioClient.java | 9 ++++++++- api/src/main/java/io/minio/S3Base.java | 17 +++++++++++++++-- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/api/src/main/java/io/minio/MinioAsyncClient.java b/api/src/main/java/io/minio/MinioAsyncClient.java index 405ecd589..a19cde085 100644 --- a/api/src/main/java/io/minio/MinioAsyncClient.java +++ b/api/src/main/java/io/minio/MinioAsyncClient.java @@ -139,7 +139,8 @@ private MinioAsyncClient( boolean useVirtualStyle, String region, Provider provider, - OkHttpClient httpClient) { + OkHttpClient httpClient, + boolean closeHttpClient) { super( baseUrl, awsS3Prefix, @@ -148,7 +149,8 @@ private MinioAsyncClient( useVirtualStyle, region, provider, - httpClient); + httpClient, + closeHttpClient); } protected MinioAsyncClient(MinioAsyncClient client) { @@ -3338,10 +3340,12 @@ public MinioAsyncClient build() { "Region missing in Amazon S3 China endpoint " + this.baseUrl); } + boolean closeHttpClient = false; if (this.httpClient == null) { this.httpClient = HttpUtils.newDefaultHttpClient( DEFAULT_CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); + closeHttpClient = true; } return new MinioAsyncClient( @@ -3352,7 +3356,8 @@ public MinioAsyncClient build() { useVirtualStyle, region, provider, - httpClient); + httpClient, + closeHttpClient); } } } diff --git a/api/src/main/java/io/minio/MinioClient.java b/api/src/main/java/io/minio/MinioClient.java index 0015d1728..5795c1180 100644 --- a/api/src/main/java/io/minio/MinioClient.java +++ b/api/src/main/java/io/minio/MinioClient.java @@ -95,7 +95,7 @@ * .build(); * } */ -public class MinioClient { +public class MinioClient implements AutoCloseable { private MinioAsyncClient asyncClient = null; private MinioClient(MinioAsyncClient asyncClient) { @@ -2450,6 +2450,13 @@ public void setAwsS3Prefix(String awsS3Prefix) { asyncClient.setAwsS3Prefix(awsS3Prefix); } + @Override + public void close() throws Exception { + if (asyncClient != null) { + asyncClient.close(); + } + } + public static Builder builder() { return new Builder(); } diff --git a/api/src/main/java/io/minio/S3Base.java b/api/src/main/java/io/minio/S3Base.java index f9aca6086..1df9dd263 100644 --- a/api/src/main/java/io/minio/S3Base.java +++ b/api/src/main/java/io/minio/S3Base.java @@ -99,7 +99,7 @@ import okhttp3.ResponseBody; /** Core S3 API client. */ -public abstract class S3Base { +public abstract class S3Base implements AutoCloseable { static { try { RequestBody.create(new byte[] {}, null); @@ -136,6 +136,7 @@ public abstract class S3Base { protected String region; protected Provider provider; protected OkHttpClient httpClient; + protected boolean closeHttpClient; protected S3Base( HttpUrl baseUrl, @@ -145,7 +146,8 @@ protected S3Base( boolean useVirtualStyle, String region, Provider provider, - OkHttpClient httpClient) { + OkHttpClient httpClient, + boolean closeHttpClient) { this.baseUrl = baseUrl; this.awsS3Prefix = awsS3Prefix; this.awsDomainSuffix = awsDomainSuffix; @@ -154,6 +156,7 @@ protected S3Base( this.region = region; this.provider = provider; this.httpClient = httpClient; + this.closeHttpClient = closeHttpClient; } /** @deprecated This method is no longer supported. */ @@ -182,6 +185,7 @@ protected S3Base( this.region = region; this.provider = provider; this.httpClient = httpClient; + this.closeHttpClient = false; } protected S3Base(S3Base client) { @@ -193,6 +197,7 @@ protected S3Base(S3Base client) { this.region = client.region; this.provider = client.provider; this.httpClient = client.httpClient; + this.closeHttpClient = false; } /** Check whether argument is valid or not. */ @@ -3757,4 +3762,12 @@ protected CompletableFuture uploadPartCopyAsync( } }); } + + @Override + public void close() throws Exception { + if (closeHttpClient) { + httpClient.dispatcher().executorService().shutdown(); + httpClient.connectionPool().evictAll(); + } + } }