pub struct TokenCache { /* private fields */ }Expand description
Thread-safe JWT cache with near-expiry refresh and single-flight semantics.
Wrap in Arc<TokenCache> and share across tasks.
Implementations§
Source§impl TokenCache
impl TokenCache
Sourcepub fn new(source: Box<dyn TokenSource>, config: TokenCacheConfig) -> Self
pub fn new(source: Box<dyn TokenSource>, config: TokenCacheConfig) -> Self
Create a new cache backed by source.
pub fn with_clock(self, clock: ArcClock) -> Self
Sourcepub fn with_initial_token(self, token: impl Into<String>) -> Self
pub fn with_initial_token(self, token: impl Into<String>) -> Self
Seed the cache with an already-held token (e.g. loaded from disk), builder-style, before the cache is shared.
The stored expiry is derived from the token’s JWT exp claim (via
parse_expiry) minus TokenCacheConfig::refresh_skew — the same
discount the fetch path applies. Tokens without a parseable exp
are ignored, and an already-stale seed is filtered by get()’s
freshness check — either way the first get falls
through to the source, which is the safe direction.
This exists so a seeded cache never confuses “still-valid stored
token” with “freshly minted token”: seeding happens exactly once at
construction, so invalidate always forces a
genuine re-mint (a source that re-served stored state would hand the
same dead token back after a 401).
Sourcepub async fn get(&self) -> Result<String, TokenCacheError>
pub async fn get(&self) -> Result<String, TokenCacheError>
Return the current token, refreshing via the source if near-expired.
Sourcepub async fn invalidate(&self)
pub async fn invalidate(&self)
Discard the cached token so the next get re-mints.
Call this when the server rejects the current token (a 401 /
Unauthenticated): the token is dead — revoked, session-epoch-bumped,
or rotated out from under a still-valid clock TTL — so clock-based expiry
is the wrong staleness signal. Forcing a fetch lets a caller recover on
its next request instead of waiting out the remaining TTL.
Auto Trait Implementations§
impl !Freeze for TokenCache
impl !RefUnwindSafe for TokenCache
impl !UnwindSafe for TokenCache
impl Send for TokenCache
impl Sync for TokenCache
impl Unpin for TokenCache
impl UnsafeUnpin for TokenCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request