Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temp fix for issue #312 #313

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading