Skip to content

Commit

Permalink
go/p2p: Increase incoming connection limit for seed nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
peternose committed Nov 21, 2023
1 parent a5bec7a commit 70b8787
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions .changelog/5456.bugfix.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/p2p: Increase incoming connection limit for seed nodes
40 changes: 40 additions & 0 deletions go/p2p/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
"github.com/libp2p/go-libp2p/p2p/net/conngater"
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
"github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -36,6 +38,12 @@ type HostConfig struct {
func NewHost(cfg *HostConfig) (host.Host, *conngater.BasicConnectionGater, error) {
id := api.SignerToPrivKey(cfg.Signer)

// Set up a resource manager so that we can reserve more resources.
rm, err := NewResourceManager()
if err != nil {
return nil, nil, err
}

// Set up a connection manager so we can limit the number of connections.
cm, err := NewConnManager(&cfg.ConnManagerConfig)
if err != nil {
Expand All @@ -52,6 +60,7 @@ func NewHost(cfg *HostConfig) (host.Host, *conngater.BasicConnectionGater, error
libp2p.UserAgent(cfg.UserAgent),
libp2p.ListenAddrs(cfg.ListenAddr),
libp2p.Identity(id),
libp2p.ResourceManager(rm),
libp2p.ConnectionManager(cm),
libp2p.ConnectionGater(cg),
)
Expand Down Expand Up @@ -197,3 +206,34 @@ func (cfg *ConnGaterConfig) Load() error {

return nil
}

// NewResourceManager constructs a new resource manager.
func NewResourceManager() (network.ResourceManager, error) {
// Use the default resource manager for non-seed nodes.
if config.GlobalConfig.Mode != config.ModeSeed {
return nil, nil
}

// Tweak limits for seed nodes.
//
// Note: The connection manager will trim connections when the total number of inbound and
// outbound connections exceeds the high watermark (default set to 130). Using autoscaling
// and configuring the default limit to 128 seems to be a prudent choice.
defaultLimits := rcmgr.DefaultLimits
defaultLimits.SystemBaseLimit.ConnsInbound = 128
defaultLimits.SystemBaseLimit.StreamsInbound = 128 * 16
defaultLimits.SystemLimitIncrease.ConnsInbound = 128
defaultLimits.SystemLimitIncrease.StreamsInbound = 128 * 16

// Add limits around included libp2p protocols.
libp2p.SetDefaultServiceLimits(&defaultLimits)

// Scale limits.
scaledLimits := defaultLimits.AutoScale()

// The resource manager expects a limiter, se we create one from our limits.
limiter := rcmgr.NewFixedLimiter(scaledLimits)

// Initialize the resource manager.
return rcmgr.NewResourceManager(limiter)
}

0 comments on commit 70b8787

Please sign in to comment.