objectstore_server/endpoints/health.rs
1//! Health and readiness endpoints.
2//!
3//! Exposes `/health` (liveness) and `/ready` (readiness). The readiness
4//! endpoint fails when a [`SHUTDOWN_MARKER_PATH`] file is present, allowing
5//! graceful draining before process termination.
6
7use axum::http::StatusCode;
8use axum::response::IntoResponse;
9use axum::{Router, routing};
10
11use crate::state::ServiceState;
12
13/// Filesystem path whose presence signals that the server is shutting down.
14///
15/// When this file exists, the `/ready` endpoint returns `503 Service Unavailable`,
16/// causing load balancers to stop routing new requests to this instance. Create
17/// this file before terminating the process to drain in-flight requests gracefully.
18pub const SHUTDOWN_MARKER_PATH: &str = "/tmp/objectstore.down";
19
20/// Returns a router with the `/health` and `/ready` endpoints.
21pub fn router() -> Router<ServiceState> {
22 Router::new()
23 .route("/health", routing::get(health))
24 .route("/ready", routing::get(ready))
25}
26
27async fn health() -> impl IntoResponse {
28 "OK"
29}
30
31async fn ready() -> impl IntoResponse {
32 if tokio::fs::try_exists(SHUTDOWN_MARKER_PATH)
33 .await
34 .unwrap_or(false)
35 {
36 tracing::debug!("Shutdown marker exists, failing readiness");
37 (StatusCode::SERVICE_UNAVAILABLE, "Shutting down")
38 } else {
39 (StatusCode::OK, "OK")
40 }
41}