//! K2116–K2120 crash-loop quarantine. PID1 restarts a crashed service only //! while fewer than `limit` crashes fall inside the sliding `window`; the //! crash that reaches the limit quarantines the service instead. A //! quarantined service holds no running lease and is never restarted //! automatically; the quarantine stays visible until an explicit operator //! recovery, which clears the crash history and permits one fresh start. //! Crash and health reports carry the trusted reporter identity; reports from //! an older generation are refused without any state change. pub const MAX_LIMIT:usize=8; #[repr(u64)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Error { Configuration=1, State, Stale, Clock, Generation } #[repr(u64)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Phase { Empty=0, Running, Restarting, Quarantined } #[repr(u64)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Decision { Restart=1, Quarantine } #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Identity { pub pid:u64, pub generation:u64 } #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Policy { pub limit:u64, pub window:u64 } #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Status { pub phase:Phase, pub identity:Identity, pub leases:u64, pub crashes_in_window:u64, pub crashes_total:u64, pub restarts:u64, pub quarantined_at:u64, pub recoveries:u64, pub health_replies:u64, pub stale_refusals:u64 } #[repr(C)] #[derive(Debug)] pub struct Guard { policy:Policy, phase:Phase, identity:Identity, last:u64, crashes:[u64;MAX_LIMIT], count:u64, total:u64, restarts:u64, quarantined_at:u64, recoveries:u64, health:u64, stale:u64 } const NONE:Identity=Identity{pid:0,generation:0}; impl Guard { pub fn new(policy:Policy)->Result{ if policy.limit<2||policy.limit>MAX_LIMIT as u64||policy.window==0{return Err(Error::Configuration)} Ok(Self{policy,phase:Phase::Empty,identity:NONE,last:0,crashes:[0;MAX_LIMIT],count:0,total:0,restarts:0,quarantined_at:0,recoveries:0,health:0,stale:0}) } pub fn status(&self)->Status { Status{phase:self.phase,identity:self.identity,leases:u64::from(self.phase==Phase::Running),crashes_in_window:self.count, crashes_total:self.total,restarts:self.restarts,quarantined_at:self.quarantined_at,recoveries:self.recoveries,health_replies:self.health,stale_refusals:self.stale} } /// A start binds a running generation: the first start, a restart that a /// crash requested, or the one fresh start after operator recovery. pub fn start(&mut self,identity:Identity,now:u64)->Result{ if self.phase!=Phase::Empty&&self.phase!=Phase::Restarting{return Err(Error::State)} if nowResult{ if self.phase!=Phase::Running{return Err(Error::State)} if reporter!=self.identity{self.stale+=1;return Err(Error::Stale)} if now=self.policy.limit{self.phase=Phase::Quarantined;self.quarantined_at=now;Ok(Decision::Quarantine)} else{self.phase=Phase::Restarting;Ok(Decision::Restart)} } /// A health reply counts only from the running generation. pub fn health(&mut self,reporter:Identity)->Result{ if self.phase!=Phase::Running{return Err(Error::State)} if reporter!=self.identity{self.stale+=1;return Err(Error::Stale)} self.health+=1;Ok(self.status()) } /// Explicit operator recovery: the only way out of quarantine. pub fn recover(&mut self,now:u64)->Result{ if self.phase!=Phase::Quarantined{return Err(Error::State)} if now