diff --git a/Cargo.lock b/Cargo.lock index 9b745f49..3e5dd699 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1306,9 +1306,9 @@ dependencies = [ [[package]] name = "infinispan" -version = "0.1.0" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7902811a7297306b68a314cb9754bec7f1a3836c2048561ac5f5a2174544b5a5" +checksum = "3de3024babdcbb9928214d1870b41711a7cfa49c1483468874e0c3d881315bed" dependencies = [ "base64 0.13.0", "http", diff --git a/limitador/Cargo.toml b/limitador/Cargo.toml index ce3ba8c7..0f9a14a0 100644 --- a/limitador/Cargo.toml +++ b/limitador/Cargo.toml @@ -30,7 +30,7 @@ lazy_static = "1.4" redis = { version = "0.17", optional = true, features = ["connection-manager"] } r2d2 = { version = "0.8", optional = true } tokio = { version = "0.2", optional = true, features = ["rt-core", "macros", "time"] } -infinispan = { version = "0.1", optional = true } +infinispan = { version = "0.2", optional = true } reqwest = { version = "0.10", optional = true } # reqwest dependency. Needed here to avoid problems when compiling diff --git a/limitador/src/storage/infinispan/counters.rs b/limitador/src/storage/infinispan/counters.rs index 2d2c3724..10f93694 100644 --- a/limitador/src/storage/infinispan/counters.rs +++ b/limitador/src/storage/infinispan/counters.rs @@ -74,20 +74,20 @@ pub async fn decrement_by( .await?; if response.status() == 404 { - // TODO: we could use "reset" here, but it's not implemented in the - // client yet. So for now, delete and create. - let _ = infinispan - .run(&request::counters::delete(&counter_key)) + let reset_resp = infinispan + .run(&request::counters::reset(&counter_key)) .await?; - // TODO: the type of counter and its attributes should be configurable. - // For now let's use "weak" counters with default attributes. - let _ = infinispan - .run( - &request::counters::create_weak(&counter_key) - .with_value(create_counter_opts.initial_value - delta), - ) - .await?; + if reset_resp.status() == 404 { + // TODO: the type of counter and its attributes should be configurable. + // For now let's use "weak" counters with default attributes. + let _ = infinispan + .run( + &request::counters::create_weak(&counter_key) + .with_value(create_counter_opts.initial_value - delta), + ) + .await?; + } let _ = infinispan .run( diff --git a/limitador/src/storage/infinispan/infinispan_storage.rs b/limitador/src/storage/infinispan/infinispan_storage.rs index 5928603b..6d561323 100644 --- a/limitador/src/storage/infinispan/infinispan_storage.rs +++ b/limitador/src/storage/infinispan/infinispan_storage.rs @@ -1,6 +1,7 @@ use crate::counter::Counter; use crate::limit::{Limit, Namespace}; use crate::storage::infinispan::counters::CounterOpts; +use crate::storage::infinispan::response::response_to_string; use crate::storage::infinispan::{counters, sets}; use crate::storage::keys::*; use crate::storage::{AsyncStorage, Authorization, StorageErr}; @@ -194,21 +195,12 @@ impl AsyncStorage for InfinispanStorage { } async fn clear(&self) -> Result<(), StorageErr> { - // TODO: Flush the cache instead of deleting and re-creating. There - // isn't a way to do this using the infinispan client for now. - // - // TODO: delete all counters (they don't belong to any Infinispan - // cache). There isn't a way to do this using the client for now. - let _ = self .infinispan - .run(&request::caches::delete(INFINISPAN_LIMITS_CACHE_NAME)) + .run(&request::caches::clear(INFINISPAN_LIMITS_CACHE_NAME)) .await?; - let _ = self - .infinispan - .run(&request::caches::create_local(INFINISPAN_LIMITS_CACHE_NAME)) - .await?; + let _ = self.delete_all_counters().await?; Ok(()) } @@ -247,6 +239,22 @@ impl InfinispanStorage { Ok(()) } + async fn delete_all_counters(&self) -> Result<(), StorageErr> { + let resp = self.infinispan.run(&request::counters::list()).await?; + + let counter_names: HashSet = + serde_json::from_str(&response_to_string(resp).await).unwrap(); + + for counter_name in counter_names { + let _ = self + .infinispan + .run(&request::counters::delete(counter_name)) + .await?; + } + + Ok(()) + } + async fn counter_keys_of_limit( &self, limit: &Limit,