Skip to content
Closed
Show file tree
Hide file tree
Changes from 7 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
32 changes: 27 additions & 5 deletions apps/hermes/server/src/api/rest/ready.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
use {
crate::{api::ApiState, state::aggregate::Aggregates},
crate::{
api::ApiState,
state::{aggregate::Aggregates, cache::Cache},
},
axum::{
extract::State,
http::StatusCode,
response::{IntoResponse, Response},
Json,
},
serde_json::json,
};

/// Endpoint that returns OK (200) only when the cache is fully hydrated.
///
/// The cache is considered fully hydrated when all of the following conditions are met:
/// - `has_completed_recently`: The latest completed update is recent (within the staleness threshold)
/// - `is_not_behind`: The latest completed slot isn't too far behind the latest observed slot
/// - `is_metadata_loaded`: Price feeds metadata is not empty
///
/// If any of these conditions are not met, the endpoint returns SERVICE_UNAVAILABLE (503)
/// along with detailed metadata about the readiness state.
pub async fn ready<S>(State(state): State<ApiState<S>>) -> Response
where
S: Aggregates,
S: Aggregates + Cache,
{
let state = &*state.state;
match Aggregates::is_ready(state).await {
(true, _) => (StatusCode::OK, "OK").into_response(),
(false, metadata) => (StatusCode::SERVICE_UNAVAILABLE, Json(metadata)).into_response(),
let (aggregates_ready, metadata) = Aggregates::is_ready(state).await;
let cache_ready = Cache::is_cache_ready(state).await;

if aggregates_ready && cache_ready {
(StatusCode::OK, "OK").into_response()
} else {
let response_metadata = json!({
"aggregates_ready": aggregates_ready,
"cache_ready": cache_ready,
"details": metadata
});
(StatusCode::SERVICE_UNAVAILABLE, Json(response_metadata)).into_response()
}
}
9 changes: 9 additions & 0 deletions apps/hermes/server/src/state/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pub trait Cache {
request_time: RequestTime,
filter: MessageStateFilter,
) -> Result<Vec<MessageState>>;
async fn is_cache_ready(&self) -> bool;
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -274,6 +275,14 @@ where
let cache = self.into().wormhole_merkle_state_cache.read().await;
Ok(cache.get(&slot).cloned())
}

///
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you forgot to finish this docstring

async fn is_cache_ready(&self) -> bool {
let message_cache = self.into().message_cache.read().await;
let cache_size = self.into().cache_size as usize;

message_cache.len() >= (cache_size * 9 / 10)
}
}

async fn retrieve_message_state(
Expand Down
Loading