Skip to content

Commit

Permalink
add httpclient close option to minio client builder (#1587)
Browse files Browse the repository at this point in the history
  • Loading branch information
SerhiiKryvokin authored Oct 14, 2024
1 parent ef85db9 commit fd5192f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
11 changes: 9 additions & 2 deletions api/src/main/java/io/minio/MinioAsyncClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3223,6 +3223,7 @@ public static final class Builder {
private String region;
private Provider provider;
private OkHttpClient httpClient;
private boolean closeHttpClient;

private void setAwsInfo(String host, boolean https) {
this.awsS3Prefix = null;
Expand Down Expand Up @@ -3329,6 +3330,13 @@ public Builder httpClient(OkHttpClient httpClient) {
return this;
}

public Builder httpClient(OkHttpClient httpClient, boolean close) {
HttpUtils.validateNotNull(httpClient, "http client");
this.httpClient = httpClient;
this.closeHttpClient = close;
return this;
}

public MinioAsyncClient build() {
HttpUtils.validateNotNull(this.baseUrl, "endpoint");

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

boolean closeHttpClient = false;
if (this.httpClient == null) {
this.closeHttpClient = true;
this.httpClient =
HttpUtils.newDefaultHttpClient(
DEFAULT_CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
closeHttpClient = true;
}

return new MinioAsyncClient(
Expand Down
5 changes: 5 additions & 0 deletions api/src/main/java/io/minio/MinioClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2509,6 +2509,11 @@ public Builder httpClient(OkHttpClient httpClient) {
return this;
}

public Builder httpClient(OkHttpClient httpClient, boolean close) {
asyncClientBuilder.httpClient(httpClient, close);
return this;
}

public MinioClient build() {
MinioAsyncClient asyncClient = asyncClientBuilder.build();
return new MinioClient(asyncClient);
Expand Down
20 changes: 20 additions & 0 deletions api/src/test/java/io/minio/MinioClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.HashMap;
import java.util.Map;
import javax.crypto.KeyGenerator;
import okhttp3.OkHttpClient;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okio.Buffer;
Expand Down Expand Up @@ -362,6 +363,25 @@ public void testAwsEndpoints()
"https://s3-accelerate.dualstack.amazonaws.com.cn/mybucket/myobject", url.split("\\?")[0]);
}

@Test
public void testCustomHttpClientClose() throws Exception {
OkHttpClient httpClient = new OkHttpClient().newBuilder().build();
Assert.assertFalse(httpClient.dispatcher().executorService().isShutdown());

MinioClient clientThatDoesntCloseHttpClient =
MinioClient.builder().endpoint("https://s3.amazonaws.com").httpClient(httpClient).build();
clientThatDoesntCloseHttpClient.close();
Assert.assertFalse(httpClient.dispatcher().executorService().isShutdown());

MinioClient clientThatClosesHttpClient =
MinioClient.builder()
.endpoint("https://s3.amazonaws.com")
.httpClient(httpClient, true)
.build();
clientThatClosesHttpClient.close();
Assert.assertTrue(httpClient.dispatcher().executorService().isShutdown());
}

@Test(expected = IllegalArgumentException.class)
public void testBucketName1()
throws NoSuchAlgorithmException, IOException, InvalidKeyException, MinioException {
Expand Down

0 comments on commit fd5192f

Please sign in to comment.