From c7d6d626f7d9cad8c99741dab48559610c32ba7b Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 26 Oct 2026 20:33:29 +0100 Subject: add farewell ping --- src/swarm/peer/keepalive.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'src/swarm/peer/keepalive.rs') diff --git a/src/swarm/peer/keepalive.rs b/src/swarm/peer/keepalive.rs index a593ac9..b22d954 100644 --- a/src/swarm/peer/keepalive.rs +++ b/src/swarm/peer/keepalive.rs @@ -2,6 +2,7 @@ //! documents differ while sync is idle. use std::sync::atomic::Ordering; +use std::time::Duration; use anyhow::Result; use tokio::time::Instant; @@ -23,6 +24,25 @@ pub(crate) async fn send_ping(state: &AppState, peer: &str) -> Result<()> { .await } +/// How long shutting down waits for the last PINGs (`farewell`). +const FAREWELL_TIMEOUT: Duration = Duration::from_secs(2); + +/// At shutdown: one last PING to each neighbour, saying we aren't ready. A neighbour +/// catching up from us picks another source now, instead of once we go dormant. Bounded +/// in time: our Mycelium may be gone already. +pub(crate) async fn farewell(state: &AppState) { + state.stopping.store(true, Ordering::Release); + let mut pings = tokio::task::JoinSet::new(); + for (peer, _) in state.peers_snapshot().await { + if topology::is_neighbour(state, &peer) { + let state = state.clone(); + pings.spawn(async move { send_ping(&state, &peer).await }); + } + } + let all_sent = async { while pings.join_next().await.is_some() {} }; + let _ = tokio::time::timeout(FAREWELL_TIMEOUT, all_sent).await; +} + /// Our keepalive for `peer`. pub(crate) async fn local_keepalive(state: &AppState, peer: &str) -> transport::Keepalive { // Heads and epoch are published together (`Replica::stamps`), so they always match, @@ -48,7 +68,7 @@ pub(crate) async fn local_keepalive(state: &AppState, peer: &str) -> transport:: heads, epoch, epoch_seq, - ready: state.is_ready().await, + ready: state.is_ready().await && !state.stopping.load(Ordering::Acquire), syncs_with_you, } } -- cgit v1.3.1