better variable names

This commit is contained in:
Bryan Stitt 2023-05-16 14:58:00 -07:00
parent 40b15579ab
commit 525f6d4a34
2 changed files with 16 additions and 20 deletions

@ -30,14 +30,14 @@ use tower_http::sensitive_headers::SetSensitiveRequestHeadersLayer;
/// simple keys for caching responses /// simple keys for caching responses
#[derive(Copy, Clone, Hash, PartialEq, Eq, EnumCount, EnumIter)] #[derive(Copy, Clone, Hash, PartialEq, Eq, EnumCount, EnumIter)]
pub enum FrontendResponseCacheKey { pub enum ResponseCacheKey {
BackupsNeeded, BackupsNeeded,
Health, Health,
Status, Status,
} }
pub type FrontendJsonResponseCache = quick_cache_ttl::CacheWithTTL< pub type ResponseCache = quick_cache_ttl::CacheWithTTL<
FrontendResponseCacheKey, ResponseCacheKey,
(StatusCode, axum::body::Bytes), (StatusCode, axum::body::Bytes),
UnitWeighter, UnitWeighter,
quick_cache_ttl::DefaultHashBuilder, quick_cache_ttl::DefaultHashBuilder,
@ -53,13 +53,10 @@ pub async fn serve(
// setup caches for whatever the frontend needs // setup caches for whatever the frontend needs
// no need for max items since it is limited by the enum key // no need for max items since it is limited by the enum key
// TODO: latest moka allows for different ttls for different // TODO: latest moka allows for different ttls for different
let response_cache_size = FrontendResponseCacheKey::COUNT; let response_cache_size = ResponseCacheKey::COUNT;
let json_response_cache = FrontendJsonResponseCache::new_with_unit_weights( let response_cache =
response_cache_size, ResponseCache::new_with_unit_weights(response_cache_size, Duration::from_secs(1)).await;
Duration::from_secs(1),
)
.await;
// 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
@ -225,7 +222,7 @@ pub async fn serve(
// application state // application state
.layer(Extension(proxy_app)) .layer(Extension(proxy_app))
// frontend caches // frontend caches
.layer(Extension(Arc::new(json_response_cache))) .layer(Extension(Arc::new(response_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::{FrontendJsonResponseCache, FrontendResponseCacheKey}; use super::{ResponseCache, ResponseCacheKey};
use crate::app::{Web3ProxyApp, APP_USER_AGENT}; use crate::app::{Web3ProxyApp, APP_USER_AGENT};
use axum::{body::Bytes, http::StatusCode, response::IntoResponse, Extension}; use axum::{body::Bytes, http::StatusCode, response::IntoResponse, Extension};
use axum_macros::debug_handler; use axum_macros::debug_handler;
@ -21,10 +21,10 @@ static BACKUPS_NEEDED_FALSE: Lazy<Bytes> = Lazy::new(|| Bytes::from("false\n"));
#[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<FrontendJsonResponseCache>>, Extension(cache): Extension<Arc<ResponseCache>>,
) -> impl IntoResponse { ) -> impl IntoResponse {
cache cache
.get_or_insert_async::<Infallible, _>(&FrontendResponseCacheKey::Health, async move { .get_or_insert_async::<Infallible, _>(&ResponseCacheKey::Health, async move {
Ok(_health(app).await) Ok(_health(app).await)
}) })
.await .await
@ -44,13 +44,12 @@ async fn _health(app: Arc<Web3ProxyApp>) -> (StatusCode, 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<FrontendJsonResponseCache>>, Extension(cache): Extension<Arc<ResponseCache>>,
) -> impl IntoResponse { ) -> impl IntoResponse {
cache cache
.get_or_insert_async::<Infallible, _>( .get_or_insert_async::<Infallible, _>(&ResponseCacheKey::BackupsNeeded, async move {
&FrontendResponseCacheKey::BackupsNeeded, Ok(_backups_needed(app).await)
async move { Ok(_backups_needed(app).await) }, })
)
.await .await
} }
@ -88,10 +87,10 @@ async fn _backups_needed(app: Arc<Web3ProxyApp>) -> (StatusCode, Bytes) {
#[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<FrontendJsonResponseCache>>, Extension(cache): Extension<Arc<ResponseCache>>,
) -> impl IntoResponse { ) -> impl IntoResponse {
cache cache
.get_or_insert_async::<Infallible, _>(&FrontendResponseCacheKey::Status, async move { .get_or_insert_async::<Infallible, _>(&ResponseCacheKey::Status, async move {
Ok(_status(app).await) Ok(_status(app).await)
}) })
.await .await