Skip to content

Commit

Permalink
Apply PR feedback.
Browse files Browse the repository at this point in the history
* rewrote the inter_ourselfs_inner, we now avoid a full clone
* remove unneeded clone of the limits.
* avoid sending a re-sync counter if it has expired.
  • Loading branch information
chirino committed May 24, 2024
1 parent 0f45043 commit 4de6ae0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
15 changes: 6 additions & 9 deletions limitador/src/storage/distributed/cr_counter_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,12 @@ impl<A: Ord> CrCounterValue<A> {
(expiry.into_inner(), map)
}

pub fn into_ourselves_inner(self) -> (SystemTime, A, u64) {
let Self {
ourselves,
max_value: _,
value,
others: _,
expiry,
} = self;
(expiry.into_inner(), ourselves, value.into_inner())
pub fn local_values(&self) -> (SystemTime, &A, u64) {
(
self.expiry.clone().into_inner(),
&self.ourselves,
self.value.load(Ordering::Relaxed),
)
}

fn reset(&self, expiry: SystemTime) {
Expand Down
7 changes: 3 additions & 4 deletions limitador/src/storage/distributed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ impl CrInMemoryStorage {
{
let limits = limits.clone();
tokio::spawn(async move {
let limits = limits.clone();
while let Some(sender) = re_sync_queue_rx.recv().await {
process_re_sync(&limits, sender).await;
}
Expand Down Expand Up @@ -311,11 +310,11 @@ async fn process_re_sync(
let update = {
let limits = limits.read().unwrap();
limits.get(&key).and_then(|store_value| {
let (expiry, ourself, value) = store_value.clone().into_ourselves_inner();
if value == 0 {
let (expiry, ourself, value) = store_value.local_values();
if value == 0 || expiry <= SystemTime::now() {
None // no point in sending a counter that is empty
} else {
let values = HashMap::from([(ourself, value)]);
let values = HashMap::from([(ourself.clone(), value)]);
Some(CounterUpdate {
key: key.clone(),
values,
Expand Down

0 comments on commit 4de6ae0

Please sign in to comment.