Skip to content
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

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

Open
ikhoon opened this issue Mar 15, 2024 · 2 comments · May be fixed by #5590
Open

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

ikhoon opened this issue Mar 15, 2024 · 2 comments · May be fixed by #5590
Labels
new feature sprint Issues for OSS Sprint participants

Comments

@ikhoon
Copy link
Contributor

ikhoon commented Mar 15, 2024

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

}
@injae-kim
Copy link
Contributor

oh interesting feature! may I handle this? I'll create PR within 4/22 🙇

@jrhee17 jrhee17 added the sprint Issues for OSS Sprint participants label Apr 1, 2024
injae-kim added a commit to injae-kim/armeria that referenced this issue Apr 10, 2024
injae-kim added a commit to injae-kim/armeria that referenced this issue Apr 10, 2024
@injae-kim
Copy link
Contributor

Create PR #5590 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature sprint Issues for OSS Sprint participants
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants