sui_rpc_api/service/
health.rs1use axum::extract::{Query, State};
5use std::time::Duration;
6use std::time::SystemTime;
7
8use crate::Result;
9use crate::RpcService;
10
11impl RpcService {
12 pub fn health_check(&self, threshold_seconds: Option<u32>) -> Result<()> {
20 let summary = self.reader.inner().get_latest_checkpoint()?;
21
22 if let Some(threshold_seconds) = threshold_seconds {
24 let latest_chain_time = summary.timestamp();
25
26 let threshold = SystemTime::now() - Duration::from_secs(threshold_seconds as u64);
27
28 if latest_chain_time < threshold {
29 return Err(anyhow::anyhow!(
30 "The latest checkpoint timestamp is less than the provided threshold"
31 )
32 .into());
33 }
34 }
35
36 Ok(())
37 }
38}
39
40#[derive(Debug, serde::Serialize, serde::Deserialize)]
41pub struct Threshold {
42 pub threshold_seconds: Option<u32>,
48}
49
50pub async fn health(
51 Query(Threshold { threshold_seconds }): Query<Threshold>,
52 State(state): State<RpcService>,
53) -> impl axum::response::IntoResponse {
54 match state.health_check(threshold_seconds) {
55 Ok(()) => (axum::http::StatusCode::OK, "up"),
56 Err(_) => (axum::http::StatusCode::SERVICE_UNAVAILABLE, "down"),
57 }
58}