Skip to content

Commit

Permalink
Use interval instead of manual duration calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsnaps committed Mar 25, 2024
1 parent 57d8f81 commit eab1166
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions limitador/src/storage/redis/redis_cached.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,20 +225,17 @@ impl CachedRedisStorage {
let batcher = Arc::new(Mutex::new(Default::default()));
if let Some(flushing_period) = flushing_period {
let batcher_flusher = batcher.clone();
let mut interval = tokio::time::interval(flushing_period);
tokio::spawn(async move {
loop {
let time_start = Instant::now();
let counters = {
let mut batch = batcher_flusher.lock().unwrap();
std::mem::take(&mut *batch)
};
for (counter, delta) in counters {
storage.update_counter(&counter, delta).await.unwrap();
}
let sleep_time = flushing_period
.checked_sub(time_start.elapsed())
.unwrap_or_else(|| Duration::from_secs(0));
tokio::time::sleep(sleep_time).await;
interval.tick().await;
}
});
}
Expand Down

0 comments on commit eab1166

Please sign in to comment.