gitdataai/libs/gingress-proxy/src/health_checker.rs
ZhenYi 4e2a39a5c0 fix(workspace): resolve all cargo check warnings across workspace
Remove unused imports and add #[allow(dead_code)] annotations to
intentionally retained fields/methods. Also add deploy/.server.yaml
to .gitignore to prevent accidental credential exposure.
2026-05-10 21:56:08 +08:00

74 lines
2.5 KiB
Rust

//! 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<RwLock<Vec<Endpoint>>>,
/// 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<Endpoint>, 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<Endpoint>) {
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<Self>) -> 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;
}
}