use ahash. allow no redis

This commit is contained in:
Bryan Stitt 2022-09-17 01:19:11 +00:00
parent 6182b5f8e6
commit 3d4bfbfde0
4 changed files with 34 additions and 23 deletions

32
Cargo.lock generated

@ -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",

@ -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"

@ -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<K>
where
K: Send + Sync,
{
local_cache: Cache<K, Arc<AtomicU64>>,
local_cache: Cache<K, Arc<AtomicU64>, 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,
}

@ -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)
}