diff --git a/limitador-server/src/config.rs b/limitador-server/src/config.rs index 9c5931bb..372b1732 100644 --- a/limitador-server/src/config.rs +++ b/limitador-server/src/config.rs @@ -163,7 +163,6 @@ pub struct RedisStorageConfiguration { #[derive(PartialEq, Eq, Debug)] pub struct RedisStorageCacheConfiguration { pub flushing_period: i64, - pub ttl_ratio: u64, pub max_counters: usize, pub response_timeout: u64, } diff --git a/limitador-server/src/main.rs b/limitador-server/src/main.rs index cbfa72c1..226f1aa9 100644 --- a/limitador-server/src/main.rs +++ b/limitador-server/src/main.rs @@ -25,7 +25,7 @@ use limitador::storage::disk::DiskStorage; use limitador::storage::infinispan::{Consistency, InfinispanStorageBuilder}; use limitador::storage::redis::{ AsyncRedisStorage, CachedRedisStorage, CachedRedisStorageBuilder, DEFAULT_FLUSHING_PERIOD_SEC, - DEFAULT_MAX_CACHED_COUNTERS, DEFAULT_RESPONSE_TIMEOUT_MS, DEFAULT_TTL_RATIO_CACHED_COUNTERS, + DEFAULT_MAX_CACHED_COUNTERS, DEFAULT_RESPONSE_TIMEOUT_MS, }; use limitador::storage::{AsyncCounterStorage, AsyncStorage, Storage}; use limitador::{ @@ -133,7 +133,6 @@ impl Limiter { let cached_redis_storage = CachedRedisStorageBuilder::new(redis_url) .flushing_period(Duration::from_millis(cache_cfg.flushing_period as u64)) - .ttl_ratio_cached_counters(cache_cfg.ttl_ratio) .max_cached_counters(cache_cfg.max_counters) .response_timeout(Duration::from_millis(cache_cfg.response_timeout)); @@ -589,18 +588,6 @@ fn create_config() -> (Configuration, &'static str) { .about("Uses Redis to store counters, with an in-memory cache") .display_order(4) .arg(redis_url_arg) - .arg( - Arg::new("ratio") - .long("ratio") - .action(ArgAction::Set) - .value_parser(clap::value_parser!(u64)) - .default_value( - config::env::REDIS_LOCAL_CACHE_TTL_RATIO_CACHED_COUNTERS - .unwrap_or(leak(DEFAULT_TTL_RATIO_CACHED_COUNTERS)), - ) - .display_order(3) - .help("Ratio to apply to the TTL from Redis on cached counters"), - ) .arg( Arg::new("flush") .long("flush-period") @@ -734,7 +721,6 @@ fn create_config() -> (Configuration, &'static str) { url: sub.get_one::("URL").unwrap().to_owned(), cache: Some(RedisStorageCacheConfiguration { flushing_period: *sub.get_one("flush").unwrap(), - ttl_ratio: *sub.get_one("ratio").unwrap(), max_counters: *sub.get_one("max").unwrap(), response_timeout: *sub.get_one("timeout").unwrap(), }), @@ -817,10 +803,6 @@ fn storage_config_from_env() -> Result { .unwrap_or_else(|_| (DEFAULT_FLUSHING_PERIOD_SEC * 1000).to_string()) .parse() .expect("Expected an i64"), - ttl_ratio: env::var("REDIS_LOCAL_CACHE_TTL_RATIO_CACHED_COUNTERS") - .unwrap_or_else(|_| DEFAULT_TTL_RATIO_CACHED_COUNTERS.to_string()) - .parse() - .expect("Expected an u64"), max_counters: DEFAULT_MAX_CACHED_COUNTERS, response_timeout: DEFAULT_RESPONSE_TIMEOUT_MS, }) diff --git a/limitador/src/storage/redis/counters_cache.rs b/limitador/src/storage/redis/counters_cache.rs index 4a47b188..3949d0c6 100644 --- a/limitador/src/storage/redis/counters_cache.rs +++ b/limitador/src/storage/redis/counters_cache.rs @@ -1,6 +1,6 @@ use crate::counter::Counter; use crate::storage::atomic_expiring_value::AtomicExpiringValue; -use crate::storage::redis::{DEFAULT_MAX_CACHED_COUNTERS, DEFAULT_TTL_RATIO_CACHED_COUNTERS}; +use crate::storage::redis::DEFAULT_MAX_CACHED_COUNTERS; use dashmap::mapref::entry::Entry; use dashmap::DashMap; use moka::sync::Cache; @@ -219,7 +219,6 @@ impl Default for Batcher { } pub struct CountersCache { - pub ttl_ratio_cached_counters: u64, cache: Cache>, batcher: Batcher, } @@ -288,14 +287,12 @@ impl CountersCache { pub struct CountersCacheBuilder { max_cached_counters: usize, - ttl_ratio_cached_counters: u64, } impl CountersCacheBuilder { pub fn new() -> Self { Self { max_cached_counters: DEFAULT_MAX_CACHED_COUNTERS, - ttl_ratio_cached_counters: DEFAULT_TTL_RATIO_CACHED_COUNTERS, } } @@ -304,14 +301,8 @@ impl CountersCacheBuilder { self } - pub fn ttl_ratio_cached_counter(mut self, ttl_ratio_cached_counter: u64) -> Self { - self.ttl_ratio_cached_counters = ttl_ratio_cached_counter; - self - } - pub fn build(&self, period: Duration) -> CountersCache { CountersCache { - ttl_ratio_cached_counters: self.ttl_ratio_cached_counters, cache: Cache::new(self.max_cached_counters as u64), batcher: Batcher::new(period), } @@ -532,10 +523,16 @@ mod tests { let counter = test_counter(10, None); let cache = CountersCacheBuilder::new().build(Duration::default()); - cache.apply_remote_delta(counter.clone(), 10, 0, SystemTime::now() - .add(Duration::from_secs(1)) - .duration_since(UNIX_EPOCH) - .unwrap().as_micros() as i64); + cache.apply_remote_delta( + counter.clone(), + 10, + 0, + SystemTime::now() + .add(Duration::from_secs(1)) + .duration_since(UNIX_EPOCH) + .unwrap() + .as_micros() as i64, + ); assert!(cache.get(&counter).is_some()); } @@ -556,10 +553,16 @@ mod tests { let counter = test_counter(max_val, None); let cache = CountersCacheBuilder::new().build(Duration::default()); - cache.apply_remote_delta(counter.clone(), current_value, 0, SystemTime::now() - .add(Duration::from_secs(1)) - .duration_since(UNIX_EPOCH) - .unwrap().as_micros() as i64); + cache.apply_remote_delta( + counter.clone(), + current_value, + 0, + SystemTime::now() + .add(Duration::from_secs(1)) + .duration_since(UNIX_EPOCH) + .unwrap() + .as_micros() as i64, + ); assert_eq!( cache.get(&counter).map(|e| e.hits(&counter)).unwrap(), @@ -574,10 +577,16 @@ mod tests { let counter = test_counter(current_val, None); let cache = CountersCacheBuilder::new().build(Duration::default()); - cache.apply_remote_delta(counter.clone(), current_val, 0, SystemTime::now() - .add(Duration::from_secs(1)) - .duration_since(UNIX_EPOCH) - .unwrap().as_micros() as i64); + cache.apply_remote_delta( + counter.clone(), + current_val, + 0, + SystemTime::now() + .add(Duration::from_secs(1)) + .duration_since(UNIX_EPOCH) + .unwrap() + .as_micros() as i64, + ); cache.increase_by(&counter, increase_by); assert_eq!( diff --git a/limitador/src/storage/redis/mod.rs b/limitador/src/storage/redis/mod.rs index b4ad9dfc..785c13f8 100644 --- a/limitador/src/storage/redis/mod.rs +++ b/limitador/src/storage/redis/mod.rs @@ -9,7 +9,6 @@ mod scripts; pub const DEFAULT_FLUSHING_PERIOD_SEC: u64 = 1; pub const DEFAULT_MAX_CACHED_COUNTERS: usize = 10000; -pub const DEFAULT_TTL_RATIO_CACHED_COUNTERS: u64 = 10; pub const DEFAULT_RESPONSE_TIMEOUT_MS: u64 = 350; use crate::counter::Counter; diff --git a/limitador/src/storage/redis/redis_cached.rs b/limitador/src/storage/redis/redis_cached.rs index f9a5cc94..33872aa8 100644 --- a/limitador/src/storage/redis/redis_cached.rs +++ b/limitador/src/storage/redis/redis_cached.rs @@ -8,7 +8,6 @@ use crate::storage::redis::redis_async::AsyncRedisStorage; use crate::storage::redis::scripts::BATCH_UPDATE_COUNTERS; use crate::storage::redis::{ DEFAULT_FLUSHING_PERIOD_SEC, DEFAULT_MAX_CACHED_COUNTERS, DEFAULT_RESPONSE_TIMEOUT_MS, - DEFAULT_TTL_RATIO_CACHED_COUNTERS, }; use crate::storage::{AsyncCounterStorage, Authorization, StorageErr}; use async_trait::async_trait; @@ -149,7 +148,6 @@ impl CachedRedisStorage { redis_url, Duration::from_secs(DEFAULT_FLUSHING_PERIOD_SEC), DEFAULT_MAX_CACHED_COUNTERS, - DEFAULT_TTL_RATIO_CACHED_COUNTERS, Duration::from_millis(DEFAULT_RESPONSE_TIMEOUT_MS), ) .await @@ -159,7 +157,6 @@ impl CachedRedisStorage { redis_url: &str, flushing_period: Duration, max_cached_counters: usize, - ttl_ratio_cached_counters: u64, response_timeout: Duration, ) -> Result { let info = ConnectionInfo::from_str(redis_url)?; @@ -177,7 +174,6 @@ impl CachedRedisStorage { let cached_counters = CountersCacheBuilder::new() .max_cached_counters(max_cached_counters) - .ttl_ratio_cached_counter(ttl_ratio_cached_counters) .build(flushing_period); let counters_cache = Arc::new(cached_counters); @@ -230,7 +226,6 @@ pub struct CachedRedisStorageBuilder { redis_url: String, flushing_period: Duration, max_cached_counters: usize, - ttl_ratio_cached_counters: u64, response_timeout: Duration, } @@ -240,7 +235,6 @@ impl CachedRedisStorageBuilder { redis_url: redis_url.to_string(), flushing_period: Duration::from_secs(DEFAULT_FLUSHING_PERIOD_SEC), max_cached_counters: DEFAULT_MAX_CACHED_COUNTERS, - ttl_ratio_cached_counters: DEFAULT_TTL_RATIO_CACHED_COUNTERS, response_timeout: Duration::from_millis(DEFAULT_RESPONSE_TIMEOUT_MS), } } @@ -255,11 +249,6 @@ impl CachedRedisStorageBuilder { self } - pub fn ttl_ratio_cached_counters(mut self, ttl_ratio_cached_counters: u64) -> Self { - self.ttl_ratio_cached_counters = ttl_ratio_cached_counters; - self - } - pub fn response_timeout(mut self, response_timeout: Duration) -> Self { self.response_timeout = response_timeout; self @@ -270,7 +259,6 @@ impl CachedRedisStorageBuilder { &self.redis_url, self.flushing_period, self.max_cached_counters, - self.ttl_ratio_cached_counters, self.response_timeout, ) .await diff --git a/limitador/tests/integration_tests.rs b/limitador/tests/integration_tests.rs index 1b08129d..7bd8cb7d 100644 --- a/limitador/tests/integration_tests.rs +++ b/limitador/tests/integration_tests.rs @@ -60,7 +60,6 @@ macro_rules! test_with_all_storage_impls { async fn [<$function _with_async_redis_and_local_cache>]() { let storage_builder = CachedRedisStorageBuilder::new("redis://127.0.0.1:6379"). flushing_period(Duration::from_millis(2)). - ttl_ratio_cached_counters(1). max_cached_counters(10000); let storage = storage_builder.build().await.expect("We need a Redis running locally"); storage.clear().await.unwrap();