Skip to content

Commit

Permalink
initialize the resource manager
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Feb 28, 2022
1 parent 0daa5a7 commit 0c9ddba
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
9 changes: 5 additions & 4 deletions core/node/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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" +
Expand All @@ -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)),
Expand Down
40 changes: 40 additions & 0 deletions core/node/libp2p/rcmgr.go
Original file line number Diff line number Diff line change
@@ -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
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.4
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
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -862,8 +862,9 @@ github.com/libp2p/go-libp2p-record v0.1.0/go.mod h1:ujNc8iuE5dlKWVy6wuL6dd58t0n7
github.com/libp2p/go-libp2p-record v0.1.2/go.mod h1:pal0eNcT5nqZaTV7UGhqeGqxFgGdsU/9W//C8dqjQDk=
github.com/libp2p/go-libp2p-record v0.1.3 h1:R27hoScIhQf/A8XJZ8lYpnqh9LatJ5YbHs28kCIfql0=
github.com/libp2p/go-libp2p-record v0.1.3/go.mod h1:yNUff/adKIfPnYQXgp6FQmNu3gLJ6EMg7+/vv2+9pY4=
github.com/libp2p/go-libp2p-resource-manager v0.1.3 h1:Umf0tW6WNXSb6Uoma0YT56azB5iikL/aeGAP7s7+f5o=
github.com/libp2p/go-libp2p-resource-manager v0.1.3/go.mod h1:wJPNjeE4XQlxeidwqVY5G6DLOKqFK33u2n8blpl0I6Y=
github.com/libp2p/go-libp2p-resource-manager v0.1.4 h1:RcxMD0pytOUimx3BqTVs6IqItb3H5Qg44SD7XyT68lw=
github.com/libp2p/go-libp2p-resource-manager v0.1.4/go.mod h1:wJPNjeE4XQlxeidwqVY5G6DLOKqFK33u2n8blpl0I6Y=
github.com/libp2p/go-libp2p-routing v0.0.1/go.mod h1:N51q3yTr4Zdr7V8Jt2JIktVU+3xBBylx1MZeVA6t1Ys=
github.com/libp2p/go-libp2p-routing-helpers v0.2.3 h1:xY61alxJ6PurSi+MXbywZpelvuU4U4p/gPTxjqCqTzY=
github.com/libp2p/go-libp2p-routing-helpers v0.2.3/go.mod h1:795bh+9YeoFl99rMASoiVgHdi5bjack0N1+AFAdbvBw=
Expand Down

0 comments on commit 0c9ddba

Please sign in to comment.