mz_environmentd/http/
probe.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apach
9
10//! Health check HTTP endpoints.
11
12use axum::Extension;
13use axum::extract::Query;
14use axum::response::IntoResponse;
15use futures::FutureExt;
16use http::StatusCode;
17use serde::Deserialize;
18
19use crate::http::Delayed;
20
21/// Query parameters for [`handle_ready`].
22#[derive(Deserialize)]
23pub struct ReadyParams {
24    /// Whether to wait for readiness or to return immediately if unready.
25    #[serde(default)]
26    wait: bool,
27}
28
29/// Handles a readiness probe.
30pub async fn handle_ready(
31    Extension(client): Extension<Delayed<mz_adapter::Client>>,
32    query: Query<ReadyParams>,
33) -> impl IntoResponse {
34    // `environmentd` is ready to serve queries when the adapter client is
35    // available.
36    let is_ready = if query.wait {
37        let _ = client.await;
38        true
39    } else {
40        client.now_or_never().is_some()
41    };
42    match is_ready {
43        false => (StatusCode::SERVICE_UNAVAILABLE, "not ready"),
44        true => (StatusCode::OK, "ready"),
45    }
46}