Skip to content

Extend max token stale period buffer with dynamic computation based on TTL#1293

Merged
mihaimitrea-db merged 12 commits intomainfrom
mihaimitrea-db/auth_token_buffer_extension
Mar 2, 2026
Merged

Extend max token stale period buffer with dynamic computation based on TTL#1293
mihaimitrea-db merged 12 commits intomainfrom
mihaimitrea-db/auth_token_buffer_extension

Conversation

@mihaimitrea-db
Copy link
Contributor

Summary

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.

The TTL is computed based on the time to live at the moment the token is received and not it's real TTL. For example, if the token has a TTL of 60 mins, but has been acquired 20 mins before being used by the SDK (e.g. though the CLI), it's TTL will be 60 - 20 = 40 mins.

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.

Backward compatibility:

  • The exposed stale_period attribute in the Refreshable constructor is preserved. Passing a value will now make the implementation revert back to the legacy stale period duration of 5 minutes.

Implementation:

  • Add _update_token() helper that stores the token and pre-computes _stale_duration as min(TTL × 0.5, max_stale_duration).
  • Update init() to call _update_token() for the initial token.
  • Update _blocking_token() to recompute stale period after a synchronous refresh.
  • Update _trigger_async_refresh() to recompute stale period after an async refresh.
  • Rename _DEFAULT_STALE_DURATION / stale_duration to _MAX_STALE_DURATION / max_stale_duration 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:

  • Modify _MockRefreshable class to use the dynamic stale period
  • Add test_dynamic_stale_period to test the fresh -> stale -> refresh lifecycle for all types of tokens (standard OAuth, FastPath, and very short tokens).
  • All tests and linters pass

… short-lived tokens.

Introduce a max stale period of 20 minutes (previously a flat of 5 minutes for all token types), that dinamically scales down based on the token's TTL. The stale period is now computed at token acquisition time as: min(TTL x 0.5, max_stale_period).
Modify the MockRefreshable to work with the dynamic stale period. You can now override the dynamic refresh, but otherwise it will default to it.

Add test_dynamic_stale_period test which verifies the whole fresh -> stale -> async refresh -> expired -> blobking refresh lifecycle for each of the three types of expected token lifespans (90 mins, 10 mins, 90 sec).
Signed-off-by: mihaimitrea-db <mihai.mitrea@databricks.com>
When the TTL is < 0 (the token is expired) the stale period is set to 0 to avoid having a negative stale period.
…fer_extension' into auth_token_buffer_extension
…ment

Reintroduce the stale_period attribute in the Refreshable constructor for backwards compatibility as an optional field. If it is present default back to the legacy fixed stale period of 5 minutes. If it is not present the dynamic stale period based on the token's TTL is used.

The _use_dynamic_stale_duration flag is used to differentiate between these two methods.
# Config properties
self._stale_duration = stale_duration
self._use_dynamic_stale_duration = stale_duration is None
self._stale_duration = stale_duration if stale_duration is not None else timedelta(seconds=0)
Copy link
Contributor

Choose a reason for hiding this comment

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

[optional] Wondering if it won't be simpler to make this a timestamp rather than a duration.

Comment on lines +293 to +297
"""
For each token type, verifies:
- Stale tokens trigger async refresh (returns old token immediately)
- Expired tokens trigger blocking refresh (blocks and returns new token)
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we separate this into two tests? Also, is there a way we could avoid waiting for 1 second in the loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, 1 second is too much. I think without changing the implementation of blocking_token() I can drop this to 0.1.

Does that work?

Copy link
Contributor

Choose a reason for hiding this comment

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

Discussed offline, let's proceed with the code as it is and maybe follow-up with using threading to wait exactly the right amount of time. The goal here is to make sure that we are most wasting seconds on a test suite that should run in 2 digits milliseconds.

Address PR feedback by:
- Splitting test_dynamic_stale_period into two tests:
  - test_dynamic_stale_period_blocking_refresh: expired token triggers blocking refresh
  - test_dynamic_stale_period_async_refresh: stale token triggers async refresh
- Reducing the post-unblock wait from 1s to 0.1s

Full removal of the wait (e.g. via threading or explicit event) to be done in a follow-up.
@github-actions
Copy link

github-actions bot commented Mar 2, 2026

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-py

Inputs:

  • PR number: 1293
  • Commit SHA: 9bddcb45e9c904e9e3417bf8526978efcb2b595f

Checks will be approved automatically on success.

@mihaimitrea-db mihaimitrea-db enabled auto-merge March 2, 2026 15:43
@mihaimitrea-db mihaimitrea-db added this pull request to the merge queue Mar 2, 2026
Merged via the queue into main with commit 4940eff Mar 2, 2026
17 checks passed
@mihaimitrea-db mihaimitrea-db deleted the mihaimitrea-db/auth_token_buffer_extension branch March 2, 2026 16:20
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.

2 participants