Skip to content

Commit

Permalink
OAUth2Client: defer initial token fetch when user interaction is requ…
Browse files Browse the repository at this point in the history
…ired
  • Loading branch information
adutra committed Apr 18, 2024
1 parent 62aaddd commit 11b8d54
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class OAuth2Client implements OAuth2Authenticator, Closeable {
private final HttpClient httpClient;
private final ScheduledExecutorService executor;
private final CompletableFuture<Void> started = new CompletableFuture<>();
private final CompletableFuture<Void> used = new CompletableFuture<>();
/* Visible for testing. */ final AtomicBoolean sleeping = new AtomicBoolean();
private final AtomicBoolean closing = new AtomicBoolean();

Expand All @@ -70,14 +71,21 @@ class OAuth2Client implements OAuth2Authenticator, Closeable {
.getExecutor()
.orElseGet(
() -> new OAuth2TokenRefreshExecutor(config.getBackgroundThreadIdleTimeout()));
currentTokensStage = started.thenApplyAsync((v) -> fetchNewTokens(), this.executor);
// when user interaction is not required, token fetch can happen immediately upon start();
// otherwise, it will be deferred until authenticate() is called the first time.
CompletableFuture<?> ready =
config.getGrantType().requiresUserInteraction()
? CompletableFuture.allOf(started, used)
: started;
currentTokensStage = ready.thenApplyAsync((v) -> fetchNewTokens(), executor);
currentTokensStage
.whenComplete((tokens, error) -> log(error))
.whenComplete((tokens, error) -> maybeScheduleTokensRenewal(tokens));
}

@Override
public AccessToken authenticate() {
used.complete(null);
Instant now = config.getClock().get();
lastAccess = now;
if (sleeping.compareAndSet(true, false)) {
Expand Down

0 comments on commit 11b8d54

Please sign in to comment.