use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use crate::auth::policy::Clock;
struct IdleWindow {
clock: Arc<dyn Clock>,
timeout: Duration,
deadline: Mutex<Instant>,
}
pub struct Residency {
live: AtomicBool,
idle: Option<IdleWindow>,
}
impl std::fmt::Debug for Residency {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Residency")
.field("live", &self.is_live())
.field("idle_bounded", &self.idle.is_some())
.finish()
}
}
impl Residency {
pub(crate) fn new() -> Self {
Self {
live: AtomicBool::new(true),
idle: None,
}
}
pub(crate) fn idle_bounded(clock: Arc<dyn Clock>, timeout: Duration) -> Self {
let deadline = clock.now() + timeout;
Self {
live: AtomicBool::new(true),
idle: Some(IdleWindow {
clock,
timeout,
deadline: Mutex::new(deadline),
}),
}
}
pub fn is_live(&self) -> bool {
self.live.load(Ordering::Acquire) && !self.idle.as_ref().is_some_and(IdleWindow::has_lapsed)
}
pub(crate) fn touch(&self) {
if !self.is_live() {
return;
}
if let Some(idle) = &self.idle {
idle.refresh();
}
}
pub(crate) fn revoke(&self) {
self.live.store(false, Ordering::Release);
}
}
impl IdleWindow {
fn has_lapsed(&self) -> bool {
self.clock.now()
>= *self
.deadline
.lock()
.expect("the idle deadline lock is never poisoned")
}
fn refresh(&self) {
*self
.deadline
.lock()
.expect("the idle deadline lock is never poisoned") = self.clock.now() + self.timeout;
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Arc;
use std::time::{Duration, Instant};
#[test]
fn a_fresh_residency_is_live_and_revocation_is_visible_through_every_clone() {
let residency = Arc::new(Residency::new());
let capability = residency.clone();
assert!(capability.is_live());
residency.revoke();
assert!(
!capability.is_live(),
"a capability holding its own reference must observe the revocation"
);
}
struct TestClock(std::sync::Mutex<Instant>);
impl TestClock {
fn started_now() -> Arc<Self> {
Arc::new(Self(std::sync::Mutex::new(Instant::now())))
}
fn advance(&self, d: Duration) {
*self.0.lock().unwrap() += d;
}
}
impl Clock for TestClock {
fn now(&self) -> Instant {
*self.0.lock().unwrap()
}
}
#[test]
fn an_idle_bounded_residency_dies_when_the_clock_passes_its_deadline() {
let clock = TestClock::started_now();
let residency = Residency::idle_bounded(clock.clone(), Duration::from_secs(60));
clock.advance(Duration::from_secs(59));
assert!(residency.is_live(), "59s of quiet is inside a 60s window");
clock.advance(Duration::from_secs(1));
assert!(
!residency.is_live(),
"the deadline is reached, and the window is exclusive"
);
}
#[test]
fn touching_extends_the_window_but_cannot_resurrect_a_lapsed_one() {
let clock = TestClock::started_now();
let residency = Residency::idle_bounded(clock.clone(), Duration::from_secs(60));
clock.advance(Duration::from_secs(59));
residency.touch();
clock.advance(Duration::from_secs(59));
assert!(
residency.is_live(),
"118s total, but never 60s of quiet, so the session is still live"
);
clock.advance(Duration::from_secs(60));
assert!(!residency.is_live(), "now it has lapsed");
residency.touch();
assert!(
!residency.is_live(),
"a lapsed window is a relock, and a relock is never undone by touching it"
);
}
#[test]
fn an_unbounded_residency_never_lapses_on_its_own() {
let residency = Residency::new();
assert!(residency.is_live());
residency.touch();
assert!(residency.is_live());
residency.revoke();
assert!(!residency.is_live());
}
#[test]
fn revocation_is_idempotent_and_has_no_way_back() {
let residency = Residency::new();
residency.revoke();
residency.revoke();
assert!(!residency.is_live());
}
}