remove caching the /status things
This commit is contained in:
parent
dbda653812
commit
c5827b6f37
@ -17,14 +17,12 @@ use axum::{
|
|||||||
routing::{get, post},
|
routing::{get, post},
|
||||||
BoxError, Extension, Router,
|
BoxError, Extension, Router,
|
||||||
};
|
};
|
||||||
use http::{header::AUTHORIZATION, Request, StatusCode};
|
use http::{header::AUTHORIZATION, Request};
|
||||||
use hyper::Body;
|
use hyper::Body;
|
||||||
use listenfd::ListenFd;
|
use listenfd::ListenFd;
|
||||||
use moka::future::{Cache, CacheBuilder};
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::{iter::once, time::Duration};
|
use std::{iter::once, time::Duration};
|
||||||
use std::{net::SocketAddr, sync::atomic::Ordering};
|
use std::{net::SocketAddr, sync::atomic::Ordering};
|
||||||
use strum::{EnumCount, EnumIter};
|
|
||||||
use tokio::sync::broadcast;
|
use tokio::sync::broadcast;
|
||||||
use tower::timeout::TimeoutLayer;
|
use tower::timeout::TimeoutLayer;
|
||||||
use tower::ServiceBuilder;
|
use tower::ServiceBuilder;
|
||||||
@ -33,32 +31,12 @@ use tower_http::{cors::CorsLayer, normalize_path::NormalizePathLayer, trace::Tra
|
|||||||
use tracing::{error_span, info};
|
use tracing::{error_span, info};
|
||||||
use ulid::Ulid;
|
use ulid::Ulid;
|
||||||
|
|
||||||
/// simple keys for caching responses
|
|
||||||
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, EnumCount, EnumIter)]
|
|
||||||
pub enum ResponseCacheKey {
|
|
||||||
BackupsNeeded,
|
|
||||||
Health,
|
|
||||||
Status,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type ResponseCache = Cache<ResponseCacheKey, (StatusCode, &'static str, axum::body::Bytes)>;
|
|
||||||
|
|
||||||
/// Start the frontend server.
|
/// Start the frontend server.
|
||||||
pub async fn serve(
|
pub async fn serve(
|
||||||
app: Arc<Web3ProxyApp>,
|
app: Arc<Web3ProxyApp>,
|
||||||
mut shutdown_receiver: broadcast::Receiver<()>,
|
mut shutdown_receiver: broadcast::Receiver<()>,
|
||||||
shutdown_complete_sender: broadcast::Sender<()>,
|
shutdown_complete_sender: broadcast::Sender<()>,
|
||||||
) -> Web3ProxyResult<()> {
|
) -> Web3ProxyResult<()> {
|
||||||
// setup caches for whatever the frontend needs
|
|
||||||
// no need for max items since it is limited by the enum key
|
|
||||||
// TODO: latest moka allows for different ttls for different
|
|
||||||
let response_cache_size = ResponseCacheKey::COUNT;
|
|
||||||
|
|
||||||
let response_cache: ResponseCache = CacheBuilder::new(response_cache_size as u64)
|
|
||||||
.name("frontend_response")
|
|
||||||
.time_to_live(Duration::from_secs(1))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
// 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
|
||||||
|
|
||||||
// build our axum Router
|
// build our axum Router
|
||||||
@ -264,8 +242,6 @@ pub async fn serve(
|
|||||||
.layer(CorsLayer::very_permissive())
|
.layer(CorsLayer::very_permissive())
|
||||||
// application state
|
// application state
|
||||||
.layer(Extension(app.clone()))
|
.layer(Extension(app.clone()))
|
||||||
// frontend caches
|
|
||||||
.layer(Extension(Arc::new(response_cache)))
|
|
||||||
// request timeout
|
// request timeout
|
||||||
.layer(
|
.layer(
|
||||||
ServiceBuilder::new()
|
ServiceBuilder::new()
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
//! 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::{ResponseCache, ResponseCacheKey};
|
|
||||||
use crate::{
|
use crate::{
|
||||||
app::{Web3ProxyApp, APP_USER_AGENT},
|
app::{Web3ProxyApp, APP_USER_AGENT},
|
||||||
errors::Web3ProxyError,
|
errors::Web3ProxyError,
|
||||||
@ -22,8 +21,7 @@ use moka::future::Cache;
|
|||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use serde::{ser::SerializeStruct, Serialize};
|
use serde::{ser::SerializeStruct, Serialize};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use std::{sync::Arc, time::Duration};
|
use std::sync::Arc;
|
||||||
use tokio::time::timeout;
|
|
||||||
use tracing::trace;
|
use tracing::trace;
|
||||||
|
|
||||||
static HEALTH_OK: Lazy<Bytes> = Lazy::new(|| Bytes::from("OK\n"));
|
static HEALTH_OK: Lazy<Bytes> = Lazy::new(|| Bytes::from("OK\n"));
|
||||||
@ -77,13 +75,8 @@ pub async fn debug_request(
|
|||||||
#[debug_handler]
|
#[debug_handler]
|
||||||
pub async fn health(
|
pub async fn health(
|
||||||
Extension(app): Extension<Arc<Web3ProxyApp>>,
|
Extension(app): Extension<Arc<Web3ProxyApp>>,
|
||||||
Extension(cache): Extension<Arc<ResponseCache>>,
|
|
||||||
) -> Result<impl IntoResponse, Web3ProxyError> {
|
) -> Result<impl IntoResponse, Web3ProxyError> {
|
||||||
let (code, content_type, body) = timeout(
|
let (code, content_type, body) = _health(app).await;
|
||||||
Duration::from_secs(3),
|
|
||||||
cache.get_with(ResponseCacheKey::Health, async move { _health(app).await }),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let x = Response::builder()
|
let x = Response::builder()
|
||||||
.status(code)
|
.status(code)
|
||||||
@ -114,15 +107,8 @@ async fn _health(app: Arc<Web3ProxyApp>) -> (StatusCode, &'static str, Bytes) {
|
|||||||
#[debug_handler]
|
#[debug_handler]
|
||||||
pub async fn backups_needed(
|
pub async fn backups_needed(
|
||||||
Extension(app): Extension<Arc<Web3ProxyApp>>,
|
Extension(app): Extension<Arc<Web3ProxyApp>>,
|
||||||
Extension(cache): Extension<Arc<ResponseCache>>,
|
|
||||||
) -> Result<impl IntoResponse, Web3ProxyError> {
|
) -> Result<impl IntoResponse, Web3ProxyError> {
|
||||||
let (code, content_type, body) = timeout(
|
let (code, content_type, body) = _backups_needed(app).await;
|
||||||
Duration::from_secs(3),
|
|
||||||
cache.get_with(ResponseCacheKey::BackupsNeeded, async move {
|
|
||||||
_backups_needed(app).await
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let x = Response::builder()
|
let x = Response::builder()
|
||||||
.status(code)
|
.status(code)
|
||||||
@ -165,13 +151,8 @@ async fn _backups_needed(app: Arc<Web3ProxyApp>) -> (StatusCode, &'static str, B
|
|||||||
#[debug_handler]
|
#[debug_handler]
|
||||||
pub async fn status(
|
pub async fn status(
|
||||||
Extension(app): Extension<Arc<Web3ProxyApp>>,
|
Extension(app): Extension<Arc<Web3ProxyApp>>,
|
||||||
Extension(cache): Extension<Arc<ResponseCache>>,
|
|
||||||
) -> Result<impl IntoResponse, Web3ProxyError> {
|
) -> Result<impl IntoResponse, Web3ProxyError> {
|
||||||
let (code, content_type, body) = timeout(
|
let (code, content_type, body) = _status(app).await;
|
||||||
Duration::from_secs(3),
|
|
||||||
cache.get_with(ResponseCacheKey::Status, async move { _status(app).await }),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let x = Response::builder()
|
let x = Response::builder()
|
||||||
.status(code)
|
.status(code)
|
||||||
|
Loading…
Reference in New Issue
Block a user