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
}
+80
View File
@@ -0,0 +1,80 @@
package p2p
import (
"context"
"testing"
"time"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
)
// inboundLimit is the cap on inbound connections the node's resource manager enforces.
func inboundLimit(t *testing.T, n *Node) int {
t.Helper()
var limit int
err := n.host.Network().ResourceManager().ViewSystem(func(s network.ResourceScope) error {
l, ok := s.(rcmgr.ResourceScopeLimiter)
if !ok {
t.Fatalf("the system scope %T exposes no limit", s)
}
limit = l.Limit().GetConnLimit(network.DirInbound)
return nil
})
if err != nil {
t.Fatal(err)
}
return limit
}
func TestAPublicNodeAcceptsManyMoreConnectionsThanLibp2pDoesByDefault(t *testing.T) {
public := startNodeWith(t, nil, func(c *Config) { c.Public = true })
if got := inboundLimit(t, public.Node); got != DefaultPublicConns {
t.Errorf("a --public node accepts %d inbound connections, want %d", got, DefaultPublicConns)
}
private := startNode(t, nil)
if got := inboundLimit(t, private.Node); got == DefaultPublicConns {
t.Errorf("a node that is not --public must keep libp2p's own default, got %d", got)
}
opted := startNodeWith(t, nil, func(c *Config) { c.Public = true; c.MaxConns = -1 })
if got := inboundLimit(t, opted.Node); got == DefaultPublicConns {
t.Errorf("MaxConns -1 must keep libp2p's default even when public, got %d", got)
}
}
func TestTheConnectionLimitIsEnforced(t *testing.T) {
const limit = 3
n := startNodeWith(t, nil, func(c *Config) { c.Public = true; c.MaxConns = limit })
if got := inboundLimit(t, n.Node); got != limit {
t.Fatalf("limit is %d, want %d", got, limit)
}
info, err := peer.AddrInfoFromString(n.Addrs()[0])
if err != nil {
t.Fatal(err)
}
connected := 0
for i := 0; i < 8; i++ {
h, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { h.Close() })
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
if h.Connect(ctx, *info) == nil {
connected++
}
cancel()
}
if connected < limit {
t.Errorf("only %d of the first %d connections were accepted", connected, limit)
}
time.Sleep(500 * time.Millisecond)
if got := len(n.host.Network().Conns()); got > limit {
t.Errorf("the node holds %d connections, over its limit of %d", got, limit)
}
}
+16
View File
@@ -107,6 +107,11 @@ type Config struct {
// Bandwidth, if set, counts the bytes this node sends and receives (the
// simulator uses it; a real node leaves it nil).
Bandwidth metrics.Reporter
// MaxConns is the number of inbound connections the node accepts. Zero
// means DefaultPublicConns for a Public node and libp2p's own default
// (which scales with memory) for any other; a negative number keeps
// libp2p's default even for a Public node.
MaxConns int
// ExtraOptions are added to the libp2p host's options; the simulator uses
// them to put latency and connection loss on the transport.
ExtraOptions []libp2p.Option
@@ -206,6 +211,17 @@ func New(ctx context.Context, cfg Config) (*Node, error) {
}
opts = append(opts, libp2p.AddrsFactory(func([]ma.Multiaddr) []ma.Multiaddr { return announce }))
}
conns := cfg.MaxConns
if conns == 0 && cfg.Public {
conns = DefaultPublicConns
}
if conns > 0 {
limits, err := connectionLimits(conns)
if err != nil {
return nil, fmt.Errorf("connection limits: %w", err)
}
opts = append(opts, limits)
}
opts = append(opts, cfg.ExtraOptions...)
if cfg.Bandwidth != nil {
opts = append(opts, libp2p.BandwidthReporter(cfg.Bandwidth))