Skip to content

Commit

Permalink
Add a config option to disable libp2p resource limits (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maelkum authored Jul 24, 2024
1 parent 01dc8a2 commit af0c229
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
1 change: 1 addition & 0 deletions cmd/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 13 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 ""
}
Expand Down
9 changes: 9 additions & 0 deletions host/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Config struct {

BootNodesReachabilityCheckInterval time.Duration
MustReachBootNodes bool
DisableResourceLimits bool
}

// WithPrivateKey specifies the private key for the Host.
Expand Down Expand Up @@ -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
}
}
10 changes: 10 additions & 0 deletions host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit af0c229

Please sign in to comment.