From 9d381dd8a21939dfded6c6f1f92d0cfe358a7a2f Mon Sep 17 00:00:00 2001 From: Thomas Chataigner Date: Fri, 13 Sep 2024 15:35:52 +0200 Subject: [PATCH] refactor: liveness & readiness --- aptos/proof-server/src/bin/proof_server.rs | 23 +++++++++++-------- .../templates/proof-server-deployment.yaml | 6 +++++ ethereum/light-client/src/bin/proof_server.rs | 5 ++++ kadena/light-client/src/bin/proof_server.rs | 6 +++++ 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/aptos/proof-server/src/bin/proof_server.rs b/aptos/proof-server/src/bin/proof_server.rs index 67898a77..059e97c1 100644 --- a/aptos/proof-server/src/bin/proof_server.rs +++ b/aptos/proof-server/src/bin/proof_server.rs @@ -109,6 +109,7 @@ async fn main() -> Result<()> { let app = Router::new() .route("/health", get(health_check)) + .route("/ready", get(ready_check)) .route("/inclusion/proof", post(inclusion_proof)) .route("/epoch/proof", post(epoch_proof)) .route("/epoch/verify", post(epoch_verify)) @@ -128,6 +129,19 @@ async fn main() -> Result<()> { Ok(()) } +async fn health_check(State(state): State) -> impl IntoResponse { + StatusCode::OK +} + +async fn ready_check(State(state): State) -> impl IntoResponse { + let active_requests = state.active_requests.load(Ordering::SeqCst); + if active_requests > 0 { + StatusCode::CONFLICT + } else { + StatusCode::OK + } +} + async fn inclusion_proof( State(state): State, request: axum::extract::Request, @@ -403,15 +417,6 @@ async fn forward_request( Ok(res_bytes.to_vec()) } -async fn health_check(State(state): State) -> impl IntoResponse { - let active_requests = state.active_requests.load(Ordering::SeqCst); - if active_requests > 0 { - StatusCode::CONFLICT - } else { - StatusCode::OK - } -} - async fn count_requests_middleware( State(state): State, req: axum::http::Request, diff --git a/docker/proof-server-chart/templates/proof-server-deployment.yaml b/docker/proof-server-chart/templates/proof-server-deployment.yaml index 4f793c25..01d49d65 100644 --- a/docker/proof-server-chart/templates/proof-server-deployment.yaml +++ b/docker/proof-server-chart/templates/proof-server-deployment.yaml @@ -43,6 +43,12 @@ spec: port: 8080 initialDelaySeconds: 10 periodSeconds: 5 + readinessProbe: + httpGet: + path: /ready + port: 8080 + initialDelaySeconds: 15 + periodSeconds: 10 name: proof-server ports: - containerPort: 8080 diff --git a/ethereum/light-client/src/bin/proof_server.rs b/ethereum/light-client/src/bin/proof_server.rs index 51c4840f..ac7640e9 100644 --- a/ethereum/light-client/src/bin/proof_server.rs +++ b/ethereum/light-client/src/bin/proof_server.rs @@ -81,6 +81,7 @@ async fn main() -> Result<()> { let app = Router::new() .route("/health", get(health_check)) + .route("/ready", get(ready_check)) .route("/inclusion/proof", post(inclusion_proof)) .route("/committee/proof", post(committee_proof)) .route("/committee/verify", post(committee_verify)) @@ -100,6 +101,10 @@ async fn main() -> Result<()> { } async fn health_check(State(state): State) -> impl IntoResponse { + StatusCode::OK +} + +async fn ready_check(State(state): State) -> impl IntoResponse { let active_requests = state.active_requests.load(Ordering::SeqCst); if active_requests > 0 { StatusCode::CONFLICT diff --git a/kadena/light-client/src/bin/proof_server.rs b/kadena/light-client/src/bin/proof_server.rs index 7bbf3911..6c16b5eb 100644 --- a/kadena/light-client/src/bin/proof_server.rs +++ b/kadena/light-client/src/bin/proof_server.rs @@ -65,6 +65,7 @@ async fn main() -> Result<()> { let app = Router::new() .route("/health", get(health_check)) + .route("/ready", get(ready_check)) .layer(axum::middleware::from_fn_with_state( state.clone(), count_requests_middleware, @@ -80,6 +81,10 @@ async fn main() -> Result<()> { } async fn health_check(State(state): State) -> impl IntoResponse { + StatusCode::OK +} + +async fn ready_check(State(state): State) -> impl IntoResponse { let active_requests = state.active_requests.load(Ordering::SeqCst); if active_requests > 0 { StatusCode::CONFLICT @@ -88,6 +93,7 @@ async fn health_check(State(state): State) -> impl IntoResponse { } } + #[allow(dead_code)] async fn forward_request(request_bytes: &[u8], snd_addr: &str) -> Result, StatusCode> { info!("Connecting to the secondary server");