From 2b814e7a4dd77d154657687e0ddf22d5409044fe Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Wed, 7 Jun 2023 10:48:55 -0700 Subject: [PATCH] serialize the caches on /status --- Cargo.lock | 1 + quick_cache_ttl/Cargo.toml | 1 + quick_cache_ttl/src/cache.rs | 44 +++++++++++++++++------- quick_cache_ttl/src/kq_cache.rs | 38 ++++++++++++++------ web3_proxy/src/frontend/authorization.rs | 3 +- web3_proxy/src/frontend/status.rs | 9 +++-- web3_proxy/src/rpcs/many.rs | 2 +- 7 files changed, 70 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 28ccecd6..7337edec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4042,6 +4042,7 @@ dependencies = [ "flume", "log", "quick_cache", + "serde", "tokio", ] diff --git a/quick_cache_ttl/Cargo.toml b/quick_cache_ttl/Cargo.toml index 8df44b3e..6e0da4f4 100644 --- a/quick_cache_ttl/Cargo.toml +++ b/quick_cache_ttl/Cargo.toml @@ -9,6 +9,7 @@ edition = "2021" flume = "0.10.14" log = "0.4.18" quick_cache = "0.3.0" +serde = "1" tokio = { version = "1.28.2", features = ["full"] } [dev-dependencies] diff --git a/quick_cache_ttl/src/cache.rs b/quick_cache_ttl/src/cache.rs index 418d6082..fd14c03b 100644 --- a/quick_cache_ttl/src/cache.rs +++ b/quick_cache_ttl/src/cache.rs @@ -1,4 +1,6 @@ +use crate::{KQCacheWithTTL, PlaceholderGuardWithTTL}; use quick_cache::{DefaultHashBuilder, UnitWeighter, Weighter}; +use serde::{Serialize, Serializer}; use std::{ fmt::Debug, future::Future, @@ -8,8 +10,6 @@ use std::{ time::Duration, }; -use crate::{KQCacheWithTTL, PlaceholderGuardWithTTL}; - pub struct CacheWithTTL( KQCacheWithTTL, ); @@ -110,14 +110,6 @@ impl< self.0.get_or_insert_async(key, &(), f).await } - #[inline] - pub async fn try_get_or_insert_async(&self, key: &Key, f: Fut) -> Result - where - Fut: Future>, - { - self.0.try_get_or_insert_async(key, &(), f).await - } - #[inline] pub async fn get_value_or_guard_async( &self, @@ -126,6 +118,16 @@ impl< self.0.get_value_or_guard_async(key, ()).await } + #[inline] + pub fn peek(&self, key: &Key) -> Option { + self.0.peek(key, &()) + } + + #[inline] + pub fn remove(&self, key: &Key) -> bool { + self.0.remove(key, &()) + } + /// if the item was too large to insert, it is returned with the error /// IMPORTANT! Inserting the same key multiple times does NOT reset the TTL! #[inline] @@ -134,7 +136,25 @@ impl< } #[inline] - pub fn remove(&self, key: &Key) -> bool { - self.0.remove(key, &()) + pub async fn try_get_or_insert_async(&self, key: &Key, f: Fut) -> Result + where + Fut: Future>, + { + self.0.try_get_or_insert_async(key, &(), f).await + } +} + +impl< + Key: Clone + Debug + Eq + Hash + Send + Sync + 'static, + Val: Clone + Send + Sync + 'static, + We: Weighter + Clone + Send + Sync + 'static, + B: BuildHasher + Clone + Send + Sync + 'static, + > Serialize for CacheWithTTL +{ + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + self.0.serialize(serializer) } } diff --git a/quick_cache_ttl/src/kq_cache.rs b/quick_cache_ttl/src/kq_cache.rs index 11f9608d..78353893 100644 --- a/quick_cache_ttl/src/kq_cache.rs +++ b/quick_cache_ttl/src/kq_cache.rs @@ -1,6 +1,8 @@ use log::{log_enabled, trace}; use quick_cache::sync::KQCache; use quick_cache::{PlaceholderGuard, Weighter}; +use serde::ser::SerializeStruct; +use serde::{Serialize, Serializer}; use std::convert::Infallible; use std::fmt::Debug; use std::future::Future; @@ -147,11 +149,6 @@ impl< } } - #[inline] - pub fn hits(&self) -> u64 { - self.cache.hits() - } - /// if the item was too large to insert, it is returned with the error /// IMPORTANT! Inserting the same key multiple times does NOT reset the TTL! #[inline] @@ -179,11 +176,6 @@ impl< } } - #[inline] - pub fn misses(&self) -> u64 { - self.cache.misses() - } - #[inline] pub fn peek(&self, key: &Key, qey: &Qey) -> Option { self.cache.peek(key, qey) @@ -268,3 +260,29 @@ impl< } } } + +impl< + Key: Clone + Debug + Eq + Hash + Send + Sync + 'static, + Qey: Clone + Debug + Eq + Hash + Send + Sync + 'static, + Val: Clone + Send + Sync + 'static, + We: Weighter + Clone + Send + Sync + 'static, + B: BuildHasher + Clone + Send + Sync + 'static, + > Serialize for KQCacheWithTTL +{ + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + let mut state = serializer.serialize_struct(self.name, 5)?; + + state.serialize_field("len", &self.cache.len())?; + state.serialize_field("weight", &self.cache.weight())?; + + state.serialize_field("capacity", &self.cache.capacity())?; + + state.serialize_field("hits", &self.cache.hits())?; + state.serialize_field("misses", &self.cache.misses())?; + + state.end() + } +} diff --git a/web3_proxy/src/frontend/authorization.rs b/web3_proxy/src/frontend/authorization.rs index 7e9a615a..93f1f17c 100644 --- a/web3_proxy/src/frontend/authorization.rs +++ b/web3_proxy/src/frontend/authorization.rs @@ -7,7 +7,7 @@ use crate::jsonrpc::{JsonRpcForwardedResponse, JsonRpcRequest}; use crate::rpcs::one::Web3Rpc; use crate::stats::{AppStat, BackendRequests, RpcQueryStats}; use crate::user_token::UserBearerToken; -use anyhow::{Context, Error}; +use anyhow::{Context}; use axum::headers::authorization::Bearer; use axum::headers::{Header, Origin, Referer, UserAgent}; use chrono::Utc; @@ -25,7 +25,6 @@ use ipnet::IpNet; use log::{error, trace, warn}; use migration::sea_orm::prelude::Decimal; use migration::sea_orm::{ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter}; -use num_traits::ToPrimitive; use rdkafka::message::{Header as KafkaHeader, OwnedHeaders as KafkaOwnedHeaders, OwnedMessage}; use rdkafka::producer::{FutureProducer, FutureRecord}; use rdkafka::util::Timeout as KafkaTimeout; diff --git a/web3_proxy/src/frontend/status.rs b/web3_proxy/src/frontend/status.rs index fe3f0eb5..46d4af15 100644 --- a/web3_proxy/src/frontend/status.rs +++ b/web3_proxy/src/frontend/status.rs @@ -135,12 +135,15 @@ async fn _status(app: Arc) -> (StatusCode, &'static str, Bytes) { // TODO: what else should we include? uptime, cache hit rates, cpu load, memory used // TODO: the hostname is probably not going to change. only get once at the start? let body = json!({ - "version": APP_USER_AGENT, - "chain_id": app.config.chain_id, "balanced_rpcs": app.balanced_rpcs, - "private_rpcs": app.private_rpcs, "bundler_4337_rpcs": app.bundler_4337_rpcs, + "chain_id": app.config.chain_id, "hostname": app.hostname, + "jsonrpc_response_cache": app.jsonrpc_response_cache, + "private_rpcs": app.private_rpcs, + "rpc_secret_key_cache": app.rpc_secret_key_cache, + "user_balance_cache": app.user_balance_cache, + "version": APP_USER_AGENT, }); let body = body.to_string().into_bytes(); diff --git a/web3_proxy/src/rpcs/many.rs b/web3_proxy/src/rpcs/many.rs index c89c99eb..7a4187ec 100644 --- a/web3_proxy/src/rpcs/many.rs +++ b/web3_proxy/src/rpcs/many.rs @@ -1234,7 +1234,7 @@ impl Serialize for Web3Rpcs { where S: Serializer, { - let mut state = serializer.serialize_struct("Web3Rpcs", 1)?; + let mut state = serializer.serialize_struct("Web3Rpcs", 2)?; { let by_name = self.by_name.load();