//! Active and passive health checks for upstream endpoints. //! //! - Active: periodically probes upstream `/health` endpoints. //! - Passive: tracks request failure rates and marks unhealthy endpoints. use crate::config::Endpoint; use std::sync::Arc; use tokio::sync::RwLock; /// Health checker for a pool of upstream endpoints. pub struct HealthChecker { endpoints: Arc>>, /// How often to run active health probes. #[allow(dead_code)] interval: std::time::Duration, /// Failure threshold for passive health checks. #[allow(dead_code)] passive_fail_threshold: u32, /// Success threshold for recovery. #[allow(dead_code)] passive_success_threshold: u32, } impl HealthChecker { /// Create a new health checker. pub fn new(endpoints: Vec, interval: std::time::Duration) -> Self { Self { endpoints: Arc::new(RwLock::new(endpoints)), interval, passive_fail_threshold: 3, passive_success_threshold: 2, } } /// Update the endpoint pool. pub async fn update_endpoints(&self, new_endpoints: Vec) { let mut eps = self.endpoints.write().await; *eps = new_endpoints; } /// Run active health probes in a background task. pub fn spawn_active_probes(self: Arc) -> tokio::task::JoinHandle<()> { tokio::spawn(async move { // Active probing loop would go here. // For now a placeholder that keeps endpoints as-is. loop { tokio::time::sleep(std::time::Duration::from_secs(10)).await; } }) } /// Mark an endpoint as failing (passive check). /// Returns true if this endpoint should now be considered unhealthy. #[allow(dead_code)] pub async fn record_failure(&self, endpoint_ip: &str) -> bool { let mut eps = self.endpoints.write().await; if let Some(ep) = eps.iter_mut().find(|e| e.ip == endpoint_ip) { // Track failure count via metadata (simplified: uses ready field) // Full impl would add failure_count field to Endpoint let _ = ep; return false; // Placeholder } false } /// Mark an endpoint as successful (passive check). #[allow(dead_code)] pub async fn record_success(&self, endpoint_ip: &str) { let _eps = self.endpoints.write().await; // Reset failure count for the endpoint let _ = endpoint_ip; } }