Skip to content

Provide a way to load a value periodically or when it expires #5506

@ikhoon

Description

@ikhoon

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) {
        ...
    }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    new featuresprintIssues for OSS Sprint participants

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions