Sfoglia il codice sorgente

:gear: Set rcmgr limits from maxconns

Also disables it by default

Fixes https://github.com/mudler/edgevpn/issues/12
Ettore Di Giacinto 3 anni fa
parent
commit
e0ccd8c137
2 ha cambiato i file con 64 aggiunte e 15 eliminazioni
  1. 5 4
      cmd/util.go
  2. 59 11
      pkg/config/config.go

+ 5 - 4
cmd/util.go

@@ -207,9 +207,9 @@ var CommonFlags []cli.Flag = []cli.Flag{
 		EnvVar: "LIMITCONFIG",
 	},
 	&cli.BoolFlag{
-		Name:   "limit-disable",
-		Usage:  "Disable resource limit",
-		EnvVar: "LIMITDISABLE",
+		Name:   "limit-enable",
+		Usage:  "Enable resource manager. (Experimental) All options prefixed with limit requires resource manager to be enabled",
+		EnvVar: "LIMITENABLE",
 	},
 	&cli.BoolFlag{
 		Name:   "limit-config-dynamic",
@@ -353,9 +353,10 @@ func cliToOpts(c *cli.Context) ([]node.Option, []vpn.Option, *logger.Logger) {
 			HolePunch:      c.Bool("holepunch"),
 		},
 		Limit: config.ResourceLimit{
-			Disable:     c.Bool("limit-disable"),
+			Enable:      c.Bool("limit-enable"),
 			FileLimit:   c.String("limit-file"),
 			Scope:       c.String("limit-scope"),
+			MaxConns:    c.Int("max-connections"), // Turn to 0 to use other way of limiting. Files take precedence
 			LimitConfig: limitConfig,
 		},
 	}

+ 59 - 11
pkg/config/config.go

@@ -17,6 +17,7 @@ package config
 
 import (
 	"fmt"
+	"math/bits"
 	"os"
 	"time"
 
@@ -61,7 +62,10 @@ type ResourceLimit struct {
 	FileLimit   string
 	LimitConfig *node.NetLimitConfig
 	Scope       string
-	Disable     bool
+	MaxConns    int
+	StaticMin   int64
+	StaticMax   int64
+	Enable      bool
 }
 
 // Ledger is the ledger configuration structure
@@ -201,18 +205,20 @@ func (c Config) ToOpts(l *logger.Logger) ([]node.Option, []vpn.Option, error) {
 		))
 	}
 
-	cm, err := connmanager.NewConnManager(
-		20,
-		c.Connection.MaxConnections,
-		connmanager.WithGracePeriod(80*time.Second),
-	)
-	if err != nil {
-		llger.Fatal("could not create connection manager")
-	}
+	if c.Connection.MaxConnections != 0 {
+		cm, err := connmanager.NewConnManager(
+			1,
+			c.Connection.MaxConnections,
+			connmanager.WithGracePeriod(80*time.Second),
+		)
+		if err != nil {
+			llger.Fatal("could not create connection manager")
+		}
 
-	libp2pOpts = append(libp2pOpts, libp2p.ConnectionManager(cm))
+		libp2pOpts = append(libp2pOpts, libp2p.ConnectionManager(cm))
+	}
 
-	if c.Limit.Disable {
+	if !c.Limit.Enable {
 		libp2pOpts = append(libp2pOpts, libp2p.ResourceManager(network.NullResourceManager))
 	} else {
 		var limiter *rcmgr.BasicLimiter
@@ -228,6 +234,43 @@ func (c Config) ToOpts(l *logger.Logger) ([]node.Option, []vpn.Option, error) {
 			if err != nil {
 				return opts, vpnOpts, err
 			}
+		} else if c.Limit.MaxConns != 0 {
+			min := int64(1 << 30)
+			max := int64(4 << 30)
+			if c.Limit.StaticMin != 0 {
+				min = c.Limit.StaticMin
+			}
+			if c.Limit.StaticMax != 0 {
+				max = c.Limit.StaticMax
+			}
+
+			defaultLimits := rcmgr.DefaultLimits.WithSystemMemory(.125, min, max)
+
+			maxconns := int(c.Limit.MaxConns)
+			if 2*maxconns > defaultLimits.SystemBaseLimit.ConnsInbound {
+				// adjust conns to 2x to allow for two conns per peer (TCP+QUIC)
+				defaultLimits.SystemBaseLimit.ConnsInbound = logScale(2 * maxconns)
+				defaultLimits.SystemBaseLimit.ConnsOutbound = logScale(2 * maxconns)
+				defaultLimits.SystemBaseLimit.Conns = logScale(4 * maxconns)
+
+				defaultLimits.SystemBaseLimit.StreamsInbound = logScale(16 * maxconns)
+				defaultLimits.SystemBaseLimit.StreamsOutbound = logScale(64 * maxconns)
+				defaultLimits.SystemBaseLimit.Streams = logScale(64 * maxconns)
+
+				if 2*maxconns > defaultLimits.SystemBaseLimit.FD {
+					defaultLimits.SystemBaseLimit.FD = logScale(2 * maxconns)
+				}
+
+				defaultLimits.ServiceBaseLimit.StreamsInbound = logScale(8 * maxconns)
+				defaultLimits.ServiceBaseLimit.StreamsOutbound = logScale(32 * maxconns)
+				defaultLimits.ServiceBaseLimit.Streams = logScale(32 * maxconns)
+
+				defaultLimits.ProtocolBaseLimit.StreamsInbound = logScale(8 * maxconns)
+				defaultLimits.ProtocolBaseLimit.StreamsOutbound = logScale(32 * maxconns)
+				defaultLimits.ProtocolBaseLimit.Streams = logScale(32 * maxconns)
+			}
+			limiter = rcmgr.NewStaticLimiter(defaultLimits)
+
 		} else {
 			limiter = rcmgr.NewDefaultLimiter()
 		}
@@ -273,3 +316,8 @@ func (c Config) ToOpts(l *logger.Logger) ([]node.Option, []vpn.Option, error) {
 
 	return opts, vpnOpts, nil
 }
+
+func logScale(val int) int {
+	bitlen := bits.Len(uint(val))
+	return 1 << bitlen
+}