Skip to content

Commit

Permalink
Merge pull request #313 from Kuadrant/issue-312
Browse files Browse the repository at this point in the history
Temp fix for issue #312
  • Loading branch information
alexsnaps authored May 3, 2024
2 parents 43ed429 + c1561c0 commit 0caa044
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
2 changes: 1 addition & 1 deletion limitador-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// check if the real path to the config file changed
// (eg: k8s ConfigMap replacement)
if canonical_limit_file != last_known_canonical_path {
last_known_canonical_path = canonical_limit_file.clone();
last_known_canonical_path.clone_from(&canonical_limit_file);
let limiter = limiter.clone();
handle.spawn(async move {
match limiter.load_limits_from_file(&canonical_limit_file).await {
Expand Down
10 changes: 10 additions & 0 deletions limitador/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ impl Counter {
}
}

#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn key(&self) -> Self {
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 0caa044

Please sign in to comment.