//! The change publisher: after document changes, which store paths they touched //! (`AppState::change_events`, for the API's streams, the dashboard and remakes), and //! a nudge to topology when the member list changed, and to the peer worker when the //! forget records did. use std::sync::Arc; use tracing::{info, warn}; use crate::document::store::ChangeSet; use crate::state::AppState; pub(crate) async fn run_change_publisher(state: AppState) { let mut changed = state.change_tx.subscribe(); // Where the document stood when last looked at. let mut last = crate::document::passing::mark(&*state.doc.read().await); info!("Starting change publisher"); while changed.changed().await.is_ok() { let before = last.clone(); let doc = Arc::clone(&state.doc); let looked = tokio::task::spawn_blocking(move || { crate::document::passing::changed_since(&doc.blocking_read(), &before) }) .await; let (now, paths, conflicts) = match looked { Ok(looked) => looked, // The runtime is shutting down (the node stops): nothing to publish to. Err(error) if error.is_cancelled() => return, Err(error) => std::panic::resume_unwind(error.into_panic()), }; if now == last { continue; } last = now; for path in &conflicts { warn!(path = ?path, "Concurrent writes produced a conflict"); } if paths .iter() .any(|path| crate::swarm::topology::affects_neighbours(path)) { state.topology_changed.notify_one(); } if paths .iter() .any(|path| crate::swarm::peer::affects_forget_records(path)) { state .forget_records_changed .store(true, std::sync::atomic::Ordering::Release); state.peers_due.now(); } // Fails only when nobody is subscribed. let _ = state.change_events.send(Arc::new(ChangeSet { paths })); } }