use crate::id::types::ChildId;
use serde::{Deserialize, Serialize};
use std::time::{Duration, Instant, SystemTime};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct HealthPolicy {
pub heartbeat_interval: Duration,
pub stale_after: Duration,
}
impl HealthPolicy {
pub fn new(heartbeat_interval: Duration, stale_after: Duration) -> Self {
Self {
heartbeat_interval,
stale_after,
}
}
pub fn is_stale(&self, heartbeat: &Heartbeat, now: Instant) -> bool {
now.duration_since(heartbeat.monotonic_at) > self.stale_after
}
}
#[derive(Debug, Clone)]
pub struct Heartbeat {
pub child_id: ChildId,
pub monotonic_at: Instant,
pub recorded_at: SystemTime,
pub detail: Option<String>,
}
impl Heartbeat {
pub fn new(child_id: ChildId, monotonic_at: Instant, detail: Option<String>) -> Self {
Self {
child_id,
monotonic_at,
recorded_at: SystemTime::now(),
detail,
}
}
pub fn age_at(&self, now: Instant) -> Duration {
now.duration_since(self.monotonic_at)
}
}