From 13183a5190b85ac7f4e3af5108dd1afd5b3813bc Mon Sep 17 00:00:00 2001 From: Alex Snaps Date: Thu, 16 May 2024 10:33:10 -0400 Subject: [PATCH] Some feature clean ups --- limitador/src/counter.rs | 1 + limitador/src/storage/atomic_expiring_value.rs | 9 ++------- limitador/src/storage/redis/counters_cache.rs | 1 + limitador/tests/helpers/tests_limiter.rs | 4 ++-- limitador/tests/integration_tests.rs | 3 +++ 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/limitador/src/counter.rs b/limitador/src/counter.rs index efd486e7..9763d627 100644 --- a/limitador/src/counter.rs +++ b/limitador/src/counter.rs @@ -40,6 +40,7 @@ impl Counter { } } + #[cfg(any(feature = "redis_storage", feature = "disk_storage"))] pub(crate) fn key(&self) -> Self { Self { limit: self.limit.clone(), diff --git a/limitador/src/storage/atomic_expiring_value.rs b/limitador/src/storage/atomic_expiring_value.rs index 6f66f246..282dd281 100644 --- a/limitador/src/storage/atomic_expiring_value.rs +++ b/limitador/src/storage/atomic_expiring_value.rs @@ -27,7 +27,7 @@ impl AtomicExpiringValue { self.value_at(SystemTime::now()) } - #[allow(dead_code)] + #[cfg(feature = "redis_storage")] pub fn add_and_set_expiry(&self, delta: u64, expiry: SystemTime) -> u64 { self.expiry.update(expiry); self.value.fetch_add(delta, Ordering::SeqCst) + delta @@ -59,11 +59,6 @@ impl AtomicExpiryTime { } } - #[allow(dead_code)] - pub fn from_now(ttl: Duration) -> Self { - Self::new(SystemTime::now() + ttl) - } - fn since_epoch(when: SystemTime) -> u64 { when.duration_since(UNIX_EPOCH) .expect("SystemTime before UNIX EPOCH!") @@ -83,7 +78,7 @@ impl AtomicExpiryTime { self.expiry.load(Ordering::SeqCst) <= when } - #[allow(dead_code)] + #[cfg(feature = "redis_storage")] pub fn update(&self, expiry: SystemTime) { self.expiry .store(Self::since_epoch(expiry), Ordering::SeqCst); diff --git a/limitador/src/storage/redis/counters_cache.rs b/limitador/src/storage/redis/counters_cache.rs index 463485f9..f8155314 100644 --- a/limitador/src/storage/redis/counters_cache.rs +++ b/limitador/src/storage/redis/counters_cache.rs @@ -39,6 +39,7 @@ impl CachedCounterValue { } } + #[cfg(feature = "redis_storage")] pub fn add_from_authority(&self, delta: u64, expire_at: SystemTime) { self.value.add_and_set_expiry(delta, expire_at); self.initial_value.fetch_add(delta, Ordering::SeqCst); diff --git a/limitador/tests/helpers/tests_limiter.rs b/limitador/tests/helpers/tests_limiter.rs index b7bc4ca9..2bae0c3e 100644 --- a/limitador/tests/helpers/tests_limiter.rs +++ b/limitador/tests/helpers/tests_limiter.rs @@ -10,7 +10,7 @@ use std::collections::{HashMap, HashSet}; enum LimiterImpl { Blocking(RateLimiter), - #[allow(dead_code)] // dead when no "redis_storage" + #[cfg(feature = "redis_storage")] Async(AsyncRateLimiter), } @@ -25,7 +25,7 @@ impl TestsLimiter { } } - #[allow(dead_code)] // dead when no "redis_storage" + #[cfg(feature = "redis_storage")] pub fn new_from_async_impl(limiter: AsyncRateLimiter) -> Self { Self { limiter_impl: LimiterImpl::Async(limiter), diff --git a/limitador/tests/integration_tests.rs b/limitador/tests/integration_tests.rs index f14d8f95..8237a00c 100644 --- a/limitador/tests/integration_tests.rs +++ b/limitador/tests/integration_tests.rs @@ -13,6 +13,7 @@ macro_rules! test_with_all_storage_impls { $function(&mut TestsLimiter::new_from_blocking_impl(rate_limiter)).await; } + #[cfg(feature = "disk_storage")] #[tokio::test] async fn [<$function _disk_storage>]() { let dir = TempDir::new().expect("We should have a dir!"); @@ -88,11 +89,13 @@ mod test { use self::limitador::RateLimiter; use crate::helpers::tests_limiter::*; use limitador::limit::Limit; + #[cfg(feature = "disk_storage")] use limitador::storage::disk::{DiskStorage, OptimizeFor}; use limitador::storage::in_memory::InMemoryStorage; use std::collections::{HashMap, HashSet}; use std::thread::sleep; use std::time::Duration; + #[cfg(feature = "disk_storage")] use tempfile::TempDir; test_with_all_storage_impls!(get_namespaces);