Skip to content

Commit 393b548

Browse files
Fix stale comment, null guard, and minor cleanups in CachedTokenSource
- Update field comment on staleAfter to reflect its Instant type - Guard against null newToken in async refresh before calling cachedTokenIsNewer - Remove redundant synchronized block in handleFailedAsyncRefresh - Collapse duplicate null-check branches in updateToken - Use imperative tense in changelog entry Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent ba30e69 commit 393b548

2 files changed

Lines changed: 7 additions & 15 deletions

File tree

NEXT_CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
### Documentation
1212

1313
### Internal Changes
14-
* Added retry with backoff to `CachedTokenSource` async refresh so that a failed background refresh no longer disables async until a blocking call succeeds.
14+
* Add retry with backoff to `CachedTokenSource` async refresh so that a failed background refresh no longer disables async until a blocking call succeeds.
1515

1616
### API Changes
1717
* Add `com.databricks.sdk.service.dataclassification` and `com.databricks.sdk.service.knowledgeassistants` packages.

databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/CachedTokenSource.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private enum TokenState {
5353
private final Duration staticStaleDuration;
5454
// Whether to use the dynamic stale duration computation or defer to the legacy duration.
5555
private final boolean useLegacyStaleDuration;
56-
// The dynamically computed duration before expiry to consider a token as 'stale'.
56+
// The earliest time at which the cached token should be considered stale.
5757
private volatile Instant staleAfter;
5858
// Additional buffer before expiry to consider a token as expired.
5959
private final Duration expiryBuffer;
@@ -199,13 +199,7 @@ public CachedTokenSource build() {
199199
* @param t The token to cache. May be null.
200200
*/
201201
private void updateToken(Token t) {
202-
if (t == null) {
203-
this.staleAfter = null;
204-
this.token = t;
205-
return;
206-
}
207-
208-
if (t.getExpiry() == null) {
202+
if (t == null || t.getExpiry() == null) {
209203
this.staleAfter = null;
210204
this.token = t;
211205
return;
@@ -239,11 +233,9 @@ private void updateToken(Token t) {
239233
* auth service is unhealthy.
240234
*/
241235
private void handleFailedAsyncRefresh() {
242-
synchronized (this) {
243-
if (this.staleAfter != null) {
244-
Instant now = Instant.now(clockSupplier.getClock());
245-
this.staleAfter = now.plus(ASYNC_REFRESH_RETRY_BACKOFF);
246-
}
236+
if (this.staleAfter != null) {
237+
Instant now = Instant.now(clockSupplier.getClock());
238+
this.staleAfter = now.plus(ASYNC_REFRESH_RETRY_BACKOFF);
247239
}
248240
}
249241

@@ -373,7 +365,7 @@ private synchronized void triggerAsyncRefresh() {
373365
try {
374366
Token newToken = tokenSource.getToken();
375367
synchronized (this) {
376-
if (!cachedTokenIsNewer(newToken)) {
368+
if (newToken != null && !cachedTokenIsNewer(newToken)) {
377369
updateToken(newToken);
378370
}
379371
refreshInProgress = false;

0 commit comments

Comments
 (0)