p2p: peer exchange to find a region without the DHT, region-tagged peer cache, faster anti-entropy

A node with too few peers in a followed region asks its connected peers who they know there (sync kind "peers"); a peer answers even from another region, since gossip tells it which topics its connections subscribe to. Peers of the region are dialed, others only while the region has none. The peer cache records the regions a peer was seen in and a restart redials those first. Anti-entropy now runs every 10 minutes (plus or minus a fifth) with 3 peers per round instead of every 30 with one. Simulator: a newcomer whose remembered peers were all in another region got 0 of 114 listings with the seeds down, now 56 of 56; repair median at the same interval 25.9 s to 5.7 s.

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-19 00:54:48 +01:00
co-authored by Claude Sonnet 5.5
parent 2e105ef353
commit 59973cbf43
8 changed files with 452 additions and 31 deletions
+22 -14
View File
@@ -98,10 +98,13 @@ type Config struct {
MessageDifficulty int
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 is how often the node syncs with random peers it
// shares a region with, to repair what gossip lost while connected
// (default 10 min, each round a fifth sooner or later so nodes do not
// all ask at once). AntiEntropyPeers is how many peers a round asks
// (default 3). A round with matching digests costs almost nothing.
AntiEntropyInterval time.Duration
AntiEntropyPeers int
// IntakeSummaryEvery is how often the node logs how many listings reached
// it by gossip since the last time (default 1 min; nothing is logged when
// none did).
@@ -176,7 +179,10 @@ func New(ctx context.Context, cfg Config) (*Node, error) {
cfg.RedialInterval = 20 * time.Second
}
if cfg.AntiEntropyInterval <= 0 {
cfg.AntiEntropyInterval = 30 * time.Minute
cfg.AntiEntropyInterval = 10 * time.Minute
}
if cfg.AntiEntropyPeers <= 0 {
cfg.AntiEntropyPeers = 3
}
if cfg.IntakeSummaryEvery <= 0 {
cfg.IntakeSummaryEvery = defaultIntakeSummary
@@ -615,6 +621,8 @@ func (n *Node) subscribeRegion(region string) error {
n.discoverRegion(rctx, rd, region)
}
n.wg.Add(1)
go n.exchangePeers(rctx, region)
n.wg.Add(1)
go func() {
defer n.wg.Done()
n.consume(rctx, sub, func(msg *pubsub.Message) {
@@ -907,19 +915,18 @@ 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.
// the sync on a new connection cannot: every interval (give or take a fifth)
// it syncs with a few random peers that share 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():
d := n.cfg.AntiEntropyInterval
d = d - d/5 + time.Duration(rand.Int64N(int64(d)*2/5+1))
if !sleepCtx(n.ctx, d) {
return
case <-t.C:
n.antiEntropyOnce()
}
n.antiEntropyOnce()
}
}
@@ -937,8 +944,9 @@ func (n *Node) antiEntropyOnce() {
}
}
}
if len(peers) > 0 {
n.syncSoon(peers[rand.IntN(len(peers))])
rand.Shuffle(len(peers), func(i, j int) { peers[i], peers[j] = peers[j], peers[i] })
for _, p := range peers[:min(len(peers), n.cfg.AntiEntropyPeers)] {
n.syncSoon(p)
}
}