p2p: a --public node accepts 1000 inbound connections (--max-conns), instead of libp2p's memory-scaled default

The simulator showed libp2p's default cap (about 190 on 16 GB) stranding newcomers
on a bootstrap node: 12 of 200 and 212 of 400 nodes never found a peer. As
shipped, the same networks now form and deliver 100% up to 600 nodes.

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 22:46:05 +01:00
co-authored by Claude Sonnet 5.5
parent 130ba5dc65
commit 86668baf5a
10 changed files with 170 additions and 53 deletions
+31
View File
@@ -0,0 +1,31 @@
package p2p
import (
"github.com/libp2p/go-libp2p"
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
)
// DefaultPublicConns is how many inbound connections a --public node accepts
// unless told otherwise. libp2p's own cap scales with the machine's memory
// (about 190 on 16 GB, fewer in a small container), which strands newcomers
// once a bootstrap node has that many peers: the simulator showed 12 of 200
// nodes and 212 of 400 never finding a single peer. A connection costs a few
// tens of kilobytes, so 1000 fits a 512 MiB pod.
const DefaultPublicConns = 1000
// connectionLimits is libp2p's resource manager with the connection caps set
// to conns inbound (and twice that in total, which includes the node's own
// dials). Everything else keeps libp2p's defaults, scaled to the machine.
func connectionLimits(conns int) (libp2p.Option, error) {
limits := rcmgr.DefaultLimits
libp2p.SetDefaultServiceLimits(&limits)
cfg := rcmgr.PartialLimitConfig{
System: rcmgr.ResourceLimits{Conns: rcmgr.LimitVal(2 * conns), ConnsInbound: rcmgr.LimitVal(conns)},
Transient: rcmgr.ResourceLimits{Conns: rcmgr.LimitVal(conns), ConnsInbound: rcmgr.LimitVal(conns)},
}
mgr, err := rcmgr.NewResourceManager(rcmgr.NewFixedLimiter(cfg.Build(limits.AutoScale())))
if err != nil {
return nil, err
}
return libp2p.ResourceManager(mgr), nil
}