aboutsummaryrefslogtreecommitdiffstats
path: root/src/swarm/document_file.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-12-01 05:10:45 +0000
committertv <tv@krebsco.de>2026-12-01 05:10:45 +0000
commit81aa0ea92fbb9a84b484f9f9cf739047108e7697 (patch)
tree9503e0eb5715a3c4e6810e0be04f4578f671500a /src/swarm/document_file.rs
parent45b067620bce84cb1fae440efd7e4de78c6af63e (diff)
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.
Diffstat (limited to 'src/swarm/document_file.rs')
-rw-r--r--src/swarm/document_file.rs26
1 files changed, 21 insertions, 5 deletions
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);
+ }
+ }
}
}