use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::time::{Duration, Instant};
use super::key::LookupKey;
use super::{InteractiveFetchGate, NoLiveList};
use crate::cache_policy::evict_expired_then_oldest_at;
pub(super) const INTERACTIVE_FAILURE_TTL: Duration = Duration::from_secs(30);
pub(super) const INTERACTIVE_MEMO_CAP: usize = 1024;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum RecordState {
SeededFromOutcome,
InteractiveFailed,
InteractiveSucceeded,
}
#[derive(Debug)]
pub(super) struct InteractiveRecord {
state: RecordState,
recorded_at: Instant,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum FastFail {
Seeded,
Memo,
Route,
}
impl FastFail {
pub(super) const fn as_str(self) -> &'static str {
match self {
Self::Seeded => "seeded from background outcome",
Self::Memo => "interactive failure memo",
Self::Route => "interactive failure of the package's version list",
}
}
}
#[derive(Debug)]
pub(super) enum Decision {
Live,
Degrade(NoLiveList, FastFail),
}
impl InteractiveFetchGate {
pub(super) fn decide(
&self,
lookup: &Arc<LookupKey>,
background_failure_known: bool,
generation: u64,
now: Instant,
) -> Decision {
let own = self.decide_slot(lookup, background_failure_known, generation, now);
if let Decision::Degrade(..) = own {
return own;
}
let Some(honored) = lookup.kind().memo_policy().honors else {
return own;
};
if self.interactive_failure_active(&lookup.in_slot(honored), now) {
Decision::Degrade(NoLiveList::Unverified, FastFail::Route)
} else {
own
}
}
fn interactive_failure_active(&self, key: &LookupKey, now: Instant) -> bool {
self.memo.get(key).is_some_and(|record| {
record.state == RecordState::InteractiveFailed
&& now.saturating_duration_since(record.recorded_at) < INTERACTIVE_FAILURE_TTL
})
}
fn decide_slot(
&self,
lookup: &Arc<LookupKey>,
background_failure_known: bool,
generation: u64,
now: Instant,
) -> Decision {
let existing = self
.memo
.get(&**lookup)
.map(|record| (record.state, record.recorded_at));
match existing {
Some((state, recorded_at))
if now.saturating_duration_since(recorded_at) < INTERACTIVE_FAILURE_TTL =>
{
match state {
RecordState::SeededFromOutcome if background_failure_known => {
Decision::Degrade(NoLiveList::Unverified, FastFail::Seeded)
}
RecordState::SeededFromOutcome => Decision::Live,
RecordState::InteractiveFailed => {
Decision::Degrade(NoLiveList::Unverified, FastFail::Memo)
}
RecordState::InteractiveSucceeded => Decision::Live,
}
}
Some(_) => Decision::Live,
None if background_failure_known => {
self.record(lookup, RecordState::SeededFromOutcome, generation);
Decision::Degrade(NoLiveList::Unverified, FastFail::Seeded)
}
None => Decision::Live,
}
}
fn insert(&self, key: &Arc<LookupKey>, state: RecordState, now: Instant) {
evict_expired_then_oldest_at(
&self.memo,
INTERACTIVE_MEMO_CAP,
now,
|record| record.recorded_at,
|_| INTERACTIVE_FAILURE_TTL,
);
self.memo.insert(
Arc::clone(key),
InteractiveRecord {
state,
recorded_at: now,
},
);
}
pub(super) fn record(&self, key: &Arc<LookupKey>, state: RecordState, generation: u64) {
self.insert(key, state, super::now());
if self.generation.load(Ordering::SeqCst) != generation {
self.memo.remove(&**key);
}
}
pub(super) fn forget_record(&self, key: &LookupKey) {
if self.memo.contains_key(key) {
self.memo.remove(key);
}
}
}