diff --git a/limitador/src/limit.rs b/limitador/src/limit.rs index 12adb7ff..963daa34 100644 --- a/limitador/src/limit.rs +++ b/limitador/src/limit.rs @@ -28,7 +28,7 @@ mod deprecated { #[cfg(feature = "lenient_conditions")] pub use deprecated::check_deprecated_syntax_usages_and_reset; -#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)] +#[derive(Debug, Hash, Eq, PartialEq, Clone, PartialOrd, Ord, Serialize, Deserialize)] pub struct Namespace(String); impl From<&str> for Namespace { @@ -49,7 +49,7 @@ impl From for Namespace { } } -#[derive(Eq, Debug, Clone, Serialize, Deserialize)] +#[derive(Eq, Debug, Clone, PartialOrd, Ord, Serialize, Deserialize)] pub struct Limit { namespace: Namespace, #[serde(skip_serializing, default)] @@ -60,13 +60,11 @@ pub struct Limit { // Need to sort to generate the same object when using the JSON as a key or // value in Redis. - #[serde(serialize_with = "ordered_condition_set")] - conditions: HashSet, - #[serde(serialize_with = "ordered_set")] - variables: HashSet, + conditions: BTreeSet, + variables: BTreeSet, } -#[derive(Deserialize, Serialize, PartialEq, Eq, Debug, Clone, Hash)] +#[derive(Deserialize, Serialize, PartialEq, Eq, Debug, Clone, Hash, PartialOrd, Ord)] #[serde(try_from = "String", into = "String")] pub struct Condition { var_name: String, @@ -265,7 +263,7 @@ impl From for String { } } -#[derive(PartialEq, Eq, Debug, Clone, Hash)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Hash)] pub enum Predicate { Equal, NotEqual, diff --git a/limitador/src/storage/in_memory.rs b/limitador/src/storage/in_memory.rs index a784cc71..48872c33 100644 --- a/limitador/src/storage/in_memory.rs +++ b/limitador/src/storage/in_memory.rs @@ -3,14 +3,14 @@ use crate::limit::{Limit, Namespace}; use crate::storage::atomic_expiring_value::AtomicExpiringValue; use crate::storage::{Authorization, CounterStorage, StorageErr}; use moka::sync::Cache; -use std::collections::hash_map::Entry; -use std::collections::{HashMap, HashSet}; +use std::collections::{BTreeMap, HashMap, HashSet}; +use std::collections::btree_map::Entry; use std::ops::Deref; use std::sync::{Arc, RwLock}; use std::time::{Duration, SystemTime}; pub struct InMemoryStorage { - simple_limits: RwLock>, + simple_limits: RwLock>, qualified_counters: Cache>, } @@ -197,7 +197,7 @@ impl CounterStorage for InMemoryStorage { impl InMemoryStorage { pub fn new(cache_size: u64) -> Self { Self { - simple_limits: RwLock::new(HashMap::new()), + simple_limits: RwLock::new(BTreeMap::new()), qualified_counters: Cache::new(cache_size), } }