Skip to content

Commit

Permalink
Temp fix for issue #312
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsnaps committed May 2, 2024
1 parent 43ed429 commit 7af607e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
9 changes: 9 additions & 0 deletions limitador/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ impl Counter {
}
}

pub(crate) fn key(&self) -> Self {

Check failure on line 43 in limitador/src/counter.rs

View workflow job for this annotation

GitHub Actions / Build for WASM

method `key` is never used

Check failure on line 43 in limitador/src/counter.rs

View workflow job for this annotation

GitHub Actions / Build for WASM

method `key` is never used
Self {
limit: self.limit.clone(),
set_variables: self.set_variables.clone(),
remaining: None,
expires_in: None,
}
}

pub fn limit(&self) -> &Limit {
&self.limit
}
Expand Down
30 changes: 25 additions & 5 deletions limitador/src/storage/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@ use crate::counter::Counter;
use crate::limit::Limit;

pub fn key_for_counter(counter: &Counter) -> String {
format!(
"{},counter:{}",
prefix_for_namespace(counter.namespace().as_ref()),
serde_json::to_string(counter).unwrap()
)
if counter.remaining().is_some() || counter.expires_in().is_some() {
format!(
"{},counter:{}",
prefix_for_namespace(counter.namespace().as_ref()),
serde_json::to_string(&counter.key()).unwrap()
)
} else {
format!(
"{},counter:{}",
prefix_for_namespace(counter.namespace().as_ref()),
serde_json::to_string(counter).unwrap()
)
}
}

pub fn key_for_counters_of_limit(limit: &Limit) -> String {
Expand Down Expand Up @@ -79,6 +87,7 @@ mod tests {
use crate::counter::Counter;
use crate::Limit;
use std::collections::HashMap;
use std::time::Duration;

#[test]
fn key_for_limit_format() {
Expand All @@ -104,6 +113,17 @@ mod tests {
let prefix = prefix_for_namespace(namespace);
assert_eq!(&raw[0..prefix.len()], &prefix);
}

#[test]
fn counter_key_does_not_include_transient_state() {
let namespace = "ns_counter:";
let limit = Limit::new(namespace, 1, 1, vec!["req.method == 'GET'"], vec!["app_id"]);
let counter = Counter::new(limit.clone(), HashMap::default());
let mut other = counter.clone();
other.set_remaining(123);
other.set_expires_in(Duration::from_millis(456));
assert_eq!(key_for_counter(&counter), key_for_counter(&other));
}
}

#[cfg(feature = "disk_storage")]
Expand Down

0 comments on commit 7af607e

Please sign in to comment.