Skip to content

Commit 0d673e4

Browse files
feat: add is_ready method to Cache trait and update ready endpoint
Co-Authored-By: Tejas Badadare <[email protected]>
1 parent b82542e commit 0d673e4

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

apps/hermes/server/src/api/rest/ready.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use {
2-
crate::{api::ApiState, state::aggregate::Aggregates},
2+
crate::{api::ApiState, state::{aggregate::Aggregates, cache::Cache}},
33
axum::{
44
extract::State,
55
http::StatusCode,
66
response::{IntoResponse, Response},
77
Json,
88
},
9+
serde_json::json,
910
};
1011

1112
/// Endpoint that returns OK (200) only when the cache is fully hydrated.
@@ -19,11 +20,20 @@ use {
1920
/// along with detailed metadata about the readiness state.
2021
pub async fn ready<S>(State(state): State<ApiState<S>>) -> Response
2122
where
22-
S: Aggregates,
23+
S: Aggregates + Cache,
2324
{
2425
let state = &*state.state;
25-
match Aggregates::is_ready(state).await {
26-
(true, _) => (StatusCode::OK, "OK").into_response(),
27-
(false, metadata) => (StatusCode::SERVICE_UNAVAILABLE, Json(metadata)).into_response(),
26+
let (aggregates_ready, metadata) = Aggregates::is_ready(state).await;
27+
let cache_ready = Cache::is_ready(state).await;
28+
29+
if aggregates_ready && cache_ready {
30+
(StatusCode::OK, "OK").into_response()
31+
} else {
32+
let response_metadata = json!({
33+
"aggregates_ready": aggregates_ready,
34+
"cache_ready": cache_ready,
35+
"details": metadata
36+
});
37+
(StatusCode::SERVICE_UNAVAILABLE, Json(response_metadata)).into_response()
2838
}
2939
}

apps/hermes/server/src/state/cache.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ pub trait Cache {
136136
request_time: RequestTime,
137137
filter: MessageStateFilter,
138138
) -> Result<Vec<MessageState>>;
139+
///
140+
async fn is_ready(&self) -> bool;
139141
}
140142

141143
#[async_trait::async_trait]
@@ -274,6 +276,12 @@ where
274276
let cache = self.into().wormhole_merkle_state_cache.read().await;
275277
Ok(cache.get(&slot).cloned())
276278
}
279+
280+
///
281+
async fn is_ready(&self) -> bool {
282+
let message_cache = self.into().message_cache.read().await;
283+
!message_cache.is_empty()
284+
}
277285
}
278286

279287
async fn retrieve_message_state(

0 commit comments

Comments
 (0)