diff --git a/web3_proxy/src/frontend/mod.rs b/web3_proxy/src/frontend/mod.rs index f4005963..e1a55e2f 100644 --- a/web3_proxy/src/frontend/mod.rs +++ b/web3_proxy/src/frontend/mod.rs @@ -30,6 +30,7 @@ pub enum FrontendResponseCaches { // TODO: what should this cache's value be? pub type FrontendResponseCache = Cache, hashbrown::hash_map::DefaultHashBuilder>; +pub type FrontendHealthCache = Cache<(), bool, hashbrown::hash_map::DefaultHashBuilder>; /// Start the frontend server. pub async fn serve(port: u16, proxy_app: Arc) -> anyhow::Result<()> { @@ -37,7 +38,11 @@ pub async fn serve(port: u16, proxy_app: Arc) -> anyhow::Result<() // TODO: a moka cache is probably way overkill for this. // no need for max items. only expire because of time to live let response_cache: FrontendResponseCache = Cache::builder() - .time_to_live(Duration::from_secs(1)) + .time_to_live(Duration::from_secs(2)) + .build_with_hasher(hashbrown::hash_map::DefaultHashBuilder::default()); + + let health_cache: FrontendHealthCache = Cache::builder() + .time_to_live(Duration::from_millis(100)) .build_with_hasher(hashbrown::hash_map::DefaultHashBuilder::default()); // TODO: read config for if fastest/versus should be available publicly. default off @@ -173,6 +178,7 @@ pub async fn serve(port: u16, proxy_app: Arc) -> anyhow::Result<() .layer(Extension(proxy_app.clone())) // frontend caches .layer(Extension(response_cache)) + .layer(Extension(health_cache)) // 404 for any unknown routes .fallback(errors::handler_404); diff --git a/web3_proxy/src/frontend/status.rs b/web3_proxy/src/frontend/status.rs index df7f8bc9..1199dc25 100644 --- a/web3_proxy/src/frontend/status.rs +++ b/web3_proxy/src/frontend/status.rs @@ -3,7 +3,7 @@ //! For ease of development, users can currently access these endponts. //! They will eventually move to another port. -use super::{FrontendResponseCache, FrontendResponseCaches}; +use super::{FrontendHealthCache, FrontendResponseCache, FrontendResponseCaches}; use crate::app::{Web3ProxyApp, APP_USER_AGENT}; use axum::{http::StatusCode, response::IntoResponse, Extension, Json}; use axum_macros::debug_handler; @@ -12,9 +12,15 @@ use std::sync::Arc; /// Health check page for load balancers to use. #[debug_handler] -pub async fn health(Extension(app): Extension>) -> impl IntoResponse { - // TODO: add a check that we aren't shutting down - if app.balanced_rpcs.synced() { +pub async fn health( + Extension(app): Extension>, + Extension(health_cache): Extension, +) -> impl IntoResponse { + let synced = health_cache + .get_with((), async { app.balanced_rpcs.synced() }) + .await; + + if synced { (StatusCode::OK, "OK") } else { (StatusCode::SERVICE_UNAVAILABLE, ":(")