From af0c229c7bf9116de3c25d279e403c0c422de8b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksandar=20=C4=8Cekrli=C4=87?= Date: Wed, 24 Jul 2024 23:36:58 +0200 Subject: [PATCH] Add a config option to disable libp2p resource limits (#163) --- cmd/node/main.go | 1 + config/config.go | 23 +++++++++++++---------- host/config.go | 9 +++++++++ host/host.go | 10 ++++++++++ 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/cmd/node/main.go b/cmd/node/main.go index 1d521a37..efe1e830 100644 --- a/cmd/node/main.go +++ b/cmd/node/main.go @@ -128,6 +128,7 @@ func run() int { host.WithWebsocket(cfg.Connectivity.Websocket), host.WithWebsocketPort(cfg.Connectivity.WebsocketPort), host.WithMustReachBootNodes(cfg.Connectivity.MustReachBootNodes), + host.WithDisabledResourceLimits(cfg.Connectivity.DisableConnectionLimits), } if !cfg.Connectivity.NoDialbackPeers { diff --git a/config/config.go b/config/config.go index c2967e0a..56d27cad 100644 --- a/config/config.go +++ b/config/config.go @@ -59,16 +59,17 @@ type Log struct { // Connectivity describes the libp2p host that the node will use. type Connectivity struct { - Address string `koanf:"address" flag:"address,a"` - Port uint `koanf:"port" flag:"port,p"` - PrivateKey string `koanf:"private-key" flag:"private-key"` - DialbackAddress string `koanf:"dialback-address" flag:"dialback-address"` - DialbackPort uint `koanf:"dialback-port" flag:"dialback-port"` - Websocket bool `koanf:"websocket" flag:"websocket,w"` - WebsocketPort uint `koanf:"websocket-port" flag:"websocket-port"` - WebsocketDialbackPort uint `koanf:"websocket-dialback-port" flag:"websocket-dialback-port"` - NoDialbackPeers bool `koanf:"no-dialback-peers" flag:"no-dialback-peers"` - MustReachBootNodes bool `koanf:"must-reach-boot-nodes" flag:"must-reach-boot-nodes"` + Address string `koanf:"address" flag:"address,a"` + Port uint `koanf:"port" flag:"port,p"` + PrivateKey string `koanf:"private-key" flag:"private-key"` + DialbackAddress string `koanf:"dialback-address" flag:"dialback-address"` + DialbackPort uint `koanf:"dialback-port" flag:"dialback-port"` + Websocket bool `koanf:"websocket" flag:"websocket,w"` + WebsocketPort uint `koanf:"websocket-port" flag:"websocket-port"` + WebsocketDialbackPort uint `koanf:"websocket-dialback-port" flag:"websocket-dialback-port"` + NoDialbackPeers bool `koanf:"no-dialback-peers" flag:"no-dialback-peers"` + MustReachBootNodes bool `koanf:"must-reach-boot-nodes" flag:"must-reach-boot-nodes"` + DisableConnectionLimits bool `koanf:"disable-connection-limits" flag:"disable-connection-limits"` } type Head struct { @@ -142,6 +143,8 @@ func getFlagDescription(flag string) string { return "start without dialing back peers from previous runs" case "must-reach-boot-nodes": return "halt node if we fail to reach boot nodes on start" + case "disable-connection-limits": + return "disable libp2p connection limits (experimental)" default: return "" } diff --git a/host/config.go b/host/config.go index 5ac81e1c..c1593c9d 100644 --- a/host/config.go +++ b/host/config.go @@ -38,6 +38,7 @@ type Config struct { BootNodesReachabilityCheckInterval time.Duration MustReachBootNodes bool + DisableResourceLimits bool } // WithPrivateKey specifies the private key for the Host. @@ -127,3 +128,11 @@ func WithBootNodesReachabilityInterval(d time.Duration) func(cfg *Config) { cfg.BootNodesReachabilityCheckInterval = d } } + +// WithDisabledResourceLimits allows removing any resource limits set by libp2p. +// WARNING: experimental +func WithDisabledResourceLimits(b bool) func(cfg *Config) { + return func(cfg *Config) { + cfg.DisableResourceLimits = b + } +} diff --git a/host/host.go b/host/host.go index a40a79a9..0afe8bca 100644 --- a/host/host.go +++ b/host/host.go @@ -12,6 +12,7 @@ import ( pubsub "github.com/libp2p/go-libp2p-pubsub" "github.com/libp2p/go-libp2p/core/crypto" "github.com/libp2p/go-libp2p/core/host" + rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager" ma "github.com/multiformats/go-multiaddr" ) @@ -57,6 +58,15 @@ func New(log zerolog.Logger, address string, port uint, options ...func(*Config) libp2p.NATPortMap(), } + if cfg.DisableResourceLimits { + rcmgr, err := rcmgr.NewResourceManager(rcmgr.NewFixedLimiter(rcmgr.InfiniteLimits)) + if err != nil { + return nil, fmt.Errorf("could not create new resource manager: %w", err) + } + + opts = append(opts, libp2p.ResourceManager(rcmgr)) + } + // Read private key, if provided. if cfg.PrivateKey != "" { key, err := readPrivateKey(cfg.PrivateKey)