Skip to content

Commit

Permalink
Merge pull request #186 from Kuadrant/faster-baby-ii
Browse files Browse the repository at this point in the history
 Improving perf when using InMemory storage
  • Loading branch information
didierofrivia authored Jul 11, 2023
2 parents ea8d9a4 + 326986b commit 1a457d7
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 130 deletions.
4 changes: 3 additions & 1 deletion .idea/limitador.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions limitador/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,10 @@ impl Hash for Counter {
fn hash<H: Hasher>(&self, state: &mut H) {
self.limit.hash(state);

let mut encoded_vars = self
.set_variables
.iter()
.map(|(k, v)| k.to_owned() + ":" + v)
.collect::<Vec<String>>();

encoded_vars.sort();
encoded_vars.hash(state);
self.set_variables.iter().for_each(|(k, v)| {
k.hash(state);
v.hash(state);
});
}
}

Expand Down
20 changes: 2 additions & 18 deletions limitador/src/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,24 +406,8 @@ impl Hash for Limit {
fn hash<H: Hasher>(&self, state: &mut H) {
self.namespace.hash(state);
self.seconds.hash(state);

let mut encoded_conditions = self
.conditions
.iter()
.map(|c| c.clone().into())
.collect::<Vec<String>>();

encoded_conditions.sort();
encoded_conditions.hash(state);

let mut encoded_vars = self
.variables
.iter()
.map(|c| c.to_string())
.collect::<Vec<String>>();

encoded_vars.sort();
encoded_vars.hash(state);
self.conditions.iter().for_each(|e| e.hash(state));
self.variables.iter().for_each(|e| e.hash(state));
}
}

Expand Down
1 change: 0 additions & 1 deletion limitador/src/storage/disk/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::storage::StorageErr;

mod expiring_value;
mod rocksdb_storage;

pub use rocksdb_storage::RocksDbStorage as DiskStorage;
Expand Down
2 changes: 1 addition & 1 deletion limitador/src/storage/disk/rocksdb_storage.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::counter::Counter;
use crate::limit::Limit;
use crate::storage::disk::expiring_value::ExpiringValue;
use crate::storage::disk::OptimizeFor;
use crate::storage::expiring_value::ExpiringValue;
use crate::storage::keys::bin::{
key_for_counter, partial_counter_from_counter_key, prefix_for_namespace,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl ExpiringValue {
self.value_at(SystemTime::now())
}

#[must_use]
pub fn update(self, delta: i64, ttl: u64, now: SystemTime) -> Self {

Check warning on line 28 in limitador/src/storage/expiring_value.rs

View workflow job for this annotation

GitHub Actions / Build for WASM

methods `update` and `merge` are never used
let expiry = if self.expiry <= now {
now + Duration::from_secs(ttl)
Expand All @@ -35,6 +36,18 @@ impl ExpiringValue {
Self { value, expiry }
}

pub fn update_mut(&mut self, delta: i64, ttl: u64, now: SystemTime) {
let expiry = if self.expiry <= now {
now + Duration::from_secs(ttl)
} else {
self.expiry
};

self.value = self.value_at(now) + delta;
self.expiry = expiry;
}

#[must_use]
pub fn merge(self, other: ExpiringValue, now: SystemTime) -> Self {
if self.expiry > now {
ExpiringValue {
Expand Down
Loading

0 comments on commit 1a457d7

Please sign in to comment.