Skip to content

Commit

Permalink
fix(network): set infinite limit for resource manager (#1261)
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f authored and themantre committed May 6, 2024
1 parent 87de4b5 commit 49a789a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 34 deletions.
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

0 comments on commit 49a789a

Please sign in to comment.