[edge] Fixes: skip 1xx, enforce auth timeout, resolve upstreams per connect, all Upgrade tokens, strip Remote-*
This commit is contained in:
+61
-23
@@ -35,25 +35,40 @@ async fn probe(connector: &Connector, u: &UpstreamRt, path: &str) -> bool {
|
||||
{
|
||||
return false;
|
||||
}
|
||||
match tokio::time::timeout(TIMEOUT, fetch(connector, u, req, 64 * 1024, TIMEOUT)).await {
|
||||
Ok(Ok(f)) => f.header.status.is_success(),
|
||||
_ => false,
|
||||
match fetch(connector, u, req, 64 * 1024, TIMEOUT).await {
|
||||
Ok(f) => f.header.status.is_success(),
|
||||
Err(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// One round over a host's upstreams. Each probe runs in its own task, so a panic inside a
|
||||
/// probe (e.g. a library bug on an odd answer) marks that upstream unhealthy instead of
|
||||
/// ending health checks.
|
||||
async fn round(host: &Arc<HostRt>, path: &str, connector: &Arc<Connector>) {
|
||||
for i in 0..host.upstreams.len() {
|
||||
let (h, p, c) = (host.clone(), path.to_owned(), connector.clone());
|
||||
let ok = match tokio::spawn(async move { probe(&c, &h.upstreams[i], &p).await }).await {
|
||||
Ok(ok) => ok,
|
||||
Err(e) => {
|
||||
log::error!(target: "ianitor::health", "{}: health check of {} crashed ({e}); counting it as failed", host.cfg.hostname, host.upstreams[i].authority);
|
||||
false
|
||||
}
|
||||
};
|
||||
let u = &host.upstreams[i];
|
||||
if u.healthy.swap(ok, Ordering::Relaxed) != ok {
|
||||
if ok {
|
||||
log::info!(target: "ianitor::health", "{}: upstream {} is healthy again", host.cfg.hostname, u.authority);
|
||||
} else {
|
||||
log::warn!(target: "ianitor::health", "{}: upstream {} failed its health check", host.cfg.hostname, u.authority);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn watch(host: Arc<HostRt>, path: String, every: Duration) {
|
||||
let connector = Connector::new(None);
|
||||
let connector = Arc::new(Connector::new(None));
|
||||
loop {
|
||||
for u in &host.upstreams {
|
||||
let ok = probe(&connector, u, &path).await;
|
||||
if u.healthy.swap(ok, Ordering::Relaxed) != ok {
|
||||
if ok {
|
||||
log::info!(target: "ianitor::health", "{}: upstream {} is healthy again", host.cfg.hostname, u.authority);
|
||||
} else {
|
||||
log::warn!(target: "ianitor::health", "{}: upstream {} failed its health check", host.cfg.hostname, u.authority);
|
||||
}
|
||||
}
|
||||
}
|
||||
round(&host, &path, &connector).await;
|
||||
tokio::time::sleep(every).await;
|
||||
}
|
||||
}
|
||||
@@ -61,16 +76,39 @@ async fn watch(host: Arc<HostRt>, path: String, every: Duration) {
|
||||
#[async_trait]
|
||||
impl BackgroundService for HealthChecks {
|
||||
async fn start(&self, mut shutdown: ShutdownWatch) {
|
||||
let mut tasks = Vec::new();
|
||||
for host in self.rt.hosts.values() {
|
||||
if let Some(hc) = &host.cfg.health_check {
|
||||
let every = Duration::from_secs(u64::from(hc.interval_secs.max(1)));
|
||||
tasks.push(tokio::spawn(watch(host.clone(), hc.path.clone(), every)));
|
||||
let checked: Vec<(Arc<HostRt>, String, Duration)> = self
|
||||
.rt
|
||||
.hosts
|
||||
.values()
|
||||
.filter_map(|h| {
|
||||
h.cfg.health_check.as_ref().map(|hc| {
|
||||
(
|
||||
h.clone(),
|
||||
hc.path.clone(),
|
||||
Duration::from_secs(u64::from(hc.interval_secs.max(1))),
|
||||
)
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let mut tasks = tokio::task::JoinSet::new();
|
||||
let mut which = std::collections::HashMap::new();
|
||||
for (i, (h, p, every)) in checked.iter().enumerate() {
|
||||
which.insert(tasks.spawn(watch(h.clone(), p.clone(), *every)).id(), i);
|
||||
}
|
||||
// A watcher never returns; if one ends anyway, say so and start it again.
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = shutdown.changed() => break,
|
||||
Some(done) = tasks.join_next_with_id() => {
|
||||
let id = match &done { Ok((id, ())) => *id, Err(e) => e.id() };
|
||||
if let Some(i) = which.remove(&id) {
|
||||
let (h, p, every) = &checked[i];
|
||||
log::error!(target: "ianitor::health", "{}: health checker stopped; restarting it", h.cfg.hostname);
|
||||
which.insert(tasks.spawn(watch(h.clone(), p.clone(), *every)).id(), i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let _ = shutdown.changed().await;
|
||||
for t in tasks {
|
||||
t.abort();
|
||||
}
|
||||
tasks.abort_all();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user