From 81aa0ea92fbb9a84b484f9f9cf739047108e7697 Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 1 Dec 2026 05:10:45 +0000 Subject: The document file is written after the document changed, not on a tick Its worker looked every append interval whether something was due. It now writes that long after the document changed, so changes close together go together, and sleeps otherwise; while the node isn't ready or rejoins, when what it writes isn't told by a change of its document alone, it looks every interval as before. --- src/swarm/document_file.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/swarm/document_file.rs b/src/swarm/document_file.rs index 6c71e42..9a515c8 100644 --- a/src/swarm/document_file.rs +++ b/src/swarm/document_file.rs @@ -12,16 +12,32 @@ use crate::document::replica::Epoch; use crate::locks::RwLockExt; use crate::state::AppState; -/// Keeps the document file up to date. Runs for as long as the node does. +/// Keeps the document file up to date: written `append_every` after the document +/// changed, so changes close together go together; and every `append_every` while the +/// node isn't ready or rejoins, when what it writes isn't told by a change of its +/// document alone. Runs for as long as the node does. pub(crate) async fn run(state: AppState) { let Some(file) = state.document_file.clone() else { return; }; - let mut tick = tokio::time::interval(file.append_every()); - tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); + let every = file.append_every(); + let mut changed = state.change_tx.subscribe(); + // First, what the node starts with. + let mut due = Some(tokio::time::Instant::now() + every); loop { - tick.tick().await; - flush(&state).await; + tokio::select! { + () = async { tokio::time::sleep_until(due.unwrap_or_else(tokio::time::Instant::now)).await }, if due.is_some() => { + flush(&state).await; + let settled = state.is_ready().await && !state.remake.is_rejoining(); + due = (!settled).then(|| tokio::time::Instant::now() + every); + } + change = changed.changed() => { + if change.is_err() { + return; + } + due.get_or_insert(tokio::time::Instant::now() + every); + } + } } } -- cgit v1.3.1