Skip to content

Commit

Permalink
fix: add resource manager tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentsenta committed Mar 3, 2023
1 parent 7b5bfeb commit 543f833
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ type daemon struct {
}

func NewDaemon() *daemon {
rm, err := NewResourceManager()
if err != nil {
panic(err)
}

c, err := connmgr.NewConnManager(600, 900, connmgr.WithGracePeriod(time.Second*30))
if err != nil {
panic(err)
Expand All @@ -39,6 +44,7 @@ func NewDaemon() *daemon {
h, err := libp2p.New(
libp2p.ConnectionManager(c),
libp2p.ConnectionGater(&privateAddrFilterConnectionGater{}),
libp2p.ResourceManager(rm),
)
if err != nil {
panic(err)
Expand Down
40 changes: 40 additions & 0 deletions resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// https://github.com/libp2p/go-libp2p/tree/master/p2p/host/resource-manager
package main

import (
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/network"
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
)

func NewResourceManager() (network.ResourceManager, error) {
// Start with the default scaling limits.
scalingLimits := rcmgr.DefaultLimits

// Add limits around included libp2p protocols
libp2p.SetDefaultServiceLimits(&scalingLimits)

// Turn the scaling limits into a concrete set of limits using `.AutoScale`. This
// scales the limits proportional to your system memory.
scaledDefaultLimits := scalingLimits.AutoScale()

// Tweak certain settings
cfg := rcmgr.PartialLimitConfig{
System: rcmgr.ResourceLimits{
ConnsOutbound: rcmgr.Unlimited,
Conns: rcmgr.Unlimited,
ConnsInbound: rcmgr.Unlimited,
},
}

// Create our limits by using our cfg and replacing the default values with values from `scaledDefaultLimits`
limits := cfg.Build(scaledDefaultLimits)

// The resource manager expects a limiter, se we create one from our limits.
limiter := rcmgr.NewFixedLimiter(limits)

// Initialize the resource manager
rm, err := rcmgr.NewResourceManager(limiter)

return rm, err
}

0 comments on commit 543f833

Please sign in to comment.