p2p: anti-entropy, a periodic sync with one random peer

Repairs what gossip lost while two nodes stay connected; cheap because
matching digests end the round at once.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
2026-11-18 17:33:06 +01:00
co-authored by Claude Sonnet 5.5
parent 11fd2a88c0
commit a54094ea35
3 changed files with 71 additions and 7 deletions
+50 -5
View File
@@ -17,6 +17,7 @@ import (
"errors"
"fmt"
"log/slog"
"math/rand/v2"
"slices"
"sync"
"time"
@@ -89,10 +90,14 @@ type Config struct {
// MessageDifficulty is the proof-of-work bits required of message
// envelopes; it must match the store's.
MessageDifficulty int
SyncPageSize int // records per sync page (default 500)
RedialInterval time.Duration // how often lost bootstrap peers are redialed (default 20s)
Clock func() time.Time // default time.Now
Logger *slog.Logger
SyncPageSize int // records per sync page (default 500)
RedialInterval time.Duration // how often lost bootstrap peers are redialed (default 20s)
// AntiEntropyInterval is how often the node syncs with one random peer
// it shares a region with, to repair what gossip lost while connected
// (default 30 min).
AntiEntropyInterval time.Duration
Clock func() time.Time // default time.Now
Logger *slog.Logger
}
// Node is a running participant.
@@ -140,6 +145,9 @@ func New(ctx context.Context, cfg Config) (*Node, error) {
if cfg.RedialInterval <= 0 {
cfg.RedialInterval = 20 * time.Second
}
if cfg.AntiEntropyInterval <= 0 {
cfg.AntiEntropyInterval = 30 * time.Minute
}
if cfg.RelayBootDelay <= 0 {
cfg.RelayBootDelay = 10 * time.Second
}
@@ -256,7 +264,7 @@ func New(ctx context.Context, cfg Config) (*Node, error) {
// Finding the first peers can take a while (DNS, unreachable servers), so it
// does not hold up starting the node.
n.wg.Add(4)
n.wg.Add(5)
go func() {
defer n.wg.Done()
n.dialBootstrap()
@@ -264,6 +272,7 @@ func New(ctx context.Context, cfg Config) (*Node, error) {
}()
go n.maintain()
go n.redial()
go n.antiEntropy()
go n.remember()
return n, nil
}
@@ -760,6 +769,42 @@ func (n *Node) redial() {
}
}
// antiEntropy repairs what gossip lost while the node stayed connected, which
// the sync on a new connection cannot: every interval it syncs with one
// random peer that shares a region. Matching digests make a round nearly free.
func (n *Node) antiEntropy() {
defer n.wg.Done()
t := time.NewTicker(n.cfg.AntiEntropyInterval)
defer t.Stop()
for {
select {
case <-n.ctx.Done():
return
case <-t.C:
n.antiEntropyOnce()
}
}
}
func (n *Node) antiEntropyOnce() {
n.mu.Lock()
regions := append([]string(nil), n.subscribed...)
n.mu.Unlock()
seen := map[peer.ID]bool{}
var peers []peer.ID
for _, r := range regions {
for _, p := range n.RegionPeers(r) {
if !seen[p] && n.host.Network().Connectedness(p) == network.Connected {
seen[p] = true
peers = append(peers, p)
}
}
}
if len(peers) > 0 {
n.syncSoon(peers[rand.IntN(len(peers))])
}
}
// maintain periodically drops expired data.
func (n *Node) maintain() {
defer n.wg.Done()