From 5cd26d321b706113afb8431aba35ab8fb32281c4 Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 3 Nov 2026 02:43:15 +0100 Subject: announcements answered only if heads differ --- src/swarm/peer/inbound.rs | 9 ++++++++- src/swarm/peer/keepalive.rs | 15 +++++++++++++++ src/swarm/peer/mod.rs | 2 +- src/swarm/relay.rs | 11 ++++++----- 4 files changed, 30 insertions(+), 7 deletions(-) (limited to 'src/swarm') diff --git a/src/swarm/peer/inbound.rs b/src/swarm/peer/inbound.rs index 18d29d5..1454c3e 100644 --- a/src/swarm/peer/inbound.rs +++ b/src/swarm/peer/inbound.rs @@ -143,8 +143,15 @@ pub(crate) async fn handle_message( crate::swarm::blob::handle(state, &peer, blob).await; } transport::ParsedMessage::Status(remote) => { - status_received(state, &peer).await; + // An announcement is no keepalive; it is answered only if our heads differ, + // so the announcer learns what we have that it lacks. + if !remote.announcing { + status_received(state, &peer).await; + } let local = on_status(state, &peer, &remote, reactivated).await; + if remote.announcing && local.heads == remote.heads { + return Ok(()); + } if let Err(error) = state .transport .send_message(&peer, &ParsedMessage::StatusReply(local)) diff --git a/src/swarm/peer/keepalive.rs b/src/swarm/peer/keepalive.rs index 8597dcd..196b8b6 100644 --- a/src/swarm/peer/keepalive.rs +++ b/src/swarm/peer/keepalive.rs @@ -32,6 +32,20 @@ pub(crate) async fn send_status(state: &AppState, peer: &str) -> Result<()> { .await } +/// Sends `peer` a STATUS announcing our new heads (`swarm::relay`): no keepalive, so it +/// leaves the keepalives' schedule as it is, and it is answered only if the peer's heads +/// differ (`Status::announcing`). +pub(crate) async fn announce_heads(state: &AppState, peer: &str) -> Result<()> { + let status = transport::Status { + announcing: true, + ..local_status(state, peer).await + }; + state + .transport + .send_message(peer, &ParsedMessage::Status(status)) + .await +} + /// Sends `peer` a PING (the paths layer measures the path with it). pub(crate) async fn send_ping(state: &AppState, peer: &str) -> Result<()> { let nonce = crate::transport::paths::random_nonce(); @@ -173,6 +187,7 @@ pub(crate) async fn local_status(state: &AppState, peer: &str) -> transport::Sta ready: state.is_ready().await && !state.is_closing(), syncs_with_you, leaving: state.is_closing(), + announcing: false, } } diff --git a/src/swarm/peer/mod.rs b/src/swarm/peer/mod.rs index cf427a2..1826ec6 100644 --- a/src/swarm/peer/mod.rs +++ b/src/swarm/peer/mod.rs @@ -26,6 +26,6 @@ pub(crate) use handshake::{ send_hello_once, }; pub(crate) use inbound::handle_message; -pub(crate) use keepalive::{announce, farewell, send_status}; +pub(crate) use keepalive::{announce, announce_heads, farewell, send_status}; pub(crate) use lanes::SyncLanes; pub(crate) use worker::{Due, run_peer_worker}; diff --git a/src/swarm/relay.rs b/src/swarm/relay.rs index 1393d50..a4a4034 100644 --- a/src/swarm/relay.rs +++ b/src/swarm/relay.rs @@ -2,8 +2,9 @@ //! //! A node pushes its own changes to its neighbours, but nothing it receives goes any //! further by itself. So after applying changes from one neighbour, a node *announces* -//! its new heads to its other neighbours with a STATUS (a keepalive: heads, about 100 -//! bytes, not the changes). A neighbour that sees heads it lacks *pulls*: it enters the +//! its new heads to its other neighbours with a STATUS (heads, about 100 bytes, not the +//! changes; no keepalive, and answered only by a neighbour whose heads differ). A +//! neighbour that sees heads it lacks *pulls*: it enters the //! announced heads as the peer's heads in its sync state, Automerge asks for what's //! missing (`need`), and the announcer answers with exactly those changes. Each node //! receives a change about once; the rest is announcements. Regular keepalives work the @@ -11,7 +12,7 @@ //! //! Announcements are coalesced: the first applied change schedules one announcement //! round `ANNOUNCE_DELAY` later, and every change applied until then rides along. A -//! round costs one STATUS and one STATUS_REPLY per neighbour (plus the pulls), whatever the +//! round costs one STATUS per neighbour (plus the pulls and their replies), whatever the //! write rate; the delay is the price per hop. Without coalescing, every applied sync //! message would announce to every neighbour, and announcements would outnumber the //! data they announce (about 15 to 1 at 250 nodes). @@ -31,7 +32,7 @@ use tracing::debug; use crate::locks::MutexExt; use crate::state::{AppState, Standing}; -use crate::swarm::peer::send_status; +use crate::swarm::peer::announce_heads; use crate::swarm::topology; use crate::transport::{self, HeadsSummary}; @@ -86,7 +87,7 @@ async fn announce_now(state: &AppState, senders: &HashSet) { } state.stats.announce_round(); for peer in neighbours { - if let Err(error) = send_status(state, &peer).await { + if let Err(error) = announce_heads(state, &peer).await { debug!(peer = %peer, error = %format!("{error:#}"), "Announcing our heads failed"); } } -- cgit v1.3.1