fix(health): answer 503 when MongoDB does not answer (#60) (#822)
CI / Check (push) Skipped
CI / Agent integration (push) Skipped
CI / Detect Changes (push) Successful in 7s
CI / Deploy Dashboard (push) Skipped
CI / Deploy Docs (push) Skipped
CI / Deploy MCP (push) Skipped
CI / Deploy Agent (push) Canceled after 0s

This commit was merged in pull request #822.
This commit is contained in:
sharang committed 2026-11-04 08:17:15 +00:00
1 parent 7d73893485
commit 2523ecb018
2 files changed
+68 -2

No files matched your search

+54 -2
View File
@@ -13,9 +13,44 @@ pub async fn access() -> Json<serde_json::Value> {
Json(serde_json::json!({ "access": true }))
}
/// How long the health check waits for MongoDB before it reports it down.
const DB_PING_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(2);
/// `GET /api/v1/health`: 200 `{"status":"ok"}` when MongoDB answers a ping,
/// 503 `{"status":"degraded","mongo":"unreachable"}` when it does not, so a
/// load balancer and the container health check stop sending traffic to an
/// instance that cannot serve it (#60). Public, untraced, no tenant.
#[tracing::instrument(skip_all)]
pub async fn health() -> Json<serde_json::Value> {
Json(serde_json::json!({ "status": "ok" }))
pub async fn health(
axum::extract::Extension(agent): AgentExt,
) -> (axum::http::StatusCode, Json<serde_json::Value>) {
let reachable = match tokio::time::timeout(DB_PING_TIMEOUT, agent.db_pool.ping()).await {
Ok(Ok(())) => true,
Ok(Err(e)) => {
tracing::warn!(error = %e, "health: MongoDB ping failed");
false
}
Err(_) => {
tracing::warn!("health: MongoDB ping timed out");
false
}
};
health_answer(reachable)
}
/// The health answer for whether MongoDB answered.
fn health_answer(mongo_reachable: bool) -> (axum::http::StatusCode, Json<serde_json::Value>) {
if mongo_reachable {
(
axum::http::StatusCode::OK,
Json(serde_json::json!({ "status": "ok" })),
)
} else {
(
axum::http::StatusCode::SERVICE_UNAVAILABLE,
Json(serde_json::json!({ "status": "degraded", "mongo": "unreachable" })),
)
}
}
/// Findings that still need someone's attention: not resolved, not a false
@@ -128,3 +163,20 @@ pub async fn stats_overview(
page: None,
}))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_health_answer_follows_the_database() {
let (status, Json(body)) = health_answer(true);
assert_eq!(status, axum::http::StatusCode::OK);
assert_eq!(body, serde_json::json!({ "status": "ok" }));
let (status, Json(body)) = health_answer(false);
assert_eq!(status, axum::http::StatusCode::SERVICE_UNAVAILABLE);
assert_eq!(body["status"], "degraded");
assert_eq!(body["mongo"], "unreachable");
}
}
+14
View File
@@ -168,6 +168,20 @@ impl DatabasePool {
}
}
/// Ask MongoDB whether it answers (`ping` on the `admin` database), for
/// the health check.
///
/// # Errors
///
/// The driver's error when the server cannot be reached or refuses.
pub async fn ping(&self) -> Result<(), mongodb::error::Error> {
self.client
.database("admin")
.run_command(doc! { "ping": 1 })
.await
.map(|_| ())
}
/// Raw client handle. Reserved for cross-tenant admin flows that
/// must opt in explicitly (tenant listing, drop-on-offboard).
pub fn client(&self) -> &Client {