From a7761a0430ab642b9547b8c2deb740a0e52c34ed Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Mon, 19 Sep 2022 22:17:35 +0000 Subject: [PATCH] add a weigher function to sized caches --- TODO.md | 2 ++ web3_proxy/src/app.rs | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/TODO.md b/TODO.md index 35ab4c59..493ac744 100644 --- a/TODO.md +++ b/TODO.md @@ -167,6 +167,8 @@ These are roughly in order of completition - since users are actively using our service, we will need to support both - [ ] Ulid instead of Uuid for database ids - might have to use Uuid in sea-orm and then convert to Ulid on display +- [ ] database migration to change user_keys.requests_per_minute to bigint (max of 18446744073709551615) +- [ ] change user creation script to have a "unlimited requests per minute" flag that sets it to 18446744073709551615 ## V1 diff --git a/web3_proxy/src/app.rs b/web3_proxy/src/app.rs index ba530c5f..5cc8fd8b 100644 --- a/web3_proxy/src/app.rs +++ b/web3_proxy/src/app.rs @@ -30,6 +30,7 @@ use sea_orm::DatabaseConnection; use serde::Serialize; use serde_json::json; use std::fmt; +use std::mem::size_of_val; use std::net::IpAddr; use std::pin::Pin; use std::str::FromStr; @@ -62,7 +63,8 @@ pub type AnyhowJoinHandle = JoinHandle>; pub struct UserCacheValue { pub expires_at: Instant, pub user_id: u64, - pub user_count_per_period: u64, + /// if None, allow unlimited queries + pub user_count_per_period: Option, } /// The application @@ -269,7 +271,7 @@ impl Web3ProxyApp { // TODO: these blocks don't have full transactions, but they do have rather variable amounts of transaction hashes let block_map = Cache::builder() .max_capacity(10_000) - .weigher() + .weigher(|_k, v| size_of_val(v) as u32) .build_with_hasher(ahash::RandomState::new()); let (balanced_rpcs, balanced_handle) = Web3Connections::spawn( @@ -345,7 +347,7 @@ impl Web3ProxyApp { // TODO: max_capacity from config let response_cache = Cache::builder() .max_capacity(10_000) - .weigher() + .weigher(|k, v| (size_of_val(k) + size_of_val(v)) as u32) .build_with_hasher(ahash::RandomState::new()); // all the users are the same size, so no need for a weigher