pub(crate) mod bridge;
pub(crate) mod fetch;
pub(crate) mod handoff;
pub(crate) mod rfc3339;
pub(crate) mod update;
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime};
use lingara::AccessToken;
use tokio::sync::watch;
use tokio::task::JoinHandle;
use tokio::time::Instant;
use crate::error::{Diagnostics, EmbedError, ErrorKind};
use crate::handoff::IssuedToken;
use crate::ids::Ids;
use crate::queue::Outbox;
use crate::sync::lock;
use crate::token_fetch::Mode;
pub(crate) use update::UpdateLatch;
use crate::types::{EmbedEvent, TokenReason};
struct Cached {
token: AccessToken,
stale_at: Instant,
}
impl Cached {
fn fresh(&self) -> bool {
Instant::now() < self.stale_at
}
}
struct State {
cached: Option<Cached>,
need: Option<u64>,
reason: TokenReason,
mode: Mode,
timer: Option<JoinHandle<()>>,
}
#[derive(Clone, Default)]
pub(crate) struct Generation {
pub n: u64,
pub failure: Option<EmbedError>,
}
pub(crate) struct HostTokenSource {
state: Mutex<State>,
changed: watch::Sender<Generation>,
fetching: tokio::sync::Mutex<()>,
outbox: Outbox,
diag: Diagnostics,
ids: Arc<Ids>,
wait: Duration,
update: UpdateLatch,
}
impl HostTokenSource {
pub fn new(mode: Mode, diag: Diagnostics, ids: Arc<Ids>, wait: Duration, update: UpdateLatch) -> Arc<Self> {
let state = State { cached: None, need: None, reason: TokenReason::Initial, mode, timer: None };
let (changed, _) = watch::channel(Generation::default());
let outbox = diag.outbox.clone();
Arc::new(Self { state: Mutex::new(state), changed, fetching: tokio::sync::Mutex::new(()), outbox, diag, ids, wait, update })
}
pub(crate) async fn ready(self: &Arc<Self>) -> Result<AccessToken, EmbedError> {
loop {
let mut changes = self.changed.subscribe();
let mode = {
let mut state = lock(&self.state);
if let Some(cached) = state.cached.as_ref().filter(|c| c.fresh()) {
return Ok(cached.token.clone());
}
if matches!(state.mode, Mode::Poll) {
self.open_need(&mut state);
}
state.mode.clone()
};
match mode {
Mode::Poll => self.wait_for_change(&mut changes).await?,
source => self.fetch(source).await?,
}
}
}
fn open_need(&self, state: &mut State) {
if state.need.is_none() {
let request = self.ids.next();
state.need = Some(request);
self.outbox.priority(EmbedEvent::TokenNeeded { request, reason: state.reason });
}
}
async fn wait_for_change(&self, changes: &mut watch::Receiver<Generation>) -> Result<(), EmbedError> {
match tokio::time::timeout(self.wait, changes.changed()).await {
Ok(Ok(())) => changes.borrow().failure.clone().map_or(Ok(()), Err),
Ok(Err(_)) => Err(EmbedError::closed()),
Err(_) => {
let err = EmbedError::new(ErrorKind::TokenUnavailable, format!("no token within {} ms", self.wait.as_millis()));
self.fail_all(err.clone());
Err(err)
}
}
}
async fn fetch(self: &Arc<Self>, mode: Mode) -> Result<(), EmbedError> {
let _one = self.fetching.lock().await;
let reason = {
let state = lock(&self.state);
if state.cached.as_ref().is_some_and(Cached::fresh) {
return Ok(());
}
state.reason
};
let issued = match tokio::time::timeout(self.wait, fetch::fetch(&mode, reason)).await {
Ok(result) => result,
Err(_) => Err(EmbedError::new(ErrorKind::TokenUnavailable, format!("the token source gave nothing within {} ms", self.wait.as_millis()))),
};
let stored = issued.and_then(|issued| self.store(&issued));
if let Err(err) = &stored {
self.fail_all(err.clone());
}
stored
}
pub fn store(self: &Arc<Self>, issued: &IssuedToken) -> Result<(), EmbedError> {
handoff::check_embed_token(&issued.token)?;
let lifetime = handoff::lifetime(issued, SystemTime::now())?;
if let Some(message) = lifetime.clamped {
self.diag.warn("lingara_embed::token", message);
}
let stale_at = Instant::now() + handoff::stale_after(lifetime.duration);
let me = Arc::clone(self);
let timer = self.outbox.handle().spawn(async move {
tokio::time::sleep_until(stale_at).await;
me.on_stale();
});
let mut state = lock(&self.state);
state.cached = Some(Cached { token: AccessToken::new(issued.token.expose_secret()), stale_at });
state.need = None;
state.reason = TokenReason::Expiring;
if let Some(old) = state.timer.replace(timer) {
old.abort();
}
drop(state);
self.bump(None);
self.update.hear(issued.update_available.as_ref(), &self.diag);
Ok(())
}
fn on_stale(self: &Arc<Self>) {
let mut state = lock(&self.state);
match state.mode.clone() {
Mode::Poll => self.open_need(&mut state),
source => {
drop(state);
let me = Arc::clone(self);
self.outbox.handle().spawn(async move {
let _ = me.fetch(source).await;
});
}
}
}
pub fn fail_all(&self, err: EmbedError) {
let mut state = lock(&self.state);
state.need = None;
state.cached = None;
drop(state);
self.bump(Some(err));
}
fn bump(&self, failure: Option<EmbedError>) {
self.changed.send_modify(|g| {
g.n += 1;
g.failure = failure;
});
}
pub fn ask(&self) {
let mut state = lock(&self.state);
if matches!(state.mode, Mode::Poll) && !state.cached.as_ref().is_some_and(Cached::fresh) {
self.open_need(&mut state);
}
}
pub fn set_mode(&self, mode: Mode) {
lock(&self.state).mode = mode;
}
pub fn needs_token(&self) -> bool {
lock(&self.state).need.is_some()
}
pub fn subscribe(&self) -> watch::Receiver<Generation> {
self.changed.subscribe()
}
pub fn shutdown(&self) {
if let Some(timer) = lock(&self.state).timer.take() {
timer.abort();
}
}
}