Skip to content

Commit

Permalink
fix: properly close S3 client (#1531)
Browse files Browse the repository at this point in the history
  • Loading branch information
yitingb authored Oct 2, 2023
1 parent 17494e9 commit be9a3d7
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/main/java/com/aws/greengrass/util/S3SdkClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public S3Client getS3Client() throws DeviceConfigurationException {
if (configValidationError != null) {
throw configValidationError;
}
setS3EndpointType(Coerce.toString(deviceConfiguration.gets3EndpointType()));
return getClientForRegion(region);
}

Expand All @@ -90,6 +89,7 @@ public S3Client getS3Client() throws DeviceConfigurationException {
* @return s3client
*/
public S3Client getClientForRegion(Region r) {
setS3EndpointType(Coerce.toString(deviceConfiguration.gets3EndpointType()));
return clientCache.computeIfAbsent(r, (region) -> S3Client.builder()
.httpClientBuilder(ProxyUtils.getSdkHttpClientBuilder())
.serviceConfiguration(S3Configuration.builder().useArnRegionEnabled(true).build())
Expand Down Expand Up @@ -118,7 +118,15 @@ private void setS3EndpointType(String type) {
}
}

/**
* Remove the cached client and close it.
*
*/
@SuppressWarnings({"PMD.CloseResource"})
private void refreshClientCache() {
clientCache.remove(region);
S3Client clientToRemove = clientCache.remove(region);
if (clientToRemove != null) {
clientToRemove.close();
}
}
}

0 comments on commit be9a3d7

Please sign in to comment.