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

OAuth2Client: improve sleep/close detection #8337

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ class OAuth2Client implements OAuth2Authenticator, Closeable {
.getExecutor()
.orElseGet(
() -> new OAuth2TokenRefreshExecutor(config.getBackgroundThreadIdleTimeout()));
lastAccess = config.getClock().get();
currentTokensStage = started.thenApplyAsync((v) -> fetchNewTokens(), this.executor);
currentTokensStage
.whenComplete((tokens, error) -> log(error))
Expand Down Expand Up @@ -128,6 +127,7 @@ private Tokens getCurrentTokensIfAvailable() {

@Override
public void start() {
lastAccess = config.getClock().get();
started.complete(null);
}

Expand Down Expand Up @@ -158,6 +158,9 @@ public void close() {
}

private void wakeUp(Instant now) {
if (closing.get()) {
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe log a debug message in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me merge this as is and I'll add the debug message in #8338.

}
LOGGER.debug("Waking up...");
Tokens currentTokens = getCurrentTokensIfAvailable();
Duration delay = nextTokenRefresh(currentTokens, now, Duration.ZERO);
Expand All @@ -171,6 +174,9 @@ private void wakeUp(Instant now) {
}

private void maybeScheduleTokensRenewal(Tokens currentTokens) {
if (closing.get()) {
return;
}
Instant now = config.getClock().get();
if (Duration.between(lastAccess, now).compareTo(config.getPreemptiveTokenRefreshIdleTimeout())
> 0) {
Expand Down Expand Up @@ -215,8 +221,7 @@ private void renewTokens() {

private void log(Throwable error) {
if (error != null) {
boolean tokensStageCancelled = error instanceof CancellationException && closing.get();
if (tokensStageCancelled) {
if (closing.get()) {
return;
}
if (error instanceof CompletionException) {
Expand Down Expand Up @@ -250,10 +255,14 @@ Tokens fetchNewTokens() {
case AUTHORIZATION_CODE:
try (AuthorizationCodeFlow flow = new AuthorizationCodeFlow(config)) {
return flow.fetchNewTokens();
} finally {
lastAccess = config.getClock().get();
dimas-b marked this conversation as resolved.
Show resolved Hide resolved
}
case DEVICE_CODE:
try (DeviceCodeFlow flow = new DeviceCodeFlow(config)) {
return flow.fetchNewTokens();
} finally {
lastAccess = config.getClock().get();
}
default:
throw new IllegalStateException("Unsupported grant type: " + config.getGrantType());
Expand Down