Skip to content

Refactor Token Caching with CachedTokenSource Wrapper #461

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

Open
wants to merge 45 commits into
base: main
Choose a base branch
from

Conversation

emmyzhou-db
Copy link
Contributor

@emmyzhou-db emmyzhou-db commented Jun 10, 2025

Changes

This PR refactors the token refresh and caching in the Java SDK by:

  • Removing the RefreshableTokenSource class that previously handled token refresh and caching logic directly
  • Introducing CachedTokenSource as a generic wrapper that can be applied to any TokenSource implementation
  • Separating concerns by moving caching and async logic out of individual token sources into a dedicated cache wrapper

Motivation

Previously, token sources inherited from RefreshableTokenSource to gain caching and asynchronous refresh capabilities. However, this inheritance-based approach had limitations when we wanted to make configuration immutable after construction using the builder pattern.

The Problem: With abstract classes in Java, constructors are not inherited. To support builder-based configuration, each token source subclass would need to be modified to accept a builder instead of individual parameters, requiring changes across many classes.

The Solution: Switch from inheritance to composition. Instead of inheriting caching behavior, we now wrap any TokenSource which previously inherited from RefreshableTokenSource with CachedTokenSource to add caching functionality externally. This allows us to use the builder pattern to configure optional parameters like async refresh and stale duration on each wrapped instance.

How is this tested?

Existing unit and integration tests.

NO_CHANGELOG=true

@emmyzhou-db emmyzhou-db changed the title [DO NOT MERGE] Add Async Token Cache Wrapper Add Async Token Cache Wrapper Jun 18, 2025
@emmyzhou-db emmyzhou-db changed the title Add Async Token Cache Wrapper Refactor token caching with CachedTokenSource wrapper Jun 18, 2025
@emmyzhou-db emmyzhou-db changed the title Refactor token caching with CachedTokenSource wrapper Refactor Token Caching with CachedTokenSource wrapper Jun 18, 2025
@emmyzhou-db emmyzhou-db changed the title Refactor Token Caching with CachedTokenSource wrapper Refactor Token Caching with CachedTokenSource Wrapper Jun 18, 2025
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-java

Inputs:

  • PR number: 461
  • Commit SHA: 75367bfc7b977d4eda43daa779d3f1c988bec2e0

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.

2 participants