Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a config option to disable libp2p resource limits #163

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading