-
Notifications
You must be signed in to change notification settings - Fork 896
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
Outdated
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
core/src/main/java/com/linecorp/armeria/common/util/DefaultAsyncLoader.java
Show resolved
Hide resolved
.../src/test/java/com/linecorp/armeria/client/auth/oauth2/OAuth2ClientCredentialsGrantTest.java
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
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/DefaultAsyncLoader.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/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/AsyncLoaderBuilder.java
Outdated
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/AsyncLoaderBuilder.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.
Looks great! 👍
Left a few comments.
core/src/main/java/com/linecorp/armeria/common/util/AsyncLoaderBuilder.java
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
core/src/test/java/com/linecorp/armeria/server/websocket/DelegatingWebSocketServiceTest.java
Outdated
Show resolved
Hide resolved
} | ||
|
||
@Override | ||
public CacheEntry<U> join() { |
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.
Question: Do we need to override this method? cacheEntry
is set to the result of the completable future anyway?
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.
oh~ I found that we don't need to override this join()
cause we call obtrudeValue(), obtrudeException()
anyway inside of overrided complete(), completeExceptionally()
👍 thanks!
core/src/test/java/com/linecorp/armeria/common/util/DefaultAsyncLoaderTest.java
Outdated
Show resolved
Hide resolved
core/src/test/java/com/linecorp/armeria/common/util/DefaultAsyncLoaderTest.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/AsyncLoader.java
Outdated
Show resolved
Hide resolved
core/src/test/java/com/linecorp/armeria/server/websocket/DelegatingWebSocketServiceTest.java
Outdated
Show resolved
Hide resolved
@@ -27,6 +27,7 @@ | |||
import org.reactivestreams.Subscription; | |||
|
|||
import com.linecorp.armeria.client.BlockingWebClient; | |||
import com.linecorp.armeria.client.WebClient; |
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.
import com.linecorp.armeria.client.WebClient; |
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.
95592de oh I simply remove this change~! 🙇
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.
Looks nice, @injae-kim! ❤️
Please address @minwoox comments.
* Returns a newly created {@link AsyncLoaderBuilder} with the specified loader. | ||
* @param loader function to load value. {@code T} is previously cached 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.
Let's insert an empty line between method description and parameter list for readability.
* Returns a newly created {@link AsyncLoaderBuilder} with the specified loader. | |
* @param loader function to load value. {@code T} is previously cached value | |
* Returns a newly created {@link AsyncLoaderBuilder} with the specified loader. | |
* | |
* @param loader function to load value. {@code T} is previously cached value |
* AsyncLoader<String> asyncLoader = | ||
* AsyncLoader | ||
* .builder(loader) | ||
* .expireAfterLoad(Duration.ofSeconds(60)) // The loaded value is expired after 60 seconds. |
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.
Let's make sure the line is not too long for readability.
* .expireAfterLoad(Duration.ofSeconds(60)) // The loaded value is expired after 60 seconds. | |
* // Expire the loaded value after 60 seconds. | |
* .expireAfterLoad(Duration.ofSeconds(60)) |
* A builder for creating a new {@link AsyncLoader}. | ||
* | ||
* <p>Expiration should be set by {@link #expireAfterLoad(Duration)} or {@link #expireIf(Predicate)}. | ||
* If expiration is not set, {@link #build()} will throw {@link IllegalStateException}. |
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 about not throwing an ISE
but just building an instance that never expires its value?
* Expires The loaded value after the duration since it was loaded. | ||
* New value will be loaded by the loader function on next {@link AsyncLoader#get()}. | ||
*/ | ||
public AsyncLoaderBuilder<T> expireAfterLoad(Duration 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.
Maybe we could add the expireAfterLoadMillis(long)
shortcut?
private static class CacheEntry<T> { | ||
|
||
private final T value; | ||
private final long cachedAt = System.nanoTime(); |
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.
cachedAtNanos
?
if (cacheEntry == null) { | ||
return 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.
Why is null
an invalid entry value? A user might want to cache null
as the result.
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.
A user might want to cache null as the result.
I guess cacheEntry.value
would be null
in this case
} | ||
} | ||
|
||
private static class RefreshingFuture<U> extends CompletableFuture<CacheEntry<U>> { |
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.
- We should not allow a user to change the state of this future via
complete*()
andobtrude*()
. - Can we create a new future when refreshing a new value, rather than using
obtrude*()
? I wouldn't risk usingobtrude*()
here because:obtrude*()
is designed by error recovery actions, which is not the case here- The future returned by
get()
can be reused by a user later; and - We can do without it.
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.
Looks good in terms of correctness 👍 Thanks @injae-kim 🙇 👍 🙇
* @throws IllegalStateException if no expiration is set. | ||
*/ | ||
public AsyncLoader<T> build() { | ||
if (expireAfterLoad == null && expireIf == 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.
Optional) Not too strong on this, but if CacheEntry
were public then I guess it's possible to just remove expireAfterLoad
and use expireIf
to implement the same functionality.
This way would eliminate the ambiguity on which condition is evaluated first as well.
e.g.
AsyncLoaderBuilder<T> expireIf(Predicate<CacheEntry<? super T>> expireIf) {...}
...
public AsyncLoaderBuilder<T> expireAfterLoad(Duration expireAfterLoad) {
...
expireIf = cacheEntry -> {
final long elapsed = System.nanoTime() - cacheEntry.cachedAt();
return elapsed < expireAfterLoad.toNanos();
};
if (cacheEntry == null) { | ||
return 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.
A user might want to cache null as the result.
I guess cacheEntry.value
would be null
in this case
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