cache /health and cache /status for longer
This commit is contained in:
parent
671105fe07
commit
9ac3ef1e3d
@ -30,6 +30,7 @@ pub enum FrontendResponseCaches {
|
|||||||
// TODO: what should this cache's value be?
|
// TODO: what should this cache's value be?
|
||||||
pub type FrontendResponseCache =
|
pub type FrontendResponseCache =
|
||||||
Cache<FrontendResponseCaches, Arc<serde_json::Value>, hashbrown::hash_map::DefaultHashBuilder>;
|
Cache<FrontendResponseCaches, Arc<serde_json::Value>, hashbrown::hash_map::DefaultHashBuilder>;
|
||||||
|
pub type FrontendHealthCache = Cache<(), bool, hashbrown::hash_map::DefaultHashBuilder>;
|
||||||
|
|
||||||
/// Start the frontend server.
|
/// Start the frontend server.
|
||||||
pub async fn serve(port: u16, proxy_app: Arc<Web3ProxyApp>) -> anyhow::Result<()> {
|
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.
|
// TODO: a moka cache is probably way overkill for this.
|
||||||
// no need for max items. only expire because of time to live
|
// no need for max items. only expire because of time to live
|
||||||
let response_cache: FrontendResponseCache = Cache::builder()
|
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());
|
.build_with_hasher(hashbrown::hash_map::DefaultHashBuilder::default());
|
||||||
|
|
||||||
// TODO: read config for if fastest/versus should be available publicly. default off
|
// 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()))
|
.layer(Extension(proxy_app.clone()))
|
||||||
// frontend caches
|
// frontend caches
|
||||||
.layer(Extension(response_cache))
|
.layer(Extension(response_cache))
|
||||||
|
.layer(Extension(health_cache))
|
||||||
// 404 for any unknown routes
|
// 404 for any unknown routes
|
||||||
.fallback(errors::handler_404);
|
.fallback(errors::handler_404);
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
//! For ease of development, users can currently access these endponts.
|
//! For ease of development, users can currently access these endponts.
|
||||||
//! They will eventually move to another port.
|
//! They will eventually move to another port.
|
||||||
|
|
||||||
use super::{FrontendResponseCache, FrontendResponseCaches};
|
use super::{FrontendHealthCache, FrontendResponseCache, FrontendResponseCaches};
|
||||||
use crate::app::{Web3ProxyApp, APP_USER_AGENT};
|
use crate::app::{Web3ProxyApp, APP_USER_AGENT};
|
||||||
use axum::{http::StatusCode, response::IntoResponse, Extension, Json};
|
use axum::{http::StatusCode, response::IntoResponse, Extension, Json};
|
||||||
use axum_macros::debug_handler;
|
use axum_macros::debug_handler;
|
||||||
@ -12,9 +12,15 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
/// Health check page for load balancers to use.
|
/// Health check page for load balancers to use.
|
||||||
#[debug_handler]
|
#[debug_handler]
|
||||||
pub async fn health(Extension(app): Extension<Arc<Web3ProxyApp>>) -> impl IntoResponse {
|
pub async fn health(
|
||||||
// TODO: add a check that we aren't shutting down
|
Extension(app): Extension<Arc<Web3ProxyApp>>,
|
||||||
if app.balanced_rpcs.synced() {
|
Extension(health_cache): Extension<FrontendHealthCache>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
let synced = health_cache
|
||||||
|
.get_with((), async { app.balanced_rpcs.synced() })
|
||||||
|
.await;
|
||||||
|
|
||||||
|
if synced {
|
||||||
(StatusCode::OK, "OK")
|
(StatusCode::OK, "OK")
|
||||||
} else {
|
} else {
|
||||||
(StatusCode::SERVICE_UNAVAILABLE, ":(")
|
(StatusCode::SERVICE_UNAVAILABLE, ":(")
|
||||||
|
Loading…
Reference in New Issue
Block a user