Move history persistence off Tokio workers #8

Merged
sjg merged 1 commits from fix/nonblocking-history-persistence into main 2026-08-01 01:43:47 +02:00
Showing only changes of commit 8a3c426a93 - Show all commits
+22 -4
View File
@@ -185,20 +185,38 @@ pub fn flush_all(db: &mut PickleDb, rig_id: &str, histories: &Arc<DecoderHistori
let _ = db.dump();
}
fn flush_all_rigs(db: &Mutex<PickleDb>, rig_histories: &[(String, Arc<DecoderHistories>)]) {
let Ok(mut guard) = db.lock() else {
tracing::warn!("history database mutex poisoned; skipping periodic flush");
return;
};
for (rig_id, histories) in rig_histories {
flush_all(&mut guard, rig_id, histories);
}
}
/// Spawn a Tokio task that flushes all rigs' histories to disk every 60 seconds.
///
/// Snapshot cloning, JSON serialization, and disk I/O run on Tokio's blocking
/// pool so a large history database cannot stall an async runtime worker.
pub fn spawn_flush_task(
db: Arc<Mutex<PickleDb>>,
rig_histories: Vec<(String, Arc<DecoderHistories>)>,
) {
tokio::spawn(async move {
let rig_histories = Arc::new(rig_histories);
let mut interval = tokio::time::interval(Duration::from_secs(60));
interval.tick().await; // consume the immediate first tick
loop {
interval.tick().await;
if let Ok(mut guard) = db.lock() {
for (rig_id, histories) in &rig_histories {
flush_all(&mut guard, rig_id, histories);
}
let db = Arc::clone(&db);
let rig_histories = Arc::clone(&rig_histories);
if let Err(err) = tokio::task::spawn_blocking(move || {
flush_all_rigs(&db, rig_histories.as_slice());
})
.await
{
tracing::warn!(error = %err, "history flush worker failed");
}
}
});