Skip to content

Caching added similar to Swift - #1318

Merged
MaxHeimbrock merged 4 commits into
mainfrom
max/token-source-cached
Aug 14, 2026
Merged

Caching added similar to Swift#1318
MaxHeimbrock merged 4 commits into
mainfrom
max/token-source-cached

Conversation

@MaxHeimbrock

@MaxHeimbrock MaxHeimbrock commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Background

Stacked cached token source in Rust, similar to swift: https://github.com/livekit/client-sdk-swift/blob/main/Sources/LiveKit/Token/CachingTokenSource.swift

@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Changeset ✓

This PR includes a changeset covering all affected packages:

Package Bump
livekit-token-source minor

@MaxHeimbrock
MaxHeimbrock force-pushed the max/token-source-cached branch 2 times, most recently from 4adf9c6 to c99ad83 Compare August 6, 2026 14:52
@MaxHeimbrock
MaxHeimbrock force-pushed the max/token-source-cached branch from c99ad83 to a8baf86 Compare August 6, 2026 14:56
@MaxHeimbrock
MaxHeimbrock force-pushed the max/token-source-cached branch from a8baf86 to c94b983 Compare August 7, 2026 10:02
@MaxHeimbrock
MaxHeimbrock marked this pull request as ready for review August 7, 2026 10:03
@MaxHeimbrock
MaxHeimbrock requested a review from ladvoc as a code owner August 7, 2026 10:03
devin-ai-integration[bot]

This comment was marked as resolved.

@MaxHeimbrock
MaxHeimbrock force-pushed the max/token-source-cached branch from c94b983 to 8774ee0 Compare August 7, 2026 10:05
@MaxHeimbrock
MaxHeimbrock marked this pull request as draft August 10, 2026 15:11
@ladvoc
ladvoc force-pushed the max/token-source-cached branch from 8774ee0 to d281120 Compare August 10, 2026 17:52
Base automatically changed from max/token-source to main August 10, 2026 18:30
@ladvoc
ladvoc force-pushed the max/token-source-cached branch from d281120 to 1b15456 Compare August 10, 2026 18:30
@MaxHeimbrock
MaxHeimbrock force-pushed the max/token-source-cached branch from 1b15456 to 1fdbc89 Compare August 13, 2026 14:45
@MaxHeimbrock
MaxHeimbrock marked this pull request as ready for review August 13, 2026 14:58

@xianshijing-lk xianshijing-lk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

some nits and questions, lgtm in general

/// Removes the stored credentials, forcing the next fetch to hit the
/// underlying source.
pub async fn invalidate(&self) {
self.store.clear().await;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

what will happen if a fetch is ongoing here ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added documentation:

/// A fetch already in flight is unaffected: it still resolves and stores
/// its response afterwards, repopulating the cache (last writer wins).

}
}

let response = self.source.fetch(options).await?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

curiously, can multiple fetch() be called at the same time ? I wonder if we will need to protect such corner case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, and we don't protect against this, same as in Swift btw. This would be a bigger design. I think as other SDKs are not that advanced either yet, maybe we keep it in mind as a follow up.


#[async_trait]
impl TokenSourceConfigurable for CountingSource {
async fn fetch(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit, should this fetch() has a .await to mimic the real use case ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In these tests it would not change the behaviour, it would still run in sequence.

I added tests though for a token source that can mimic parallel in flight requests to test the existing contract of last writer wins.

@MaxHeimbrock
MaxHeimbrock force-pushed the max/token-source-cached branch from fdb0638 to 98e948c Compare August 14, 2026 08:54
@MaxHeimbrock
MaxHeimbrock merged commit 4fe31e3 into main Aug 14, 2026
26 checks passed
@MaxHeimbrock
MaxHeimbrock deleted the max/token-source-cached branch August 14, 2026 10:13
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