-
Notifications
You must be signed in to change notification settings - Fork 909
Make default region and credential loading lazy. #1068
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"category": "AWS SDK for Java v2", | ||
"type": "bugfix", | ||
"description": "Defer all errors raised when creating `ProfileCredentialsProvider` to the `resolveCredentials()` call." | ||
} |
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,5 @@ | ||
{ | ||
"category": "AWS SDK for Java v2", | ||
"type": "feature", | ||
"description": "Never initialize the default credentials provider chain if credentials are always specified in the client builder." | ||
} |
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,5 @@ | ||
{ | ||
"category": "AWS SDK for Java v2", | ||
"type": "feature", | ||
"description": "Never initialie the default region provider chain if the region is always specified in the client builder." | ||
} |
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
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
67 changes: 67 additions & 0 deletions
67
...ain/java/software/amazon/awssdk/auth/credentials/internal/LazyAwsCredentialsProvider.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,67 @@ | ||
/* | ||
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.auth.credentials.internal; | ||
|
||
import java.util.function.Supplier; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.auth.credentials.AwsCredentials; | ||
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; | ||
import software.amazon.awssdk.utils.IoUtils; | ||
import software.amazon.awssdk.utils.SdkAutoCloseable; | ||
import software.amazon.awssdk.utils.ToString; | ||
|
||
/** | ||
* A wrapper for {@link AwsCredentialsProvider} that defers creation of the underlying provider until the first time the | ||
* {@link AwsCredentialsProvider#resolveCredentials()} method is invoked. | ||
*/ | ||
@SdkInternalApi | ||
public class LazyAwsCredentialsProvider implements AwsCredentialsProvider, SdkAutoCloseable { | ||
private final Supplier<AwsCredentialsProvider> delegateConstructor; | ||
private volatile AwsCredentialsProvider delegate; | ||
|
||
private LazyAwsCredentialsProvider(Supplier<AwsCredentialsProvider> delegateConstructor) { | ||
this.delegateConstructor = delegateConstructor; | ||
} | ||
|
||
public static LazyAwsCredentialsProvider create(Supplier<AwsCredentialsProvider> delegateConstructor) { | ||
return new LazyAwsCredentialsProvider(delegateConstructor); | ||
} | ||
|
||
@Override | ||
public AwsCredentials resolveCredentials() { | ||
if (delegate == null) { | ||
synchronized (this) { | ||
if (delegate == null) { | ||
delegate = delegateConstructor.get(); | ||
} | ||
} | ||
} | ||
return delegate.resolveCredentials(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
IoUtils.closeIfCloseable(delegate, null); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return ToString.builder("LazyAwsCredentialsProvider") | ||
.add("delegateConstructor", delegateConstructor) | ||
.add("delegate", delegate) | ||
.build(); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
...java/software/amazon/awssdk/auth/credentials/internal/LazyAwsCredentialsProviderTest.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,51 @@ | ||
/* | ||
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.auth.credentials.internal; | ||
|
||
import java.util.function.Supplier; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; | ||
|
||
public class LazyAwsCredentialsProviderTest { | ||
@SuppressWarnings("unchecked") | ||
private Supplier<AwsCredentialsProvider> credentialsConstructor = Mockito.mock(Supplier.class); | ||
|
||
private AwsCredentialsProvider credentials = Mockito.mock(AwsCredentialsProvider.class); | ||
|
||
@Before | ||
public void reset() { | ||
Mockito.reset(credentials, credentialsConstructor); | ||
Mockito.when(credentialsConstructor.get()).thenReturn(credentials); | ||
} | ||
|
||
@Test | ||
public void creationDoesntInvokeSupplier() { | ||
LazyAwsCredentialsProvider.create(credentialsConstructor); | ||
Mockito.verifyZeroInteractions(credentialsConstructor); | ||
} | ||
|
||
@Test | ||
public void resolveCredentialsInvokesSupplierExactlyOnce() { | ||
LazyAwsCredentialsProvider credentialsProvider = LazyAwsCredentialsProvider.create(credentialsConstructor); | ||
credentialsProvider.resolveCredentials(); | ||
credentialsProvider.resolveCredentials(); | ||
|
||
Mockito.verify(credentialsConstructor, Mockito.times(1)).get(); | ||
Mockito.verify(credentials, Mockito.times(2)).resolveCredentials(); | ||
} | ||
} |
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
55 changes: 55 additions & 0 deletions
55
...regions/src/main/java/software/amazon/awssdk/regions/providers/LazyAwsRegionProvider.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,55 @@ | ||
/* | ||
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.regions.providers; | ||
|
||
import java.util.function.Supplier; | ||
import software.amazon.awssdk.annotations.SdkProtectedApi; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.utils.ToString; | ||
|
||
/** | ||
* A wrapper for {@link AwsRegionProvider} that defers creation of the underlying provider until the first time the | ||
* {@link AwsRegionProvider#getRegion()} method is invoked. | ||
*/ | ||
@SdkProtectedApi | ||
public class LazyAwsRegionProvider implements AwsRegionProvider { | ||
private final Supplier<AwsRegionProvider> delegateConstructor; | ||
private volatile AwsRegionProvider delegate; | ||
|
||
public LazyAwsRegionProvider(Supplier<AwsRegionProvider> delegateConstructor) { | ||
this.delegateConstructor = delegateConstructor; | ||
} | ||
|
||
@Override | ||
public Region getRegion() { | ||
if (delegate == null) { | ||
synchronized (this) { | ||
if (delegate == null) { | ||
delegate = delegateConstructor.get(); | ||
} | ||
} | ||
} | ||
return delegate.getRegion(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return ToString.builder("LazyAwsRegionProvider") | ||
.add("delegateConstructor", delegateConstructor) | ||
.add("delegate", delegate) | ||
.build(); | ||
} | ||
} |
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.
resolveRegion()? just "region()"?
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.
It's a protected method.