From f446b861f0f75bc627d0bde566317a5568eb92ac Mon Sep 17 00:00:00 2001 From: Alex Snaps Date: Tue, 19 Jul 2022 14:44:29 +0100 Subject: [PATCH] Wired all configuration options in --- limitador-server/src/config.rs | 3 +++ limitador-server/src/main.rs | 43 ++++++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/limitador-server/src/config.rs b/limitador-server/src/config.rs index 6895e36b..ed88ea03 100644 --- a/limitador-server/src/config.rs +++ b/limitador-server/src/config.rs @@ -18,6 +18,7 @@ // HTTP_API_HOST: host // just to become HTTP_API_HOST:HTTP_API_PORT as &str // HTTP_API_PORT: port +use limitador::storage::redis::DEFAULT_MAX_CACHED_COUNTERS; use std::env; #[derive(Debug)] @@ -105,6 +106,7 @@ fn storage_config_from_env() -> Result { .unwrap_or_else(|_| "10".to_string()) .parse() .expect("Expected an u64"), + max_counters: DEFAULT_MAX_CACHED_COUNTERS, }) } else { None @@ -146,6 +148,7 @@ pub struct RedisStorageCacheConfiguration { pub flushing_period: i64, pub max_ttl: u64, pub ttl_ratio: u64, + pub max_counters: usize, } #[derive(PartialEq, Debug)] diff --git a/limitador-server/src/main.rs b/limitador-server/src/main.rs index 7cd13d99..20f88d94 100644 --- a/limitador-server/src/main.rs +++ b/limitador-server/src/main.rs @@ -20,8 +20,9 @@ use limitador::storage::infinispan::{ DEFAULT_INFINISPAN_CONSISTENCY, DEFAULT_INFINISPAN_LIMITS_CACHE_NAME, }; use limitador::storage::redis::{ - AsyncRedisStorage, CachedRedisStorage, CachedRedisStorageBuilder, - DEFAULT_MAX_TTL_CACHED_COUNTERS_SEC, + AsyncRedisStorage, CachedRedisStorage, CachedRedisStorageBuilder, DEFAULT_FLUSHING_PERIOD_SEC, + DEFAULT_MAX_CACHED_COUNTERS, DEFAULT_MAX_TTL_CACHED_COUNTERS_SEC, + DEFAULT_TTL_RATIO_CACHED_COUNTERS, }; use limitador::storage::{AsyncCounterStorage, AsyncStorage}; use limitador::{AsyncRateLimiter, AsyncRateLimiterBuilder, RateLimiter, RateLimiterBuilder}; @@ -125,6 +126,7 @@ impl Limiter { cached_redis_storage.max_ttl_cached_counters(Duration::from_millis(cache_cfg.max_ttl)); cached_redis_storage = cached_redis_storage.ttl_ratio_cached_counters(cache_cfg.ttl_ratio); + cached_redis_storage = cached_redis_storage.max_cached_counters(cache_cfg.max_counters); cached_redis_storage.build().await } @@ -219,7 +221,12 @@ impl Limiter { #[actix_rt::main] async fn main() -> Result<(), Box> { let infinispan_consistency_default = format!("{}", DEFAULT_INFINISPAN_CONSISTENCY); + let redis_cached_ttl_default = DEFAULT_MAX_TTL_CACHED_COUNTERS_SEC.to_string(); + let redis_flushing_period_default = DEFAULT_FLUSHING_PERIOD_SEC.to_string(); + let redis_max_cached_counters_default = DEFAULT_MAX_CACHED_COUNTERS.to_string(); + let redis_ttl_ratio_default = DEFAULT_TTL_RATIO_CACHED_COUNTERS.to_string(); + let cmdline = App::new("Limitador Server") .version(LIMITADOR_VERSION) .author("The Kuadrant team - github.com/Kuadrant") @@ -323,6 +330,33 @@ async fn main() -> Result<(), Box> { .default_value(&redis_cached_ttl_default) .display_order(2) .help("TTL for cached counters in seconds"), + ) + .arg( + Arg::with_name("ratio") + .long("ratio") + .takes_value(true) + .value_parser(clap::value_parser!(u64)) + .default_value(&redis_ttl_ratio_default) + .display_order(3) + .help("Ratio to apply to the TTL from Redis on cached counters"), + ) + .arg( + Arg::with_name("flush") + .long("flush-period") + .takes_value(true) + .value_parser(clap::value_parser!(i64)) + .default_value(&redis_flushing_period_default) + .display_order(4) + .help("Flushing period for counters in seconds"), + ) + .arg( + Arg::with_name("max") + .long("max-cached") + .takes_value(true) + .value_parser(clap::value_parser!(usize)) + .default_value(&redis_max_cached_counters_default) + .display_order(5) + .help("Maximum amount of counters cached"), ), ) .subcommand( @@ -381,9 +415,10 @@ async fn main() -> Result<(), Box> { Some(("redis_cached", sub)) => StorageConfiguration::Redis(RedisStorageConfiguration { url: sub.value_of("URL").unwrap().to_owned(), cache: Some(RedisStorageCacheConfiguration { - flushing_period: 0, + flushing_period: *sub.get_one("flush").unwrap(), max_ttl: *sub.get_one("TTL").unwrap(), - ttl_ratio: 0, + ttl_ratio: *sub.get_one("ratio").unwrap(), + max_counters: *sub.get_one("max").unwrap(), }), }), Some(("infinispan", sub)) => {