cache /health and cache /status for longer

This commit is contained in:
Bryan Stitt 2023-02-02 13:58:04 -08:00
parent 671105fe07
commit 9ac3ef1e3d
2 changed files with 17 additions and 5 deletions

@ -30,6 +30,7 @@ pub enum FrontendResponseCaches {
// TODO: what should this cache's value be?
pub type FrontendResponseCache =
Cache<FrontendResponseCaches, Arc<serde_json::Value>, 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<Web3ProxyApp>) -> anyhow::Result<()> {
@ -37,7 +38,11 @@ pub async fn serve(port: u16, proxy_app: Arc<Web3ProxyApp>) -> 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<Web3ProxyApp>) -> 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);

@ -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<Arc<Web3ProxyApp>>) -> impl IntoResponse {
// TODO: add a check that we aren't shutting down
if app.balanced_rpcs.synced() {
pub async fn health(
Extension(app): Extension<Arc<Web3ProxyApp>>,
Extension(health_cache): Extension<FrontendHealthCache>,
) -> impl IntoResponse {
let synced = health_cache
.get_with((), async { app.balanced_rpcs.synced() })
.await;
if synced {
(StatusCode::OK, "OK")
} else {
(StatusCode::SERVICE_UNAVAILABLE, ":(")