Skip to content

Commit

Permalink
Reset the data need in deserialized Limits
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsnaps committed Jun 27, 2022
1 parent 3cd00cb commit 0e72581
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 7 deletions.
11 changes: 11 additions & 0 deletions limitador/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ impl Counter {
self.limit.max_value()
}

pub fn update_to_limit(&mut self, limit: &Limit) -> bool {
if limit == &self.limit {
self.limit.set_max_value(limit.max_value());
if let Some(name) = limit.name() {
self.limit.set_name(name.to_string());
}
return true;
}
false
}

pub fn seconds(&self) -> u64 {
self.limit.seconds()
}
Expand Down
1 change: 1 addition & 0 deletions limitador/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ use crate::storage::{AsyncCounterStorage, AsyncStorage, Authorization, CounterSt

#[macro_use]
extern crate lazy_static;
extern crate core;

pub mod counter;
pub mod errors;
Expand Down
8 changes: 6 additions & 2 deletions limitador/src/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ impl From<String> for Namespace {
#[derive(Eq, Debug, Clone, Serialize, Deserialize)]
pub struct Limit {
namespace: Namespace,
// #[serde(skip_serializing)]
#[serde(skip)]
max_value: i64,
seconds: u64,
// #[serde(skip_serializing)]
#[serde(skip)]
name: Option<String>,

// Need to sort to generate the same object when using the JSON as a key or
Expand Down Expand Up @@ -90,6 +90,10 @@ impl Limit {
self.name = Some(name)
}

pub fn set_max_value(&mut self, value: i64) {
self.max_value = value;
}

pub fn conditions(&self) -> HashSet<String> {
self.conditions.iter().map(|cond| cond.into()).collect()
}
Expand Down
2 changes: 1 addition & 1 deletion limitador/src/storage/infinispan/infinispan_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl AsyncCounterStorage for InfinispanStorage {
// unnecessarily.

if let Some(val) = counter_val {
let mut counter: Counter = counter_from_counter_key(&counter_key);
let mut counter: Counter = counter_from_counter_key(&counter_key, &limit);
let ttl = 0; // TODO: calculate TTL from response headers.
counter.set_remaining(val);
counter.set_expires_in(Duration::from_secs(ttl));
Expand Down
14 changes: 12 additions & 2 deletions limitador/src/storage/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,21 @@ pub fn key_for_counters_of_limit(limit: &Limit) -> String {
)
}

pub fn counter_from_counter_key(key: &str) -> Counter {
pub fn counter_from_counter_key(key: &str, limit: &Limit) -> Counter {
let counter_prefix = "counter:";
let start_pos_counter = key.find(counter_prefix).unwrap() + counter_prefix.len();

serde_json::from_str(&key[start_pos_counter..]).unwrap()
let mut counter: Counter = serde_json::from_str(&key[start_pos_counter..]).unwrap();
if !counter.update_to_limit(limit) {
// this means some kind of data corruption _or_ most probably
// an out of sync `impl PartialEq for Limit` vs `pub fn key_for_counter(counter: &Counter) -> String`
panic!(
"Failed to rebuild Counter's Limit from the provided Limit: {:?} vs {:?}",
counter.limit(),
limit
)
}
counter
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion limitador/src/storage/redis/redis_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl AsyncCounterStorage for AsyncRedisStorage {
.await?;

for counter_key in counter_keys {
let mut counter: Counter = counter_from_counter_key(&counter_key);
let mut counter: Counter = counter_from_counter_key(&counter_key, &limit);

// If the key does not exist, it means that the counter expired,
// so we don't have to return it.
Expand Down
2 changes: 1 addition & 1 deletion limitador/src/storage/redis/redis_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl CounterStorage for RedisStorage {
con.smembers::<String, HashSet<String>>(key_for_counters_of_limit(limit))?;

for counter_key in counter_keys {
let mut counter: Counter = counter_from_counter_key(&counter_key);
let mut counter: Counter = counter_from_counter_key(&counter_key, limit);

// If the key does not exist, it means that the counter expired,
// so we don't have to return it.
Expand Down

0 comments on commit 0e72581

Please sign in to comment.