Skip to content

Commit 4c33cfd

Browse files
authored
Close HTTP client owned by MinioClient (minio#1546)
1 parent 40b054f commit 4c33cfd

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

api/src/main/java/io/minio/MinioAsyncClient.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ private MinioAsyncClient(
139139
boolean useVirtualStyle,
140140
String region,
141141
Provider provider,
142-
OkHttpClient httpClient) {
142+
OkHttpClient httpClient,
143+
boolean closeHttpClient) {
143144
super(
144145
baseUrl,
145146
awsS3Prefix,
@@ -148,7 +149,8 @@ private MinioAsyncClient(
148149
useVirtualStyle,
149150
region,
150151
provider,
151-
httpClient);
152+
httpClient,
153+
closeHttpClient);
152154
}
153155

154156
protected MinioAsyncClient(MinioAsyncClient client) {
@@ -3338,10 +3340,12 @@ public MinioAsyncClient build() {
33383340
"Region missing in Amazon S3 China endpoint " + this.baseUrl);
33393341
}
33403342

3343+
boolean closeHttpClient = false;
33413344
if (this.httpClient == null) {
33423345
this.httpClient =
33433346
HttpUtils.newDefaultHttpClient(
33443347
DEFAULT_CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
3348+
closeHttpClient = true;
33453349
}
33463350

33473351
return new MinioAsyncClient(
@@ -3352,7 +3356,8 @@ public MinioAsyncClient build() {
33523356
useVirtualStyle,
33533357
region,
33543358
provider,
3355-
httpClient);
3359+
httpClient,
3360+
closeHttpClient);
33563361
}
33573362
}
33583363
}

api/src/main/java/io/minio/MinioClient.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
* .build();
9696
* }</pre>
9797
*/
98-
public class MinioClient {
98+
public class MinioClient implements AutoCloseable {
9999
private MinioAsyncClient asyncClient = null;
100100

101101
private MinioClient(MinioAsyncClient asyncClient) {
@@ -2450,6 +2450,13 @@ public void setAwsS3Prefix(String awsS3Prefix) {
24502450
asyncClient.setAwsS3Prefix(awsS3Prefix);
24512451
}
24522452

2453+
@Override
2454+
public void close() throws Exception {
2455+
if (asyncClient != null) {
2456+
asyncClient.close();
2457+
}
2458+
}
2459+
24532460
public static Builder builder() {
24542461
return new Builder();
24552462
}

api/src/main/java/io/minio/S3Base.java

+37-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
import okhttp3.ResponseBody;
100100

101101
/** Core S3 API client. */
102-
public abstract class S3Base {
102+
public abstract class S3Base implements AutoCloseable {
103103
static {
104104
try {
105105
RequestBody.create(new byte[] {}, null);
@@ -136,7 +136,10 @@ public abstract class S3Base {
136136
protected String region;
137137
protected Provider provider;
138138
protected OkHttpClient httpClient;
139+
protected boolean closeHttpClient;
139140

141+
/** @deprecated This method is no longer supported. */
142+
@Deprecated
140143
protected S3Base(
141144
HttpUrl baseUrl,
142145
String awsS3Prefix,
@@ -146,6 +149,28 @@ protected S3Base(
146149
String region,
147150
Provider provider,
148151
OkHttpClient httpClient) {
152+
this(
153+
baseUrl,
154+
awsS3Prefix,
155+
awsDomainSuffix,
156+
awsDualstack,
157+
useVirtualStyle,
158+
region,
159+
provider,
160+
httpClient,
161+
false);
162+
}
163+
164+
protected S3Base(
165+
HttpUrl baseUrl,
166+
String awsS3Prefix,
167+
String awsDomainSuffix,
168+
boolean awsDualstack,
169+
boolean useVirtualStyle,
170+
String region,
171+
Provider provider,
172+
OkHttpClient httpClient,
173+
boolean closeHttpClient) {
149174
this.baseUrl = baseUrl;
150175
this.awsS3Prefix = awsS3Prefix;
151176
this.awsDomainSuffix = awsDomainSuffix;
@@ -154,6 +179,7 @@ protected S3Base(
154179
this.region = region;
155180
this.provider = provider;
156181
this.httpClient = httpClient;
182+
this.closeHttpClient = closeHttpClient;
157183
}
158184

159185
/** @deprecated This method is no longer supported. */
@@ -182,6 +208,7 @@ protected S3Base(
182208
this.region = region;
183209
this.provider = provider;
184210
this.httpClient = httpClient;
211+
this.closeHttpClient = false;
185212
}
186213

187214
protected S3Base(S3Base client) {
@@ -193,6 +220,7 @@ protected S3Base(S3Base client) {
193220
this.region = client.region;
194221
this.provider = client.provider;
195222
this.httpClient = client.httpClient;
223+
this.closeHttpClient = client.closeHttpClient;
196224
}
197225

198226
/** Check whether argument is valid or not. */
@@ -3757,4 +3785,12 @@ protected CompletableFuture<UploadPartCopyResponse> uploadPartCopyAsync(
37573785
}
37583786
});
37593787
}
3788+
3789+
@Override
3790+
public void close() throws Exception {
3791+
if (closeHttpClient) {
3792+
httpClient.dispatcher().executorService().shutdown();
3793+
httpClient.connectionPool().evictAll();
3794+
}
3795+
}
37603796
}

functional/TestUserAgent.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
public class TestUserAgent {
2525
public static void main(String[] args) throws Exception {
26-
MinioClient client = MinioClient.builder().endpoint("http://example.org").build();
26+
MinioClient client = MinioClient.builder().endpoint("http://httpbin.org").build();
2727
ByteArrayOutputStream baos = new ByteArrayOutputStream();
2828
client.traceOn(baos);
2929
client.bucketExists(BucketExistsArgs.builder().bucket("any-bucket-name-works").build());

0 commit comments

Comments
 (0)