diff --git a/network/network.go b/network/network.go index 3a372b808..20aaf435f 100644 --- a/network/network.go +++ b/network/network.go @@ -95,11 +95,11 @@ func newNetwork(networkName string, conf *Config, opts []lp2p.Option) (*network, return nil, LibP2PError{Err: err} } - maxconns := conf.MaxConns + maxConns := conf.MaxConns changes := lp2prcmgr.PartialLimitConfig{} - changes.System.ConnsInbound = lp2prcmgr.LimitVal(2 * maxconns) - changes.System.ConnsOutbound = lp2prcmgr.LimitVal(2 * maxconns) - changes.System.Conns = lp2prcmgr.LimitVal(4 * maxconns) + changes.System.ConnsInbound = lp2prcmgr.LimitVal(logScale(maxConns)) + changes.System.ConnsOutbound = lp2prcmgr.LimitVal(logScale(maxConns)) + changes.System.Conns = lp2prcmgr.LimitVal(logScale(2 * maxConns)) limit := changes.Build(lp2prcmgr.DefaultLimits.AutoScale()) resMgr, err := lp2prcmgr.NewResourceManager( diff --git a/network/utils.go b/network/utils.go index 6df001764..b56bcfb33 100644 --- a/network/utils.go +++ b/network/utils.go @@ -2,6 +2,7 @@ package network import ( "context" + "math/bits" "time" lp2phost "github.com/libp2p/go-libp2p/core/host" @@ -57,3 +58,8 @@ func ConnectAsync(ctx context.Context, h lp2phost.Host, addrInfo lp2ppeer.AddrIn } }() } + +func logScale(val int) int { + bitlen := bits.Len(uint(val)) + return 1 << bitlen +}