Skip to content

Commit fd5192f

Browse files
add httpclient close option to minio client builder (#1587)
1 parent ef85db9 commit fd5192f

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

Diff for: api/src/main/java/io/minio/MinioAsyncClient.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -3223,6 +3223,7 @@ public static final class Builder {
32233223
private String region;
32243224
private Provider provider;
32253225
private OkHttpClient httpClient;
3226+
private boolean closeHttpClient;
32263227

32273228
private void setAwsInfo(String host, boolean https) {
32283229
this.awsS3Prefix = null;
@@ -3329,6 +3330,13 @@ public Builder httpClient(OkHttpClient httpClient) {
33293330
return this;
33303331
}
33313332

3333+
public Builder httpClient(OkHttpClient httpClient, boolean close) {
3334+
HttpUtils.validateNotNull(httpClient, "http client");
3335+
this.httpClient = httpClient;
3336+
this.closeHttpClient = close;
3337+
return this;
3338+
}
3339+
33323340
public MinioAsyncClient build() {
33333341
HttpUtils.validateNotNull(this.baseUrl, "endpoint");
33343342

@@ -3340,12 +3348,11 @@ public MinioAsyncClient build() {
33403348
"Region missing in Amazon S3 China endpoint " + this.baseUrl);
33413349
}
33423350

3343-
boolean closeHttpClient = false;
33443351
if (this.httpClient == null) {
3352+
this.closeHttpClient = true;
33453353
this.httpClient =
33463354
HttpUtils.newDefaultHttpClient(
33473355
DEFAULT_CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
3348-
closeHttpClient = true;
33493356
}
33503357

33513358
return new MinioAsyncClient(

Diff for: api/src/main/java/io/minio/MinioClient.java

+5
Original file line numberDiff line numberDiff line change
@@ -2509,6 +2509,11 @@ public Builder httpClient(OkHttpClient httpClient) {
25092509
return this;
25102510
}
25112511

2512+
public Builder httpClient(OkHttpClient httpClient, boolean close) {
2513+
asyncClientBuilder.httpClient(httpClient, close);
2514+
return this;
2515+
}
2516+
25122517
public MinioClient build() {
25132518
MinioAsyncClient asyncClient = asyncClientBuilder.build();
25142519
return new MinioClient(asyncClient);

Diff for: api/src/test/java/io/minio/MinioClientTest.java

+20
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.HashMap;
2828
import java.util.Map;
2929
import javax.crypto.KeyGenerator;
30+
import okhttp3.OkHttpClient;
3031
import okhttp3.mockwebserver.MockResponse;
3132
import okhttp3.mockwebserver.MockWebServer;
3233
import okio.Buffer;
@@ -362,6 +363,25 @@ public void testAwsEndpoints()
362363
"https://s3-accelerate.dualstack.amazonaws.com.cn/mybucket/myobject", url.split("\\?")[0]);
363364
}
364365

366+
@Test
367+
public void testCustomHttpClientClose() throws Exception {
368+
OkHttpClient httpClient = new OkHttpClient().newBuilder().build();
369+
Assert.assertFalse(httpClient.dispatcher().executorService().isShutdown());
370+
371+
MinioClient clientThatDoesntCloseHttpClient =
372+
MinioClient.builder().endpoint("https://s3.amazonaws.com").httpClient(httpClient).build();
373+
clientThatDoesntCloseHttpClient.close();
374+
Assert.assertFalse(httpClient.dispatcher().executorService().isShutdown());
375+
376+
MinioClient clientThatClosesHttpClient =
377+
MinioClient.builder()
378+
.endpoint("https://s3.amazonaws.com")
379+
.httpClient(httpClient, true)
380+
.build();
381+
clientThatClosesHttpClient.close();
382+
Assert.assertTrue(httpClient.dispatcher().executorService().isShutdown());
383+
}
384+
365385
@Test(expected = IllegalArgumentException.class)
366386
public void testBucketName1()
367387
throws NoSuchAlgorithmException, IOException, InvalidKeyException, MinioException {

0 commit comments

Comments
 (0)