Skip to content

Commit

Permalink
chore: introduce libp2p resource manager + prometheus metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
EclesioMeloJunior committed Jun 16, 2023
1 parent 246900d commit 24ede47
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions dot/network/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import (
"github.com/libp2p/go-libp2p/core/peerstore"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoreds"
rm "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
rmObs "github.com/libp2p/go-libp2p/p2p/host/resource-manager/obs"
ma "github.com/multiformats/go-multiaddr"
"github.com/prometheus/client_golang/prometheus"
)

func newPrivateIPFilters() (privateIPs *ma.Filters, err error) {
Expand Down Expand Up @@ -178,8 +181,27 @@ func newHost(ctx context.Context, cfg *Config) (*host, error) {
return nil, fmt.Errorf("failed to create peerstore: %w", err)
}

limiter := rm.NewFixedLimiter(rm.DefaultLimits.AutoScale())
var managerOptions []rm.Option

if cfg.Metrics.Publish {
rmObs.MustRegisterWith(prometheus.DefaultRegisterer)
reporter, err := rmObs.NewStatsTraceReporter()
if err != nil {
return nil, fmt.Errorf("while creating resource manager stats trace reporter: %w", err)
}

managerOptions = append(managerOptions, rm.WithTraceReporter(reporter))
}

manager, err := rm.NewResourceManager(limiter, managerOptions...)
if err != nil {
return nil, fmt.Errorf("while creating the resource manager: %w", err)
}

// set libp2p host options
opts := []libp2p.Option{
libp2p.ResourceManager(manager),
libp2p.ListenAddrs(addr),
libp2p.DisableRelay(),
libp2p.Identity(cfg.privateKey),
Expand Down

0 comments on commit 24ede47

Please sign in to comment.