From 3d4bfbfde05850cd872ba8b5cfad5612eac88ec5 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Sat, 17 Sep 2022 01:19:11 +0000 Subject: [PATCH] use ahash. allow no redis --- Cargo.lock | 32 +++++++++++++++----------------- deferred-rate-limiter/Cargo.toml | 2 +- deferred-rate-limiter/src/lib.rs | 17 ++++++++++++++--- web3_proxy/src/app.rs | 6 ++++-- 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 84fe196e..bb2fda05 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -50,6 +50,18 @@ dependencies = [ "version_check", ] +[[package]] +name = "ahash" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57e6e951cfbb2db8de1828d49073a113a29fd7117b1596caa781a258c7e38d72" +dependencies = [ + "cfg-if", + "getrandom", + "once_cell", + "version_check", +] + [[package]] name = "aho-corasick" version = "0.7.18" @@ -1097,20 +1109,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "crossbeam" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2801af0d36612ae591caa9568261fddce32ce6e08a7275ea334a06a4ad021a2c" -dependencies = [ - "cfg-if", - "crossbeam-channel", - "crossbeam-deque", - "crossbeam-epoch", - "crossbeam-queue", - "crossbeam-utils", -] - [[package]] name = "crossbeam-channel" version = "0.5.6" @@ -1274,8 +1272,8 @@ dependencies = [ name = "deferred-rate-limiter" version = "0.2.0" dependencies = [ + "ahash 0.8.0", "anyhow", - "crossbeam", "moka", "redis-rate-limiter", "tokio", @@ -2239,7 +2237,7 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" dependencies = [ - "ahash", + "ahash 0.7.6", "serde", ] @@ -4611,7 +4609,7 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b69bf218860335ddda60d6ce85ee39f6cf6e5630e300e19757d1de15886a093" dependencies = [ - "ahash", + "ahash 0.7.6", "atoi", "bitflags", "byteorder", diff --git a/deferred-rate-limiter/Cargo.toml b/deferred-rate-limiter/Cargo.toml index f48d216c..a5465ad8 100644 --- a/deferred-rate-limiter/Cargo.toml +++ b/deferred-rate-limiter/Cargo.toml @@ -7,8 +7,8 @@ edition = "2021" [dependencies] redis-rate-limiter = { path = "../redis-rate-limiter" } +ahash = "0.8.0" anyhow = "1.0.65" -crossbeam = "*" moka = { version = "0.9.4", default-features = false, features = ["future"] } tokio = "1.21.1" tracing = "0.1.36" diff --git a/deferred-rate-limiter/src/lib.rs b/deferred-rate-limiter/src/lib.rs index e26bdc39..ed2bcd55 100644 --- a/deferred-rate-limiter/src/lib.rs +++ b/deferred-rate-limiter/src/lib.rs @@ -7,7 +7,7 @@ use std::hash::Hash; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{atomic::AtomicU64, Arc}; use tokio::sync::Mutex; -use tokio::time::Instant; +use tokio::time::{Duration, Instant}; use tracing::error; /// A local cache that sits in front of a RedisRateLimiter @@ -16,7 +16,7 @@ pub struct DeferredRateLimiter where K: Send + Sync, { - local_cache: Cache>, + local_cache: Cache, ahash::RandomState>, prefix: String, rrl: RedisRateLimiter, } @@ -32,8 +32,19 @@ where K: Copy + Debug + Display + Hash + Eq + Send + Sync + 'static, { pub fn new(cache_size: u64, prefix: &str, rrl: RedisRateLimiter) -> Self { + let ttl = rrl.period as u64; + + let hasher = ahash::RandomState::new(); + + // TODO: think more about this ttl. if + let local_cache = Cache::builder() + .time_to_live(Duration::from_secs(ttl)) + .max_capacity(cache_size) + .name(prefix) + .build_with_hasher(hasher); + Self { - local_cache: Cache::new(cache_size), + local_cache, prefix: prefix.to_string(), rrl, } diff --git a/web3_proxy/src/app.rs b/web3_proxy/src/app.rs index 57343792..d17e1085 100644 --- a/web3_proxy/src/app.rs +++ b/web3_proxy/src/app.rs @@ -40,7 +40,7 @@ use tokio::sync::{broadcast, watch}; use tokio::task::JoinHandle; use tokio::time::{timeout, Instant}; use tokio_stream::wrappers::{BroadcastStream, WatchStream}; -use tracing::{info, info_span, instrument, trace, warn, Instrument}; +use tracing::{error, info, info_span, instrument, trace, warn, Instrument}; use uuid::Uuid; // TODO: make this customizable? @@ -231,7 +231,9 @@ impl Web3ProxyApp { .build()?; // test the pool - redis_pool.get().await.context("Redis connection failed")?; + if let Err(err) = redis_pool.get().await { + error!("failed to connect to redis. some features will be disabled"); + }; Some(redis_pool) }