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
+7 -6
View File
@@ -78,7 +78,7 @@ type sim struct {
timeout time.Duration
made []*listing.Listing
mu sync.Mutex // guards nodes against the sampler
seedConns int // raised connection limit of the seeds; 0 keeps libp2p's default
seedConns int // the seeds' MaxConns setting
}
func main() {
@@ -95,7 +95,7 @@ func main() {
timeout := flag.Duration("timeout", 60*time.Second, "how long to wait for each thing to spread")
latency := flag.Duration("latency", 0, "delay added to every write on connections a node dials (one direction), with up to 50% jitter")
lifetime := flag.Duration("lifetime", 0, "mean lifetime of a dialed connection before it is cut (0: never); redials and resyncs follow")
seedConns := flag.Int("seed-conns", 0, "raise the seeds' inbound connection limit to this many (0: libp2p's default, which scales with RAM)")
seedConns := flag.Int("seed-conns", 0, "the seeds' inbound connection limit (0: the node's default for --public, 1000; -1: libp2p's own, which scales with RAM)")
jsonOut := flag.String("json", "", "write the run (samples every second, phases, arrival times) to this JSON file, for graphs")
hostile := flag.Bool("hostile", false, "also run attackers: invalid listings, a message flood")
killSeeds := flag.Bool("kill-seeds", false, "also shut the seeds down and see what the network and newcomers do")
@@ -336,16 +336,17 @@ func (s *sim) start(n *simNode) error {
if bootstrap == nil {
bootstrap = s.seedAddrs
}
extra := s.extra
if n.seed && s.seedConns > 0 {
extra = append(append([]libp2p.Option(nil), extra...), seedLimits(s.seedConns))
maxConns := 0 // the node's own default: DefaultPublicConns for a seed
if n.seed {
maxConns = s.seedConns
}
node, err := p2p.New(s.ctx, p2p.Config{
Device: n.dev,
Store: n.st,
ListenAddrs: []string{"/ip4/127.0.0.1/tcp/0"},
Bootstrap: bootstrap,
ExtraOptions: extra,
MaxConns: maxConns,
ExtraOptions: s.extra,
Regions: n.regions,
DHT: true,
Public: n.seed,
-16
View File
@@ -15,7 +15,6 @@ import (
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
ma "github.com/multiformats/go-multiaddr"
@@ -79,21 +78,6 @@ func (l *lossyConn) Write(p []byte) (int, error) {
return l.Conn.Write(p)
}
// seedLimits lifts libp2p's connection limits (which scale with the machine's
// memory, and by default allow only about 190 inbound connections on 16 GB of
// RAM) so a run can show what they do to a network of a few hundred nodes.
func seedLimits(conns int) libp2p.Option {
limits := rcmgr.DefaultLimits
libp2p.SetDefaultServiceLimits(&limits)
cfg := rcmgr.PartialLimitConfig{
System: rcmgr.ResourceLimits{Conns: rcmgr.LimitVal(conns * 2), 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())))
must(err)
return libp2p.ResourceManager(mgr)
}
// --- attackers ----------------------------------------------------------------------------
// attacker is a bare libp2p and gossipsub peer without any of the node's