Skip to content

Commit

Permalink
Close HTTP client owned by MinioClient
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
findepi committed Apr 12, 2024
1 parent 40b054f commit 88bf15c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
11 changes: 8 additions & 3 deletions api/src/main/java/io/minio/MinioAsyncClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ private MinioAsyncClient(
boolean useVirtualStyle,
String region,
Provider provider,
OkHttpClient httpClient) {
OkHttpClient httpClient,
boolean closeHttpClient) {
super(
baseUrl,
awsS3Prefix,
Expand All @@ -148,7 +149,8 @@ private MinioAsyncClient(
useVirtualStyle,
region,
provider,
httpClient);
httpClient,
closeHttpClient);
}

protected MinioAsyncClient(MinioAsyncClient client) {
Expand Down Expand Up @@ -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(
Expand All @@ -3352,7 +3356,8 @@ public MinioAsyncClient build() {
useVirtualStyle,
region,
provider,
httpClient);
httpClient,
closeHttpClient);
}
}
}
9 changes: 8 additions & 1 deletion api/src/main/java/io/minio/MinioClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
* .build();
* }</pre>
*/
public class MinioClient {
public class MinioClient implements AutoCloseable {
private MinioAsyncClient asyncClient = null;

private MinioClient(MinioAsyncClient asyncClient) {
Expand Down Expand Up @@ -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();
}
Expand Down
40 changes: 38 additions & 2 deletions api/src/main/java/io/minio/S3Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -136,6 +136,7 @@ public abstract class S3Base {
protected String region;
protected Provider provider;
protected OkHttpClient httpClient;
protected boolean closeHttpClient;

protected S3Base(
HttpUrl baseUrl,
Expand All @@ -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;
Expand All @@ -154,6 +156,30 @@ protected S3Base(
this.region = region;
this.provider = provider;
this.httpClient = httpClient;
this.closeHttpClient = closeHttpClient;
}

/** @deprecated This method is no longer supported. */
@Deprecated
protected S3Base(
HttpUrl baseUrl,
String awsS3Prefix,
String awsDomainSuffix,
boolean awsDualstack,
boolean useVirtualStyle,
String region,
Provider provider,
OkHttpClient httpClient) {
this(
baseUrl,
awsS3Prefix,
awsDomainSuffix,
awsDualstack,
useVirtualStyle,
region,
provider,
httpClient,
false);
}

/** @deprecated This method is no longer supported. */
Expand Down Expand Up @@ -182,6 +208,7 @@ protected S3Base(
this.region = region;
this.provider = provider;
this.httpClient = httpClient;
this.closeHttpClient = false;
}

protected S3Base(S3Base client) {
Expand All @@ -193,6 +220,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. */
Expand Down Expand Up @@ -3757,4 +3785,12 @@ protected CompletableFuture<UploadPartCopyResponse> uploadPartCopyAsync(
}
});
}

@Override
public void close() throws Exception {
if (closeHttpClient) {
httpClient.dispatcher().executorService().shutdown();
httpClient.connectionPool().evictAll();
}
}
}

0 comments on commit 88bf15c

Please sign in to comment.