use tokio::time::interval instead of sleep, for guaranteed flushes

This commit is contained in:
ppom 2025-05-25 12:00:00 +02:00
commit fe5cd70f7a
No known key found for this signature in database

View file

@ -23,7 +23,7 @@ use serde_json::Value;
use tokio::{
fs::{rename, File},
sync::mpsc,
time::sleep,
time::{interval, sleep, MissedTickBehavior},
};
use tracing::error;
@ -112,8 +112,11 @@ impl Database {
}
pub async fn task(&mut self) {
// FIXME this never flushes if entries keep coming, as receiving an entry cancels the sleep
// replace by tokio::time::Interval::Skip
let mut interval = interval(self.flush_every);
// If we missed a tick, it will tick immediately, then wait
// flush_every for the next tick, resulting in a relaxed interval.
// Hoping this will smooth IO pressure when under heavy load.
interval.set_missed_tick_behavior(MissedTickBehavior::Delay);
loop {
tokio::select! {
entry = self.rx.recv() => {
@ -121,7 +124,7 @@ impl Database {
break;
}
}
_ = sleep(self.flush_every) => {
_ = interval.tick() => {
if !self.flush().await {
break;
}