Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(network): set infinite limit for resource manager #1261

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,21 @@ func newNetwork(conf *Config, log *logger.SubLogger, opts []lp2p.Option) (*netwo
opts = append(opts, lp2p.DisableMetrics())
}

limit := BuildConcreteLimitConfig(conf.MaxConns)
resMgr, err := lp2prcmgr.NewResourceManager(
lp2prcmgr.NewFixedLimiter(limit),
lp2prcmgr.NewFixedLimiter(lp2prcmgr.InfiniteLimits),
rcMgrOpt...,
)
if err != nil {
return nil, LibP2PError{Err: err}
}

// https://github.com/libp2p/go-libp2p/issues/2616
// The connection manager doesn't reject any connections.
// It just triggers a pruning run once the high watermark is reached (or surpassed).
// https://docs.libp2p.io/concepts/security/dos-mitigation/#limit-the-number-of-connections-your-application-needs
// The ConnManager is in charge of pruning connections to stay below the defined high watermark,
// in contrast, the Resource Manager represents a hard limit where connections will fail to
// be created in the first place once we’ve reached our limits.
//
lowWM := conf.MinConns() // Low Watermark, ex: 14 (if max_conn = 64)
highWM := conf.MaxConns + conf.MinConns() // High Watermark, ex: 78 (if max_conn = 64)
lowWM := conf.MaxConns // Low Watermark, ex: 64 (if max_conn = 64)
highWM := conf.MaxConns + (conf.MaxConns / 4) // High Watermark, ex: 80 (if max_conn = 64)
connMgr, err := lp2pconnmgr.NewConnManager(
lowWM, highWM,
lp2pconnmgr.WithGracePeriod(time.Minute),
Expand Down
27 changes: 0 additions & 27 deletions network/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
lp2phost "github.com/libp2p/go-libp2p/core/host"
lp2pnetwork "github.com/libp2p/go-libp2p/core/network"
lp2ppeer "github.com/libp2p/go-libp2p/core/peer"
lp2prcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
lp2pswarm "github.com/libp2p/go-libp2p/p2p/net/swarm"
"github.com/multiformats/go-multiaddr"
"github.com/pactus-project/pactus/crypto/hash"
Expand Down Expand Up @@ -135,32 +134,6 @@ func SubnetsToFilters(subnets []*net.IPNet, action multiaddr.Action) *multiaddr.
return filters
}

func BuildConcreteLimitConfig(maxConns int) lp2prcmgr.ConcreteLimitConfig {
changes := lp2prcmgr.PartialLimitConfig{}

updateResourceLimits := func(limit *lp2prcmgr.ResourceLimits, maxConns int, coefficient float32) {
maxConnVal := lp2prcmgr.LimitVal(int(float32(maxConns) * coefficient))

limit.ConnsInbound = maxConnVal
limit.ConnsOutbound = maxConnVal
limit.Conns = maxConnVal
limit.StreamsInbound = maxConnVal * 16
limit.StreamsOutbound = maxConnVal * 16
limit.Streams = maxConnVal * 16
}

updateResourceLimits(&changes.System, maxConns, 1)
updateResourceLimits(&changes.ServiceDefault, maxConns, 1)
updateResourceLimits(&changes.ProtocolDefault, maxConns, 1)
updateResourceLimits(&changes.ProtocolPeerDefault, maxConns, 1)
updateResourceLimits(&changes.Transient, maxConns, 0.5)

defaultLimitConfig := lp2prcmgr.DefaultLimits.AutoScale()
changedLimitConfig := changes.Build(defaultLimitConfig)

return changedLimitConfig
}

func MessageIDFunc(m *lp2pspb.Message) string {
h := hash.CalcHash(m.Data)

Expand Down
Loading