use crate::route::SharedTable;
use crate::route::{Admission, Upstream};
use std::time::Duration;
use tokio::task::JoinSet;
pub async fn health_loop(table: SharedTable, interval: Duration) {
let client = match reqwest::Client::builder()
.timeout(Duration::from_secs(2))
.redirect(reqwest::redirect::Policy::none())
.build()
{
Ok(c) => c,
Err(e) => {
tracing::error!(?e, "failed to build health-check reqwest client");
return;
}
};
let mut ticker = tokio::time::interval(interval);
ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
loop {
ticker.tick().await;
let current = table.load_full();
current.publish_gauges();
let mut probes = JoinSet::new();
for up in current.upstreams() {
let client = client.clone();
let up = up.clone();
probes.spawn(async move { probe_one(&client, &up).await });
}
while probes.join_next().await.is_some() {}
}
}
async fn probe_one(client: &reqwest::Client, up: &Upstream) {
let url = health_url(&up.uri);
let status = client.get(&url).send().await.map(|r| r.status());
if is_healthy(status.as_ref().ok()) {
up.record_success(Admission::Probe);
} else {
up.record_failure(Admission::Probe);
}
}
fn is_healthy(status: Option<&reqwest::StatusCode>) -> bool {
status.is_some_and(|s| !s.is_server_error())
}
fn health_url(uri: &http::Uri) -> String {
let scheme = uri.scheme_str().unwrap_or("http");
let authority = uri.authority().map(|a| a.as_str()).unwrap_or("");
format!("{scheme}://{authority}/health")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn health_status_mapping() {
use reqwest::StatusCode as S;
assert!(is_healthy(Some(&S::OK)));
assert!(is_healthy(Some(&S::NOT_FOUND)));
assert!(!is_healthy(Some(&S::SERVICE_UNAVAILABLE)));
assert!(!is_healthy(None));
}
#[test]
fn health_url_has_no_double_slash() {
let uri: http::Uri = "http://localhost:8001".parse().unwrap();
assert_eq!(health_url(&uri), "http://localhost:8001/health");
}
#[test]
fn health_url_ignores_original_path() {
let uri: http::Uri = "http://localhost:8001/".parse().unwrap();
assert_eq!(health_url(&uri), "http://localhost:8001/health");
}
}