//! K2106–K2110 bounded health checking. At most one probe is outstanding and //! probes are spaced by `interval`; a missed or late answer is a visible miss, //! never a success. Only `threshold` consecutive misses request a restart, and //! restarts are limited by `budget` and separated by `spacing`, so a silent or //! slow service cannot drive a restart storm. A requested restart stops all //! probing until PID1 rebinds the monitor to the restarted generation. #[repr(u64)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Error { Configuration=1, State, Stale, Sequence, Late, Clock } #[repr(u64)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum State { Watching=1, RestartPending, Deferred, Exhausted } #[repr(u64)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Reason { None=0, Unresponsive, Late } #[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 interval:u64, pub timeout:u64, pub threshold:u64, pub budget:u64, pub spacing:u64 } /// Sent to the service. Not Clone: one probe, one sequence number. #[repr(C)] #[derive(Debug,PartialEq,Eq)] pub struct Probe { pub identity:Identity, pub sequence:u64, pub deadline:u64 } /// Built by the trusted channel boundary; `sender` is never read from bytes. #[repr(C)] #[derive(Debug,PartialEq,Eq)] pub struct Answer { pub sender:Identity, pub sequence:u64 } #[derive(Debug,PartialEq,Eq)] pub enum Event { Idle, Issue(Probe), Missed(u64), Restart(Reason), Deferred, Exhausted } #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Status { pub state:State, pub identity:Identity, pub sequence:u64, pub healthy:u64, pub misses:u64, pub missed_total:u64, pub restarts:u64, pub budget:u64, pub reason:Reason, pub restart_reason:Reason, pub last_restart:u64, pub outstanding:u64, pub deadline:u64 } #[repr(C)] #[derive(Debug)] pub struct Monitor { policy:Policy, state:State, identity:Identity, sequence:u64, outstanding:u64, deadline:u64, next:u64, last:u64, healthy:u64, misses:u64, missed_total:u64, restarts:u64, reason:Reason, restart_reason:Reason, last_restart:u64, restarted:bool, late:u64 } impl Policy { pub fn validate(self)->Result{ if self.interval==0||self.timeout==0||self.timeout>self.interval||self.threshold==0||self.threshold>8||self.budget>8||self.spacingResult{ let policy=policy.validate()?;if identity.pid==0||identity.generation==0{return Err(Error::Configuration)} let next=now.checked_add(policy.interval).ok_or(Error::Configuration)?; Ok(Self{policy,state:State::Watching,identity,sequence:0,outstanding:0,deadline:0,next,last:now,healthy:0,misses:0,missed_total:0,restarts:0,reason:Reason::None,restart_reason:Reason::None,last_restart:0,restarted:false,late:0}) } pub fn status(&self)->Status { Status{state:self.state,identity:self.identity,sequence:self.sequence,healthy:self.healthy,misses:self.misses, missed_total:self.missed_total,restarts:self.restarts,budget:self.policy.budget,reason:self.reason,restart_reason:self.restart_reason,last_restart:self.last_restart,outstanding:self.outstanding,deadline:self.deadline} } fn unhealthy(&mut self,now:u64)->Event{ if self.restarts>=self.policy.budget{self.state=State::Exhausted;return Event::Exhausted} if self.restarted&&nowResult{ if nowreturn Ok(Event::Exhausted), State::RestartPending=>return Ok(Event::Idle), State::Deferred=>return Ok(self.unhealthy(now)), State::Watching=>{} } if self.outstanding!=0{ if now=self.policy.threshold{return Ok(self.unhealthy(now))} return Ok(Event::Missed(missed)) } if nowResult{ let fail=if now=self.deadline{Some(Error::Late)}else{None}; if let Some(error)=fail{if error==Error::Late{self.late=self.outstanding}return Err((error,answer))} self.last=now;self.outstanding=0;self.misses=0;self.healthy+=1;self.reason=Reason::None;Ok(self.status()) } /// Only after a requested restart, and only to a different generation. pub fn rebind(&mut self,identity:Identity,now:u64)->Result{ if self.state!=State::RestartPending{return Err(Error::State)} if now