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

Improving perf when using InMemory storage #186

Merged
merged 9 commits into from
Jul 11, 2023
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,7 +24,8 @@
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

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)
} else {
Expand All @@ -35,6 +36,18 @@
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
Loading