-
Notifications
You must be signed in to change notification settings - Fork 895
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(..)
* <p>Note that if 1 refresh is in progress, other refreshes will be bypassed. | ||
* Only 1 refresh is executed at the same time. |
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.
What do you think of removing this?
It's an implementation detail that users may not need to know about it.
* <p>Note that if 1 refresh is in progress, other refreshes will be bypassed. | |
* Only 1 refresh is executed at the same time. |
import com.linecorp.armeria.common.annotation.Nullable; | ||
|
||
/** | ||
* A loader which atomically loads, caches and updates 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.
Should we write an example code?
if (needsRefresh) { | ||
logger.debug("Refreshing already in progress, " + | ||
"so not refresh it again. cacheEntry {}", cacheEntry); | ||
} | ||
return loadFuture; | ||
} | ||
logger.debug("Refresh cacheEntry {}", 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.
It would be enough to log when refreshing a new value.
if (needsRefresh) { | |
logger.debug("Refreshing already in progress, " + | |
"so not refresh it again. cacheEntry {}", cacheEntry); | |
} | |
return loadFuture; | |
} | |
logger.debug("Refresh cacheEntry {}", cacheEntry); | |
return loadFuture; | |
} | |
logger.debug("Pre-fetching a new value. loader: {}, cache: {}", loader, cacheEntry.value); |
logger.debug("CacheEntry {} is expired due to expireAfterLoad {}", | ||
cacheEntry, expireAfterLoad); |
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 would be better to inform what was the cached value rather than directly exposing CacheEntry
because CacheEntry
is an internal term that users may not understand. Additionally .toString()
is not implemented.
logger.debug("CacheEntry {} is expired due to expireAfterLoad {}", | |
cacheEntry, expireAfterLoad); | |
logger.debug("The cached value expired after {} ms. cache: {}", | |
expireAfterLoad.toMillis(), cacheEntry.value); |
} | ||
|
||
if (expireIf != null && expireIf.test(cacheEntry.value)) { | ||
logger.debug("CacheEntry {} is expired due to expireIf matches", 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.
logger.debug("CacheEntry {} is expired due to expireIf matches", cacheEntry); | |
logger.debug("The cached value expired due to 'expireIf' condition. cache: {}", cacheEntry.value); |
* Expires loaded value if predicate matches. | ||
* New value will be loaded by loader on next {@link AsyncLoader#get()}. | ||
*/ | ||
public AsyncLoaderBuilder<T> expireIf(Predicate<@Nullable T> expireIf) { |
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.
public AsyncLoaderBuilder<T> expireIf(Predicate<@Nullable T> expireIf) { | |
public AsyncLoaderBuilder<T> expireIf(Predicate<? super @Nullable T> expireIf) { |
* <p>Note that if 1 refresh is in progress, other refreshes will be bypassed. | ||
* Only 1 refresh is executed at the same time. | ||
*/ | ||
public AsyncLoaderBuilder<T> refreshIf(Predicate<@Nullable T> refreshIf) { |
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.
public AsyncLoaderBuilder<T> refreshIf(Predicate<@Nullable T> refreshIf) { | |
public AsyncLoaderBuilder<T> refreshIf(Predicate<? super @Nullable T> refreshIf) { |
public AsyncLoaderBuilder<T> exceptionHandler(BiFunction< | ||
Throwable, @Nullable T, @Nullable CompletableFuture<T>> exceptionHandler) { |
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.
Although, it looks super verbose. 😅
public AsyncLoaderBuilder<T> exceptionHandler(BiFunction< | |
Throwable, @Nullable T, @Nullable CompletableFuture<T>> exceptionHandler) { | |
public AsyncLoaderBuilder<T> exceptionHandler(BiFunction< | |
? super Throwable, ? super @Nullable T, ? extends @Nullable CompletableFuture<T>> exceptionHandler) { |
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