Skip to content

Commit

Permalink
load limit file when building resource manager
Browse files Browse the repository at this point in the history
  • Loading branch information
schomatis authored and marten-seemann committed Mar 11, 2022
1 parent 881eef2 commit 46d4219
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions core/node/libp2p/rcmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,38 @@ package libp2p

import (
"context"
"errors"
"fmt"
"os"
"github.com/ipfs/go-ipfs/repo"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/network"
rcmgr "github.com/libp2p/go-libp2p-resource-manager"
"go.uber.org/fx"
)

const NetLimitDefaultFilename = "limit.json"

func ResourceManager() func(fx.Lifecycle, repo.Repo) (network.ResourceManager, Libp2pOpts, error) {
return func(lc fx.Lifecycle, repo repo.Repo) (network.ResourceManager, Libp2pOpts, error) {
var limiter *rcmgr.BasicLimiter
var opts Libp2pOpts

// FIXME(BLOCKING): Decide how is the `limit.json` file path going to be consumed,
// either by default in the repo root or through the `go-ipfs-config`.
limiter = rcmgr.NewDefaultLimiter()
limitFile, err := os.Open(NetLimitDefaultFilename)
if errors.Is(err, os.ErrNotExist) {
limiter = rcmgr.NewDefaultLimiter()
} else {
if err != nil {
return nil, opts, fmt.Errorf("error opening limit JSON file %s: %w",
NetLimitDefaultFilename, err)
}

defer limitFile.Close() //nolint:errcheck
limiter, err = rcmgr.NewDefaultLimiterFromJSON(limitFile)
if err != nil {
return nil, opts, fmt.Errorf("error parsing limit file: %w", err)
}
}

libp2p.SetDefaultServiceLimits(limiter)

Expand Down

0 comments on commit 46d4219

Please sign in to comment.