-
Notifications
You must be signed in to change notification settings - Fork 59
feat: recreate sdk client with customized exception #1709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
yitingb
wants to merge
1
commit into
main
Choose a base branch
from
retry-exception
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/java/com/aws/greengrass/util/SdkClientWrapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package com.aws.greengrass.util; | ||
|
|
||
| import com.aws.greengrass.logging.api.Logger; | ||
| import com.aws.greengrass.logging.impl.LogManager; | ||
| import org.apache.http.NoHttpResponseException; | ||
| import software.amazon.awssdk.core.SdkClient; | ||
| import software.amazon.awssdk.core.exception.SdkClientException; | ||
|
|
||
| import java.net.SocketException; | ||
| import java.util.concurrent.locks.Lock; | ||
| import java.util.function.Function; | ||
| import java.util.function.Supplier; | ||
|
|
||
| public final class SdkClientWrapper<T extends SdkClient> { | ||
| private static final Logger logger = LogManager.getLogger(SdkClientWrapper.class); | ||
|
|
||
| private volatile T client; | ||
| private final Supplier<T> clientFactory; | ||
| private final Lock lock = LockFactory.newReentrantLock(this); | ||
|
|
||
| public SdkClientWrapper(Supplier<T> clientFactory) { | ||
| this.clientFactory = clientFactory; | ||
| this.client = clientFactory.get(); | ||
| } | ||
|
|
||
| /** | ||
| * Executes the given operation on the client, handling potential SDK client exceptions. | ||
| * | ||
| * <p>This method applies the provided operation to the client. If an {@link SdkClientException} | ||
| * occurs and the client needs refreshing (as determined by {@link #shouldRefreshClient(SdkClientException)}), | ||
| * it will attempt to refresh the client and retry the operation once.</p> | ||
| * | ||
| * @param <R> The return type of the operation | ||
| * @param operation A function that takes the client of type T and returns a result of type R | ||
| * @return The result of the operation | ||
| * @throws SdkClientException If the operation fails and the client cannot be refreshed or fails after refresh | ||
| * @throws RuntimeException If an unexpected error occurs during execution | ||
| */ | ||
| public <R> R execute(final Function<T, R> operation) { | ||
| try { | ||
| return operation.apply(client); | ||
| } catch (SdkClientException e) { | ||
| if (shouldRefreshClient(e)) { | ||
| logger.atDebug().log("Client needs refresh due to: {}", e.getMessage()); | ||
| try { | ||
| refreshClient(); | ||
| return operation.apply(client); | ||
| } catch (SdkClientException retryException) { | ||
| logger.atError().log("Failed to execute operation after client refresh", retryException); | ||
| throw retryException; | ||
| } | ||
| } | ||
| logger.atError().log("SDK client operation failed", e); | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| private void refreshClient() { | ||
| try (LockScope ls = LockScope.lock(lock)) { | ||
| if (client != null) { | ||
| try { | ||
| client.close(); | ||
| } catch (SdkClientException e) { | ||
| logger.atError().log("Error closing client: " + e.getMessage()); | ||
| } | ||
| } | ||
| // Creates new client when refresh needed | ||
| client = clientFactory.get(); | ||
| } | ||
| } | ||
|
|
||
| private boolean shouldRefreshClient(SdkClientException e) { | ||
| Throwable cause = e; | ||
| while (cause != null) { | ||
| if (cause instanceof SocketException && "Connection reset".equals(cause.getMessage())) { | ||
| return true; | ||
| } | ||
| if (cause instanceof NoHttpResponseException) { | ||
| return true; | ||
| } | ||
| // Add other conditions that should trigger a client refresh here | ||
| cause = cause.getCause(); | ||
| } | ||
| return false; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recommendation generated by Amazon CodeGuru Reviewer. Leave feedback on this recommendation by replying to the comment or by reacting to the comment using emoji.
software.amazon.awssdk.services.sts.StsClient.getCallerIdentityAPI can also throw the following exception types:SdkClientException,StsException,SdkException,AwsServiceException,UnsupportedOperationException. We recommend handling these uncaught exceptions as well.