feat: set resource limits

This commit is contained in:
Richard Ramos 2023-05-24 11:34:35 -04:00 committed by RichΛrd
parent 96c8980828
commit 39c97c5d89
8 changed files with 106 additions and 22 deletions

View File

@ -174,6 +174,20 @@ var (
Destination: &options.CircuitRelay, Destination: &options.CircuitRelay,
EnvVars: []string{"WAKUNODE2_CIRCUIT_RELAY"}, EnvVars: []string{"WAKUNODE2_CIRCUIT_RELAY"},
}) })
ResourceScalingMemoryPercent = altsrc.NewFloat64Flag(&cli.Float64Flag{
Name: "resource-scaling-memory-percentage",
Usage: "Determines the percentage of total accessible memory that wil be dedicated to go-waku. A dedicated node with a lot of RAM could allocate 25% or more memory to go-waku",
Value: 25,
Destination: &options.ResourceScalingMemoryPercent,
EnvVars: []string{"WAKUNODE2_RESOURCE_MEMORY_PERCENTAGE"},
})
ResourceScalingFDPercent = altsrc.NewFloat64Flag(&cli.Float64Flag{
Name: "resource-scaling-file-descriptors-percentage",
Usage: "Determines the percentage of total file descriptors that wil be dedicated to go-waku.",
Value: 50,
Destination: &options.ResourceScalingFDPercent,
EnvVars: []string{"WAKUNODE2_RESOURCE_FD_PERCENTAGE"},
})
LogLevel = cliutils.NewGenericFlagSingleValue(&cli.GenericFlag{ LogLevel = cliutils.NewGenericFlagSingleValue(&cli.GenericFlag{
Name: "log-level", Name: "log-level",
Aliases: []string{"l"}, Aliases: []string{"l"},

View File

@ -41,6 +41,8 @@ func main() {
ExtMultiaddresses, ExtMultiaddresses,
ShowAddresses, ShowAddresses,
CircuitRelay, CircuitRelay,
ResourceScalingMemoryPercent,
ResourceScalingFDPercent,
LogLevel, LogLevel,
LogEncoding, LogEncoding,
LogOutput, LogOutput,

View File

@ -28,7 +28,7 @@
]; ];
doCheck = false; doCheck = false;
# FIXME: This needs to be manually changed when updating modules. # FIXME: This needs to be manually changed when updating modules.
vendorSha256 = "sha256-o8SeaxD5CDhYqMEr7OtaD0CerwO9HAbA0IJnbDjSLBM="; vendorSha256 = "sha256-bDiX2+o0oXx3KvsolcrQriPYnGzWKY3fz3T+fGtVRpI=";
# Fix for 'nix run' trying to execute 'go-waku'. # Fix for 'nix run' trying to execute 'go-waku'.
meta = { mainProgram = "waku"; }; meta = { mainProgram = "waku"; };
}; };

View File

@ -14,6 +14,9 @@ import (
"syscall" "syscall"
"time" "time"
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
"github.com/pbnjay/memory"
wmetrics "github.com/waku-org/go-waku/waku/v2/metrics" wmetrics "github.com/waku-org/go-waku/waku/v2/metrics"
"github.com/waku-org/go-waku/waku/v2/rendezvous" "github.com/waku-org/go-waku/waku/v2/rendezvous"
@ -62,6 +65,18 @@ func requiresDB(options Options) bool {
return options.Store.Enable || options.Rendezvous.Server return options.Store.Enable || options.Rendezvous.Server
} }
func scalePerc(value float64) float64 {
if value > 100 {
return 100
}
if value < 0.1 {
return 0.1
}
return value
}
const dialTimeout = 7 * time.Second const dialTimeout = 7 * time.Second
// Execute starts a go-waku node with settings determined by the Options parameter // Execute starts a go-waku node with settings determined by the Options parameter
@ -129,6 +144,17 @@ func Execute(options Options) {
} }
libp2pOpts := node.DefaultLibP2POptions libp2pOpts := node.DefaultLibP2POptions
memPerc := scalePerc(options.ResourceScalingMemoryPercent)
fdPerc := scalePerc(options.ResourceScalingFDPercent)
limits := rcmgr.DefaultLimits // Default memory limit: 1/8th of total memory, minimum 128MB, maximum 1GB
scaledLimits := limits.Scale(int64(float64(memory.TotalMemory())*memPerc/100), int(float64(getNumFDs())*fdPerc/100))
resourceManager, err := rcmgr.NewResourceManager(rcmgr.NewFixedLimiter(scaledLimits))
failOnErr(err, "setting resource limits")
libp2pOpts = append(libp2pOpts, libp2p.ResourceManager(resourceManager))
libp2p.SetDefaultServiceLimits(&limits)
if len(options.AdvertiseAddresses) == 0 { if len(options.AdvertiseAddresses) == 0 {
libp2pOpts = append(libp2pOpts, libp2p.NATPortMap()) // Attempt to open ports using uPNP for NATed hosts.) libp2pOpts = append(libp2pOpts, libp2p.NATPortMap()) // Attempt to open ports using uPNP for NATed hosts.)
} }

View File

@ -156,6 +156,8 @@ type Options struct {
AdvertiseAddresses []multiaddr.Multiaddr AdvertiseAddresses []multiaddr.Multiaddr
ShowAddresses bool ShowAddresses bool
CircuitRelay bool CircuitRelay bool
ResourceScalingMemoryPercent float64
ResourceScalingFDPercent float64
LogLevel string LogLevel string
LogEncoding string LogEncoding string
LogOutput string LogOutput string

11
waku/sys_not_unix.go Normal file
View File

@ -0,0 +1,11 @@
//go:build !linux && !darwin && !windows
package waku
import "runtime"
// TODO: figure out how to get the number of file descriptors on Windows and other systems
func getNumFDs() int {
fmt.Println("cannot determine number of file descriptors on ", runtime.GOOS)
return 0
}

18
waku/sys_unix.go Normal file
View File

@ -0,0 +1,18 @@
//go:build linux || darwin
package waku
import (
"fmt"
"golang.org/x/sys/unix"
)
func getNumFDs() int {
var l unix.Rlimit
if err := unix.Getrlimit(unix.RLIMIT_NOFILE, &l); err != nil {
fmt.Println("failed to get fd limit:" + err.Error())
return 0
}
return int(l.Cur)
}

11
waku/sys_windows.go Normal file
View File

@ -0,0 +1,11 @@
//go:build windows
package waku
import (
"math"
)
func getNumFDs() int {
return math.MaxInt
}