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
+53 -2
View File
@@ -6,6 +6,7 @@ import (
"errors"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"sync"
@@ -29,8 +30,15 @@ type cachedPeer struct {
ID string `json:"id"`
Addrs []string `json:"addrs"`
Seen int64 `json:"seen"` // unix seconds
// Regions are the regions this node followed when it saw that the peer
// subscribed to them, so a restart can redial the peers of its own regions
// first instead of only the most recent ones.
Regions []string `json:"regions,omitempty"`
}
// maxCachedRegions bounds the regions remembered per peer.
const maxCachedRegions = 8
type peerFile struct {
Peers []cachedPeer `json:"peers"`
}
@@ -84,6 +92,30 @@ func writePeerCache(path string, peers []cachedPeer) error {
return os.Rename(tmp.Name(), path)
}
// orderForRedial puts the peers seen in one of regions first, keeping the
// order (newest first) within each group.
func orderForRedial(peers []cachedPeer, regions []string) {
slices.SortStableFunc(peers, func(a, b cachedPeer) int {
return boolInt(sharesRegion(b, regions)) - boolInt(sharesRegion(a, regions))
})
}
func sharesRegion(p cachedPeer, regions []string) bool {
for _, r := range p.Regions {
if slices.Contains(regions, r) {
return true
}
}
return false
}
func boolInt(b bool) int {
if b {
return 1
}
return 0
}
// directAddrs keeps the addresses worth remembering: not relayed, which only
// work while that relay does, and not unspecified.
func directAddrs(addrs []ma.Multiaddr) []string {
@@ -107,12 +139,27 @@ func (n *Node) rememberPeers() {
for _, p := range readPeerCache(n.cfg.PeerCache, now) {
byID[p.ID] = p
}
inRegion := map[peer.ID][]string{}
for _, r := range n.Regions() {
for _, p := range n.RegionPeers(r) {
inRegion[p] = append(inRegion[p], r)
}
}
for _, id := range n.host.Network().Peers() {
addrs := directAddrs(n.host.Peerstore().Addrs(id))
if len(addrs) == 0 {
continue
}
byID[id.String()] = cachedPeer{ID: id.String(), Addrs: addrs, Seen: now.Unix()}
regions := byID[id.String()].Regions
for _, r := range inRegion[id] {
if !slices.Contains(regions, r) {
regions = append(regions, r)
}
}
if len(regions) > maxCachedRegions {
regions = regions[len(regions)-maxCachedRegions:]
}
byID[id.String()] = cachedPeer{ID: id.String(), Addrs: addrs, Seen: now.Unix(), Regions: regions}
}
list := make([]cachedPeer, 0, len(byID))
for _, p := range byID {
@@ -149,7 +196,11 @@ func (n *Node) dialCached() {
}
var wg sync.WaitGroup
dialed := 0
for _, p := range readPeerCache(n.cfg.PeerCache, n.clock()) {
cached := readPeerCache(n.cfg.PeerCache, n.clock())
// Peers seen in a region we follow come first (the newest of each group first,
// as the file is sorted), then the rest.
orderForRedial(cached, n.Regions())
for _, p := range cached {
if dialed == dialCachedMax {
break
}