-
Notifications
You must be signed in to change notification settings - Fork 892
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
Add AsyncLoader to load and update value periodically #5590
base: main
Are you sure you want to change the base?
Conversation
core/src/main/java/com/linecorp/armeria/common/util/AsyncLoaderBuilder.java
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/AsyncLoaderBuilder.java
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/AsyncLoaderBuilder.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/AsyncLoader.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/DefaultAsyncLoader.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/DefaultAsyncLoader.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/DefaultAsyncLoader.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/DefaultAsyncLoader.java
Show resolved
Hide resolved
if (token == null && fallbackTokenProvider != null) { | ||
CompletableFuture<? extends GrantedOAuth2AccessToken> fallbackTokenFuture = null; | ||
try { | ||
fallbackTokenFuture = requireNonNull( | ||
fallbackTokenProvider.get(), "fallbackTokenProvider.get() returned null"); |
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.
Side note: It is not related to this PR but fallbackTokenProvider
is a name that we should only use when a token acquisition fails since it is a fallback.
I think it would be better to remove fallback
from the API method. tokenProvider
seems clearer.
526db63
to
e161fcc
Compare
core/src/main/java/com/linecorp/armeria/common/util/DefaultAsyncLoader.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/DefaultAsyncLoader.java
Outdated
Show resolved
Hide resolved
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #5590 +/- ##
============================================
+ Coverage 73.95% 74.07% +0.11%
- Complexity 20115 21252 +1137
============================================
Files 1730 1848 +118
Lines 74161 78567 +4406
Branches 9465 10024 +559
============================================
+ Hits 54847 58199 +3352
- Misses 14837 15669 +832
- Partials 4477 4699 +222 ☔ View full report in Codecov by Sentry. |
core/src/main/java/com/linecorp/armeria/common/util/DefaultAsyncLoader.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/DefaultAsyncLoader.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/DefaultAsyncLoader.java
Outdated
Show resolved
Hide resolved
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.
Overall looks good.
core/src/main/java/com/linecorp/armeria/common/util/DefaultAsyncLoader.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/DefaultAsyncLoader.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/DefaultAsyncLoader.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/DefaultAsyncLoader.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/DefaultAsyncLoader.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/DefaultAsyncLoader.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/DefaultAsyncLoader.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/DefaultAsyncLoader.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/AsyncLoader.java
Outdated
Show resolved
Hide resolved
...2/src/main/java/com/linecorp/armeria/client/auth/oauth2/DefaultOAuth2AuthorizationGrant.java
Outdated
Show resolved
Hide resolved
private static final RefreshingFuture<?> COMPLETED; | ||
|
||
static { | ||
COMPLETED = new RefreshingFuture<>(null); | ||
COMPLETED.complete(null); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
static <T> RefreshingFuture<T> completedFuture() { | ||
return (RefreshingFuture<T>) COMPLETED; | ||
} |
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.
|
||
final List<AggregatedHttpResponse> responses3 = | ||
getConcurrently(client, "/resource-read-write-update/", count).join(); | ||
validateResponses(responses3, HttpStatus.FORBIDDEN); | ||
verify(grant, times(1)).obtainAccessToken(any()); | ||
verify(grant, times(0)).refreshAccessToken(any(), any()); | ||
assertThat(newTokenCounter.get()).isOne(); |
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.
DefaultOAuth2AuthorizationGrant(..) {
tokenLoader = tokenLoader();
}
FAILED: wanted but not invoked obtainAccessToken, only invoked getAccessToken();
We make tokenLoader in constructor, then mockito can't verify obtainAccessToken, refreshAccessToken
invocation well.
So I replace it with newTokenCounter :)
import com.linecorp.armeria.common.annotation.Nullable; | ||
|
||
/** | ||
* An {@link AsyncLoader} to atomically load, cache and update value. |
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.
I think it doesn't make much sense to refer to itself when defining something, especially in the first sentence of the definition. Can we rephrase this?
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.
✅ updated!
if (!needsRefresh(cacheEntry) || loadFuture.refreshing) { | ||
return loadFuture; | ||
} |
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.
Maybe log here about why and why not refreshing?
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.
✅ updated! add logger.debug(..)
} | ||
} | ||
|
||
private boolean isValid(@Nullable CacheEntry<T> cacheEntry) { |
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.
Maybe add more logging in this method to make it easy to figure why isValid() returned false?
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.
✅ updated! add logger.debug(..)
Fixes #5506.
Motivation:
AsyncLoader
can be useful in the following situations.We already have an implementation for that on AbstractOAuth2AuthorizationGrant.java.
However, I hope to generalize it and add new features to use it in various cases.
Modifications:
AsyncLoader
to load and update value periodicallyResult:
AsyncLoader