Fix issue in ResourceManager and nopfsPlugin about repo path (#10492)

This commit is contained in:
fengzie 2024-09-29 11:58:24 +08:00 committed by GitHub
parent 9577527329
commit a17830754c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 31 additions and 28 deletions

View File

@ -132,7 +132,7 @@ func LibP2P(bcfg *BuildCfg, cfg *config.Config, userResourceOverrides rcmgr.Part
fx.Provide(libp2p.UserAgent()),
// Services (resource management)
fx.Provide(libp2p.ResourceManager(cfg.Swarm, userResourceOverrides)),
fx.Provide(libp2p.ResourceManager(bcfg.Repo.Path(), cfg.Swarm, userResourceOverrides)),
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)),

View File

@ -28,7 +28,7 @@ const NetLimitTraceFilename = "rcmgr.json.gz"
var ErrNoResourceMgr = fmt.Errorf("missing ResourceMgr: make sure the daemon is running with Swarm.ResourceMgr.Enabled")
func ResourceManager(cfg config.SwarmConfig, userResourceOverrides rcmgr.PartialLimitConfig) interface{} {
func ResourceManager(repoPath string, cfg config.SwarmConfig, userResourceOverrides rcmgr.PartialLimitConfig) interface{} {
return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, repo repo.Repo) (network.ResourceManager, Libp2pOpts, error) {
var manager network.ResourceManager
var opts Libp2pOpts
@ -46,11 +46,6 @@ func ResourceManager(cfg config.SwarmConfig, userResourceOverrides rcmgr.Partial
if enabled {
log.Debug("libp2p resource manager is enabled")
repoPath, err := config.PathRoot()
if err != nil {
return nil, opts, fmt.Errorf("opening IPFS_PATH: %w", err)
}
limitConfig, msg, err := LimitConfig(cfg, userResourceOverrides)
if err != nil {
return nil, opts, fmt.Errorf("creating final Resource Manager config: %w", err)

View File

@ -6,7 +6,6 @@ import (
"github.com/ipfs-shipyard/nopfs"
"github.com/ipfs-shipyard/nopfs/ipfs"
"github.com/ipfs/kubo/config"
"github.com/ipfs/kubo/core"
"github.com/ipfs/kubo/core/node"
"github.com/ipfs/kubo/plugin"
@ -20,7 +19,10 @@ var Plugins = []plugin.Plugin{
// fxtestPlugin is used for testing the fx plugin.
// It merely adds an fx option that logs a debug statement, so we can verify that it works in tests.
type nopfsPlugin struct{}
type nopfsPlugin struct {
// Path to the IPFS repo.
repo string
}
var _ plugin.PluginFx = (*nopfsPlugin)(nil)
@ -33,29 +35,28 @@ func (p *nopfsPlugin) Version() string {
}
func (p *nopfsPlugin) Init(env *plugin.Environment) error {
p.repo = env.Repo
return nil
}
// MakeBlocker is a factory for the blocker so that it can be provided with Fx.
func MakeBlocker() (*nopfs.Blocker, error) {
ipfsPath, err := config.PathRoot()
if err != nil {
return nil, err
func MakeBlocker(repoPath string) func() (*nopfs.Blocker, error) {
return func() (*nopfs.Blocker, error) {
defaultFiles, err := nopfs.GetDenylistFiles()
if err != nil {
return nil, err
}
kuboFiles, err := nopfs.GetDenylistFilesInDir(filepath.Join(repoPath, "denylists"))
if err != nil {
return nil, err
}
files := append(defaultFiles, kuboFiles...)
return nopfs.NewBlocker(files)
}
defaultFiles, err := nopfs.GetDenylistFiles()
if err != nil {
return nil, err
}
kuboFiles, err := nopfs.GetDenylistFilesInDir(filepath.Join(ipfsPath, "denylists"))
if err != nil {
return nil, err
}
files := append(defaultFiles, kuboFiles...)
return nopfs.NewBlocker(files)
}
// PathResolvers returns wrapped PathResolvers for Kubo.
@ -76,7 +77,7 @@ func (p *nopfsPlugin) Options(info core.FXNodeInfo) ([]fx.Option, error) {
opts := append(
info.FXOptions,
fx.Provide(MakeBlocker),
fx.Provide(MakeBlocker(p.repo)),
fx.Decorate(ipfs.WrapBlockService),
fx.Decorate(ipfs.WrapNameSystem),
fx.Decorate(PathResolvers),

View File

@ -27,6 +27,10 @@ func (m *Mock) Config() (*config.Config, error) {
return &m.C, nil // FIXME threadsafety
}
func (m *Mock) Path() string {
return ""
}
func (m *Mock) UserResourceOverrides() (rcmgr.PartialLimitConfig, error) {
return rcmgr.PartialLimitConfig{}, nil
}

View File

@ -23,6 +23,9 @@ type Repo interface {
// to the returned config are not automatically persisted.
Config() (*config.Config, error)
// Path is the repo file-system path
Path() string
// UserResourceOverrides returns optional user resource overrides for the
// libp2p resource manager.
UserResourceOverrides() (rcmgr.PartialLimitConfig, error)