use std::future::Future;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;
#[derive(Debug, Clone)]
pub(crate) struct TemporaryToken<T> {
pub token: T,
pub expiry: Option<Instant>,
}
#[derive(Debug)]
pub(crate) struct TokenCache<T> {
cache: RwLock<Option<CacheEntry<T>>>,
min_ttl: Duration,
fetch_backoff: Duration,
}
#[derive(Debug)]
struct CacheEntry<T> {
token: TemporaryToken<T>,
fetched_at: Instant,
}
impl<T> Default for TokenCache<T> {
fn default() -> Self {
Self {
cache: Default::default(),
min_ttl: Duration::from_secs(300),
fetch_backoff: Duration::from_millis(100),
}
}
}
impl<T: Clone + Send + Sync> TokenCache<T> {
#[cfg(any(feature = "aws", feature = "gcp"))]
pub(crate) fn with_min_ttl(self, min_ttl: Duration) -> Self {
Self { min_ttl, ..self }
}
pub(crate) async fn get_or_insert_with<F, Fut, E>(&self, f: F) -> Result<T, E>
where
F: FnOnce() -> Fut + Send,
Fut: Future<Output = Result<TemporaryToken<T>, E>> + Send,
{
let now = Instant::now();
let is_token_valid = |entry: &CacheEntry<T>| {
entry.token.expiry.is_none_or(|ttl| {
ttl.checked_duration_since(now).unwrap_or_default() > self.min_ttl ||
(entry.fetched_at.elapsed() < self.fetch_backoff && ttl > now)
})
};
if let Some(cache) = self.cache.read().await.as_ref()
&& is_token_valid(cache)
{
return Ok(cache.token.token.clone());
}
let mut guard = self.cache.write().await;
if let Some(cache) = guard.as_ref()
&& is_token_valid(cache)
{
return Ok(cache.token.token.clone());
}
let cached = f().await?;
let token = cached.token.clone();
*guard = Some(CacheEntry {
token: cached,
fetched_at: Instant::now(),
});
Ok(token)
}
}
#[cfg(test)]
mod test {
use crate::client::token::{TemporaryToken, TokenCache};
use std::sync::atomic::{AtomicU32, Ordering};
use std::time::{Duration, Instant};
fn create_token(expiry_duration: Option<Duration>) -> TemporaryToken<String> {
TemporaryToken {
token: "test_token".to_string(),
expiry: expiry_duration.map(|d| Instant::now() + d),
}
}
#[tokio::test]
async fn test_expired_token_is_refreshed() {
let cache = TokenCache::default();
static COUNTER: AtomicU32 = AtomicU32::new(0);
async fn get_token() -> Result<TemporaryToken<String>, String> {
COUNTER.fetch_add(1, Ordering::SeqCst);
Ok::<_, String>(create_token(Some(Duration::from_secs(0))))
}
let _ = cache.get_or_insert_with(get_token).await.unwrap();
assert_eq!(COUNTER.load(Ordering::SeqCst), 1);
tokio::time::sleep(Duration::from_millis(2)).await;
let _ = cache.get_or_insert_with(get_token).await.unwrap();
assert_eq!(COUNTER.load(Ordering::SeqCst), 2);
}
#[tokio::test]
async fn test_min_ttl_causes_refresh() {
let cache = TokenCache {
cache: Default::default(),
min_ttl: Duration::from_secs(1),
fetch_backoff: Duration::from_millis(1),
};
static COUNTER: AtomicU32 = AtomicU32::new(0);
async fn get_token() -> Result<TemporaryToken<String>, String> {
COUNTER.fetch_add(1, Ordering::SeqCst);
Ok::<_, String>(create_token(Some(Duration::from_millis(100))))
}
let _ = cache.get_or_insert_with(get_token).await.unwrap();
assert_eq!(COUNTER.load(Ordering::SeqCst), 1);
let _ = cache.get_or_insert_with(get_token).await.unwrap();
assert_eq!(COUNTER.load(Ordering::SeqCst), 1);
tokio::time::sleep(Duration::from_millis(2)).await;
let _ = cache.get_or_insert_with(get_token).await.unwrap();
assert_eq!(COUNTER.load(Ordering::SeqCst), 2);
}
}