Skip to content

Commit

Permalink
Remove ttl ratio thingy
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsnaps committed May 9, 2024
1 parent 481272e commit 715fd2c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 56 deletions.
1 change: 0 additions & 1 deletion limitador-server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
20 changes: 1 addition & 19 deletions limitador-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -734,7 +721,6 @@ fn create_config() -> (Configuration, &'static str) {
url: sub.get_one::<String>("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(),
}),
Expand Down Expand Up @@ -817,10 +803,6 @@ fn storage_config_from_env() -> Result<StorageConfiguration, ()> {
.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,
})
Expand Down
53 changes: 31 additions & 22 deletions limitador/src/storage/redis/counters_cache.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -219,7 +219,6 @@ impl Default for Batcher {
}

pub struct CountersCache {
pub ttl_ratio_cached_counters: u64,
cache: Cache<Counter, Arc<CachedCounterValue>>,
batcher: Batcher,
}
Expand Down Expand Up @@ -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,
}
}

Expand All @@ -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),
}
Expand Down Expand Up @@ -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());
}
Expand All @@ -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(),
Expand All @@ -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!(
Expand Down
1 change: 0 additions & 1 deletion limitador/src/storage/redis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 0 additions & 12 deletions limitador/src/storage/redis/redis_cached.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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<Self, RedisError> {
let info = ConnectionInfo::from_str(redis_url)?;
Expand All @@ -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);
Expand Down Expand Up @@ -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,
}

Expand All @@ -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),
}
}
Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion limitador/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 715fd2c

Please sign in to comment.