Skip to content

Implement adaptive stale period for auth tokens#1482

Open
mihaimitrea-db wants to merge 5 commits intomainfrom
mihai-mitrea_data/auth_token_buffer_extension
Open

Implement adaptive stale period for auth tokens#1482
mihaimitrea-db wants to merge 5 commits intomainfrom
mihai-mitrea_data/auth_token_buffer_extension

Conversation

@mihaimitrea-db
Copy link

Implement dynamic token stale period for improved auth reliability

Extends the token refresh buffer from a fixed 5 minutes to a dynamic period that adapts to token lifetime, improving reliability across different token types while maintaining backward compatibility.

Changes:

  • Increase maximum stale period from 5 to 20 minutes to support 99.95% availability .
  • Implement dynamic stale period calculation: min(TTL × 0.5, 20 minutes).
  • Compute stale period per-token at acquisition time.

Implementation:

  • Add computeStalePeriod() helper with formula: min(TTL × 0.5, 20 min)
  • Update NewCachedTokenSource() to compute stale period for initial tokens
  • Update blockingToken() to recompute stale period after token fetch
  • Update triggerAsyncRefresh() to recompute stale period after async refresh
  • Rename defaultStaleDuration to maxStaleDuration for clarity

Token behavior examples:

  • Standard OAuth (60 min TTL) → 20 min stale period (capped)
  • FastPath (10 min TTL) → 5 min stale period
  • Very short (90 sec TTL) → 45 sec stale period

Testing:

  • Add assertDurationNear() helper for cleaner duration comparisons
  • Add TestNewCachedTokenSource_standardOAuthToken (60-min tokens, capping)
  • Add TestNewCachedTokenSource_fastPathToken (10-min tokens, no capping)
  • Add TestNewCachedTokenSource_stalePeriodRecomputation (90-sec tokens, recomputation)
  • All existing tests pass with updated expectations

mihaimitrea-db and others added 4 commits February 17, 2026 13:31
Compute stale period dynamically as min(TTL × 0.5, 20 minutes) instead of using a fixed 5-minute value. This ensures tokens with shorter lifetimes  e.g., FastPath's 10-minute TTL) refresh appropriately without triggering premature expirations, while standard OAuth tokens continue using the 20-minute stale period for robustness.
Extract the repeated staleDuration update pattern into a cachedTokenSource
method (updateStaleDuration), placed alongside tokenState() where both
token-freshness concerns live together.

Add a withTimeNow test option to inject a fixed clock at construction time,
so updateStaleDuration() is deterministic from the first call. This removes
the post-construction Expiry/timeNow overrides and the tolerance-based
assertDurationNear helper, replacing all stale-duration assertions with
exact equality checks.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Signed-off-by: Mihai Mitrea <[email protected]>
@github-actions
Copy link

If integration tests don't run automatically, an authorized user can run them manually by following the instructions below:

Trigger:
go/deco-tests-run/sdk-go

Inputs:

  • PR number: 1482
  • Commit SHA: 1b24b4d278e1cd21fabe24a33873278ccb812a2b

Checks will be approved automatically on success.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments