Skip to content

Commit

Permalink
fix(core/commands): do not cache config (ipfs#8824)
Browse files Browse the repository at this point in the history
  • Loading branch information
schomatis authored Mar 27, 2022
1 parent d13a09a commit d928870
Show file tree
Hide file tree
Showing 9 changed files with 9 additions and 66 deletions.
6 changes: 0 additions & 6 deletions cmd/ipfs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/ipfs/go-ipfs-cmds/cli"
cmdhttp "github.com/ipfs/go-ipfs-cmds/http"
u "github.com/ipfs/go-ipfs-util"
config "github.com/ipfs/go-ipfs/config"
logging "github.com/ipfs/go-log"
loggables "github.com/libp2p/go-libp2p-loggables"
ma "github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -138,7 +137,6 @@ func mainRet() int {
// this is so that we can construct the node lazily.
return &oldcmds.Context{
ConfigRoot: repoPath,
LoadConfig: loadConfig,
ReqLog: &oldcmds.ReqLog{},
Plugins: plugins,
ConstructNode: func() (n *core.IpfsNode, err error) {
Expand Down Expand Up @@ -306,10 +304,6 @@ func getRepoPath(req *cmds.Request) (string, error) {
return repoPath, nil
}

func loadConfig(path string) (*config.Config, error) {
return fsrepo.ConfigAt(path)
}

// startProfiling begins CPU profiling and returns a `stop` function to be
// executed as late as possible. The stop function captures the memprofile.
func startProfiling() (func(), error) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/ipfs/pinmfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const defaultRepinInterval = 5 * time.Minute

type pinMFSContext interface {
Context() context.Context
GetConfigNoCache() (*config.Config, error)
GetConfig() (*config.Config, error)
}

type pinMFSNode interface {
Expand Down Expand Up @@ -104,7 +104,7 @@ func pinMFSOnChange(configPollInterval time.Duration, cctx pinMFSContext, node p
}

// reread the config, which may have changed in the meantime
cfg, err := cctx.GetConfigNoCache()
cfg, err := cctx.GetConfig()
if err != nil {
select {
case errCh <- fmt.Errorf("pinning reading config (%v)", err):
Expand Down
2 changes: 1 addition & 1 deletion cmd/ipfs/pinmfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (x *testPinMFSContext) Context() context.Context {
return x.ctx
}

func (x *testPinMFSContext) GetConfigNoCache() (*config.Config, error) {
func (x *testPinMFSContext) GetConfig() (*config.Config, error) {
return x.cfg, x.err
}

Expand Down
4 changes: 0 additions & 4 deletions cmd/ipfswatch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

fsnotify "github.com/fsnotify/fsnotify"
files "github.com/ipfs/go-ipfs-files"
config "github.com/ipfs/go-ipfs/config"
process "github.com/jbenet/goprocess"
homedir "github.com/mitchellh/go-homedir"
)
Expand Down Expand Up @@ -217,9 +216,6 @@ func IsHidden(path string) bool {
func cmdCtx(node *core.IpfsNode, repoPath string) commands.Context {
return commands.Context{
ConfigRoot: repoPath,
LoadConfig: func(path string) (*config.Config, error) {
return node.Repo.Config()
},
ConstructNode: func() (*core.IpfsNode, error) {
return node, nil
},
Expand Down
26 changes: 4 additions & 22 deletions commands/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,18 @@ type Context struct {

Plugins *loader.PluginLoader

config *config.Config
LoadConfig func(path string) (*config.Config, error)

Gateway bool
api coreiface.CoreAPI
node *core.IpfsNode
ConstructNode func() (*core.IpfsNode, error)
}

// GetConfig returns the config of the current Command execution
// context. It may load it with the provided function.
func (c *Context) GetConfig() (*config.Config, error) {
var err error
if c.config == nil {
if c.LoadConfig == nil {
return nil, errors.New("nil LoadConfig function")
}
c.config, err = c.LoadConfig(c.ConfigRoot)
node, err := c.GetNode()
if err != nil {
return nil, err
}
return c.config, err
}

func (c *Context) GetConfigNoCache() (*config.Config, error) {
return c.LoadConfig(c.ConfigRoot)
return node.Repo.Config()
}

// GetNode returns the node of the current Command execution
Expand All @@ -61,12 +49,6 @@ func (c *Context) GetNode() (*core.IpfsNode, error) {
return nil, errors.New("nil ConstructNode function")
}
c.node, err = c.ConstructNode()
if err == nil {
// Pre-load the config from the repo to avoid re-parsing it from disk.
if cfg, err := c.node.Repo.Config(); err != nil {
c.config = cfg
}
}
}
return c.node, err
}
Expand Down
11 changes: 0 additions & 11 deletions core/commands/cmdenv/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/ipfs/go-ipfs/core"

cmds "github.com/ipfs/go-ipfs-cmds"
config "github.com/ipfs/go-ipfs/config"
logging "github.com/ipfs/go-log"
coreiface "github.com/ipfs/interface-go-ipfs-core"
options "github.com/ipfs/interface-go-ipfs-core/options"
Expand Down Expand Up @@ -52,16 +51,6 @@ func GetApi(env cmds.Environment, req *cmds.Request) (coreiface.CoreAPI, error)
return api, nil
}

// GetConfig extracts the config from the environment.
func GetConfig(env cmds.Environment) (*config.Config, error) {
ctx, ok := env.(*commands.Context)
if !ok {
return nil, fmt.Errorf("expected env to be of type %T, got %T", ctx, env)
}

return ctx.GetConfig()
}

// GetConfigRoot extracts the config root from the environment
func GetConfigRoot(env cmds.Environment) (string, error) {
ctx, ok := env.(*commands.Context)
Expand Down
3 changes: 2 additions & 1 deletion core/commands/mount_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"

oldcmds "github.com/ipfs/go-ipfs/commands"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
nodeMount "github.com/ipfs/go-ipfs/fuse/node"

Expand Down Expand Up @@ -81,7 +82,7 @@ baz
cmds.StringOption(mountIPNSPathOptionName, "n", "The path where IPNS should be mounted."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
cfg, err := cmdenv.GetConfig(env)
cfg, err := env.(*oldcmds.Context).GetConfig()
if err != nil {
return err
}
Expand Down
3 changes: 0 additions & 3 deletions core/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ func MockCmdsCtx() (commands.Context, error) {

return commands.Context{
ConfigRoot: "/tmp/.mockipfsconfig",
LoadConfig: func(path string) (*config.Config, error) {
return &conf, nil
},
ConstructNode: func() (*core.IpfsNode, error) {
return node, nil
},
Expand Down
16 changes: 0 additions & 16 deletions repo/fsrepo/fsrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,22 +205,6 @@ func checkInitialized(path string) error {
return nil
}

// ConfigAt returns an error if the FSRepo at the given path is not
// initialized. This function allows callers to read the config file even when
// another process is running and holding the lock.
func ConfigAt(repoPath string) (*config.Config, error) {

// packageLock must be held to ensure that the Read is atomic.
packageLock.Lock()
defer packageLock.Unlock()

configFilename, err := config.Filename(repoPath)
if err != nil {
return nil, err
}
return serialize.Load(configFilename)
}

// configIsInitialized returns true if the repo is initialized at
// provided |path|.
func configIsInitialized(path string) bool {
Expand Down

0 comments on commit d928870

Please sign in to comment.