AsyncLoader can be useful in the following situations.
- When it is necessary to periodically read and update information from a file such as
resolv.conf .
- When data is not valid after a certain period of time, such as an OAuth 2.0 access token.
We already have an implementation for that. However, I hope to generalize it and add new features to use it in various cases.
|
final CompletableFuture<GrantedOAuth2AccessToken> tokenFuture = this.tokenFuture; |
|
if (!tokenFuture.isDone()) { |
|
return tokenFuture; |
|
} |
|
if (!tokenFuture.isCompletedExceptionally()) { |
|
token = tokenFuture.join(); |
|
if (isValidToken(token)) { |
|
return tokenFuture; |
|
} |
|
} |
|
|
|
// `tokenFuture` got completed with an invalid token; try again. |
|
future = new CompletableFuture<>(); |
|
if (tokenFutureUpdater.compareAndSet(this, tokenFuture, future)) { |
|
break; |
|
} |
|
} |
API proposal:
@FunctionalInterface
interface AsyncLoader<T> {
static <T> AsyncLoaderBuilder<T> builder(Supplier<CompletableFuture<T>> loader) {
return new AsyncLoaderBuilder<T>(loader);
}
CompletableFuture<T> get();
}
class AsyncLoaderBuilder<T> {
AsyncLoaderBuilder<T> expireAfterLoad(Duration duration) {
...
}
AsyncLoaderBuilder<T> expireIf(Predicate<T> predicate) {
...
}
}
AsyncLoadercan be useful in the following situations.resolv.conf.We already have an implementation for that. However, I hope to generalize it and add new features to use it in various cases.
armeria/oauth2/src/main/java/com/linecorp/armeria/client/auth/oauth2/AbstractOAuth2AuthorizationGrant.java
Lines 118 to 134 in 4bfa172
API proposal: