Skip to content

Commit

Permalink
Accept replicated qualified counters
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsnaps committed May 15, 2024
1 parent a9e5a94 commit 0afcfd1
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions limitador/src/storage/distributed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,12 @@ impl CrInMemoryStorage {
Namespace,
HashMap<Limit, CrCounterValue<String>>,
>::new()));
let qualified_counters = Arc::new(Cache::new(cache_size));
let qualified_counters: Arc<Cache<Counter, Arc<CrCounterValue<String>>>> =
Arc::new(Cache::new(cache_size));

{
let limits_for_namespace = limits_for_namespace.clone();
let qualified_counters = qualified_counters.clone();
tokio::spawn(async move {
let sock = UdpSocket::bind(broadcast).await.unwrap();
sock.set_broadcast(true).unwrap();
Expand All @@ -297,12 +299,21 @@ impl CrInMemoryStorage {
values,
} = message;
let counter = <CounterKey as Into<Counter>>::into(counter_key);
let counters = limits_for_namespace.read().unwrap();
let limits = counters.get(counter.namespace()).unwrap();
let value = limits.get(counter.limit()).unwrap();
value.merge(
(UNIX_EPOCH + Duration::from_secs(expiry), values).into(),
);
if counter.is_qualified() {
if let Some(counter) = qualified_counters.get(&counter) {
counter.merge(
(UNIX_EPOCH + Duration::from_secs(expiry), values)
.into(),
);
}
} else {
let counters = limits_for_namespace.read().unwrap();
let limits = counters.get(counter.namespace()).unwrap();
let value = limits.get(counter.limit()).unwrap();
value.merge(
(UNIX_EPOCH + Duration::from_secs(expiry), values).into(),
);
};
}
Err(err) => {
println!("Error from {} bytes: {:?} \n{:?}", len, err, &buf[..len])
Expand Down

0 comments on commit 0afcfd1

Please sign in to comment.