From 3bb2ecf4efec8e4e0d97d260f4ec9f7842f5dd31 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Mon, 7 Feb 2022 18:39:21 +0530 Subject: [PATCH] initialize the resource manager --- core/node/groups.go | 9 +++++---- core/node/libp2p/rcmgr.go | 40 +++++++++++++++++++++++++++++++++++++++ go.mod | 1 + 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 core/node/libp2p/rcmgr.go diff --git a/core/node/groups.go b/core/node/groups.go index 80367156e55..3baea1aa8e0 100644 --- a/core/node/groups.go +++ b/core/node/groups.go @@ -112,10 +112,10 @@ func LibP2P(bcfg *BuildCfg, cfg *config.Config) fx.Option { } // If `cfg.Swarm.DisableRelay` is set and `Network.RelayTransport` isn't, use the former. - enableRelayTransport := cfg.Swarm.Transports.Network.Relay.WithDefault(!cfg.Swarm.DisableRelay) //nolint + enableRelayTransport := cfg.Swarm.Transports.Network.Relay.WithDefault(!cfg.Swarm.DisableRelay) // nolint // Warn about a deprecated option. - //nolint + // nolint if cfg.Swarm.DisableRelay { logger.Error("The 'Swarm.DisableRelay' config field is deprecated.") if enableRelayTransport { @@ -124,7 +124,7 @@ func LibP2P(bcfg *BuildCfg, cfg *config.Config) fx.Option { logger.Error("Use the 'Swarm.Transports.Network.Relay' config field instead") } } - //nolint + // nolint if cfg.Swarm.EnableAutoRelay { logger.Error("The 'Swarm.EnableAutoRelay' config field is deprecated.") if cfg.Swarm.RelayClient.Enabled == config.Default { @@ -133,7 +133,7 @@ func LibP2P(bcfg *BuildCfg, cfg *config.Config) fx.Option { logger.Error("'Swarm.EnableAutoRelay' has been overridden by 'Swarm.AutoRelay.Enabled'") } } - //nolint + // nolint if cfg.Swarm.EnableRelayHop { logger.Fatal("The `Swarm.EnableRelayHop` config field is ignored.\n" + "Use `Swarm.RelayService` to configure the circuit v2 relay.\n" + @@ -144,6 +144,7 @@ func LibP2P(bcfg *BuildCfg, cfg *config.Config) fx.Option { opts := fx.Options( BaseLibP2P, + fx.Provide(libp2p.ResourceManager()), fx.Provide(libp2p.AddrFilters(cfg.Swarm.AddrFilters)), fx.Provide(libp2p.AddrsFactory(cfg.Addresses.Announce, cfg.Addresses.AppendAnnounce, cfg.Addresses.NoAnnounce)), fx.Provide(libp2p.SmuxTransport(cfg.Swarm.Transports)), diff --git a/core/node/libp2p/rcmgr.go b/core/node/libp2p/rcmgr.go new file mode 100644 index 00000000000..dcd80dc4699 --- /dev/null +++ b/core/node/libp2p/rcmgr.go @@ -0,0 +1,40 @@ +package libp2p + +import ( + "errors" + "fmt" + "os" + + "github.com/libp2p/go-libp2p" + rcmgr "github.com/libp2p/go-libp2p-resource-manager" +) + +func ResourceManager() func() (Libp2pOpts, error) { + return func() (opts Libp2pOpts, err error) { + var limiter *rcmgr.BasicLimiter + + limitsIn, err := os.Open("./limits.json") + switch { + case err == nil: + defer limitsIn.Close() + limiter, err = rcmgr.NewDefaultLimiterFromJSON(limitsIn) + if err != nil { + return opts, fmt.Errorf("error parsing limit file: %w", err) + } + case errors.Is(err, os.ErrNotExist): + limiter = rcmgr.NewDefaultLimiter() + default: + return opts, err + } + + libp2p.SetDefaultServiceLimits(limiter) + + // TODO: close the resource manager when the node is shut down + rcmgr, err := rcmgr.NewResourceManager(limiter) + if err != nil { + return opts, fmt.Errorf("error creating resource manager: %w", err) + } + opts.Opts = append(opts.Opts, libp2p.ResourceManager(rcmgr)) + return opts, nil + } +} diff --git a/go.mod b/go.mod index 6808543a345..235c207c1a7 100644 --- a/go.mod +++ b/go.mod @@ -81,6 +81,7 @@ require ( github.com/libp2p/go-libp2p-pubsub-router v0.5.0 github.com/libp2p/go-libp2p-quic-transport v0.16.1 github.com/libp2p/go-libp2p-record v0.1.3 + github.com/libp2p/go-libp2p-resource-manager v0.1.3 github.com/libp2p/go-libp2p-routing-helpers v0.2.3 github.com/libp2p/go-libp2p-swarm v0.10.2 github.com/libp2p/go-libp2p-testing v0.7.0