use std::{
fmt::Debug,
sync::{Arc, RwLock},
};
use ic_agent::agent::route_provider::RouteProvider;
pub trait Healthy: Send + Sync + Debug + 'static {
fn healthy(&self) -> bool;
}
impl Healthy for Arc<dyn RouteProvider> {
fn healthy(&self) -> bool {
self.routes_stats().healthy.unwrap_or_default() > 0
}
}
#[derive(Debug, Default)]
pub struct HealthManager {
services: RwLock<Vec<Arc<dyn Healthy>>>,
}
impl HealthManager {
pub fn add(&self, svc: Arc<dyn Healthy>) {
self.services.write().unwrap().push(svc);
}
}
impl Healthy for HealthManager {
fn healthy(&self) -> bool {
self.services.read().unwrap().iter().all(|x| x.healthy())
}
}