Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions pgdog/src/healthcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use hyper_util::rt::TokioIo;
use tokio::net::TcpListener;
use tracing::info;

use crate::backend::databases::databases;
use crate::backend::databases::{databases, Databases};

pub async fn server(port: u16) -> std::io::Result<()> {
info!("healthcheck endpoint http://0.0.0.0:{}", port);
Expand All @@ -36,17 +36,7 @@ async fn healthcheck(
_: Request<hyper::body::Incoming>,
) -> Result<Response<Full<Bytes>>, Infallible> {
let databases = databases();
let broken = databases.all().iter().all(|(_, cluster)| {
let pools = cluster
.shards()
.iter()
.map(|shard| shard.pools())
.collect::<Vec<_>>()
.into_iter()
.flatten()
.collect::<Vec<_>>();
pools.iter().all(|p| !p.healthy())
});
let broken = broken(&databases);

let response = if broken { "down" } else { "up" };
let status = if broken { 502 } else { 200 };
Expand All @@ -59,3 +49,24 @@ async fn healthcheck(

Ok(response)
}

fn broken(databases: &Databases) -> bool {
let mut pools = databases
.all()
.values()
.flat_map(|cluster| cluster.shards())
.flat_map(|shard| shard.pools())
.peekable();

pools.peek().is_some() && pools.all(|pool| !pool.healthy())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn no_pools_is_healthy() {
assert!(!broken(&Databases::default()));
}
}
Loading