fix(core/commands): do not cache config (#8824)
Lucas Molas committed
Mar 27, 2022 at 09:11 UTC
d92887086f9c9a4026f8742feada8aaf0aea3530
9 files changed
+9
-66
cmd/ipfs/main.go
-6
@@ -25,7 +25,6 @@ import (
25
"github.com/ipfs/go-ipfs-cmds/cli"
26
cmdhttp "github.com/ipfs/go-ipfs-cmds/http"
27
u "github.com/ipfs/go-ipfs-util"
28
- config "github.com/ipfs/go-ipfs/config"
28
logging "github.com/ipfs/go-log"
29
loggables "github.com/libp2p/go-libp2p-loggables"
30
ma "github.com/multiformats/go-multiaddr"
@@ -138,7 +137,6 @@ func mainRet() int {
137
// this is so that we can construct the node lazily.
138
return &oldcmds.Context{
139
ConfigRoot: repoPath,
141
- LoadConfig: loadConfig,
140
ReqLog: &oldcmds.ReqLog{},
141
Plugins: plugins,
142
ConstructNode: func() (n *core.IpfsNode, err error) {
@@ -306,10 +304,6 @@ func getRepoPath(req *cmds.Request) (string, error) {
304
return repoPath, nil
305
}
306
309
-func loadConfig(path string) (*config.Config, error) {
310
- return fsrepo.ConfigAt(path)
311
-}
312
-
307
// startProfiling begins CPU profiling and returns a `stop` function to be
308
// executed as late as possible. The stop function captures the memprofile.
309
func startProfiling() (func(), error) {
cmd/ipfs/pinmfs.go
+2
-2
@@ -36,7 +36,7 @@ const defaultRepinInterval = 5 * time.Minute
36
37
type pinMFSContext interface {
38
Context() context.Context
39
- GetConfigNoCache() (*config.Config, error)
39
+ GetConfig() (*config.Config, error)
40
}
41
42
type pinMFSNode interface {
@@ -104,7 +104,7 @@ func pinMFSOnChange(configPollInterval time.Duration, cctx pinMFSContext, node p
104
}
105
106
// reread the config, which may have changed in the meantime
107
- cfg, err := cctx.GetConfigNoCache()
107
+ cfg, err := cctx.GetConfig()
108
if err != nil {
109
select {
110
case errCh <- fmt.Errorf("pinning reading config (%v)", err):
cmd/ipfs/pinmfs_test.go
+1
-1
@@ -24,7 +24,7 @@ func (x *testPinMFSContext) Context() context.Context {
24
return x.ctx
25
}
26
27
-func (x *testPinMFSContext) GetConfigNoCache() (*config.Config, error) {
27
+func (x *testPinMFSContext) GetConfig() (*config.Config, error) {
28
return x.cfg, x.err
29
}
30
cmd/ipfswatch/main.go
-4
@@ -20,7 +20,6 @@ import (
20
21
fsnotify "github.com/fsnotify/fsnotify"
22
files "github.com/ipfs/go-ipfs-files"
23
- config "github.com/ipfs/go-ipfs/config"
23
process "github.com/jbenet/goprocess"
24
homedir "github.com/mitchellh/go-homedir"
25
)
@@ -217,9 +216,6 @@ func IsHidden(path string) bool {
216
func cmdCtx(node *core.IpfsNode, repoPath string) commands.Context {
217
return commands.Context{
218
ConfigRoot: repoPath,
220
- LoadConfig: func(path string) (*config.Config, error) {
221
- return node.Repo.Config()
222
- },
219
ConstructNode: func() (*core.IpfsNode, error) {
220
return node, nil
221
},
commands/context.go
+4
-22
@@ -26,30 +26,18 @@ type Context struct {
26
27
Plugins *loader.PluginLoader
28
29
- config *config.Config
30
- LoadConfig func(path string) (*config.Config, error)
31
-
29
Gateway bool
30
api coreiface.CoreAPI
31
node *core.IpfsNode
32
ConstructNode func() (*core.IpfsNode, error)
33
}
34
38
-// GetConfig returns the config of the current Command execution
39
-// context. It may load it with the provided function.
35
func (c *Context) GetConfig() (*config.Config, error) {
41
- var err error
42
- if c.config == nil {
43
- if c.LoadConfig == nil {
44
- return nil, errors.New("nil LoadConfig function")
45
- }
46
- c.config, err = c.LoadConfig(c.ConfigRoot)
36
+ node, err := c.GetNode()
37
+ if err != nil {
38
+ return nil, err
39
}
48
- return c.config, err
49
-}
50
-
51
-func (c *Context) GetConfigNoCache() (*config.Config, error) {
52
- return c.LoadConfig(c.ConfigRoot)
40
+ return node.Repo.Config()
41
}
42
43
// GetNode returns the node of the current Command execution
@@ -61,12 +49,6 @@ func (c *Context) GetNode() (*core.IpfsNode, error) {
49
return nil, errors.New("nil ConstructNode function")
50
}
51
c.node, err = c.ConstructNode()
64
- if err == nil {
65
- // Pre-load the config from the repo to avoid re-parsing it from disk.
66
- if cfg, err := c.node.Repo.Config(); err != nil {
67
- c.config = cfg
68
- }
69
- }
52
}
53
return c.node, err
54
}
core/commands/cmdenv/env.go
-11
@@ -9,7 +9,6 @@ import (
9
"github.com/ipfs/go-ipfs/core"
10
11
cmds "github.com/ipfs/go-ipfs-cmds"
12
- config "github.com/ipfs/go-ipfs/config"
12
logging "github.com/ipfs/go-log"
13
coreiface "github.com/ipfs/interface-go-ipfs-core"
14
options "github.com/ipfs/interface-go-ipfs-core/options"
@@ -52,16 +51,6 @@ func GetApi(env cmds.Environment, req *cmds.Request) (coreiface.CoreAPI, error)
51
return api, nil
52
}
53
55
-// GetConfig extracts the config from the environment.
56
-func GetConfig(env cmds.Environment) (*config.Config, error) {
57
- ctx, ok := env.(*commands.Context)
58
- if !ok {
59
- return nil, fmt.Errorf("expected env to be of type %T, got %T", ctx, env)
60
- }
61
-
62
- return ctx.GetConfig()
63
-}
64
-
54
// GetConfigRoot extracts the config root from the environment
55
func GetConfigRoot(env cmds.Environment) (string, error) {
56
ctx, ok := env.(*commands.Context)
core/commands/mount_unix.go
+2
-1
@@ -7,6 +7,7 @@ import (
7
"fmt"
8
"io"
9
10
+ oldcmds "github.com/ipfs/go-ipfs/commands"
11
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
12
nodeMount "github.com/ipfs/go-ipfs/fuse/node"
13
@@ -81,7 +82,7 @@ baz
82
cmds.StringOption(mountIPNSPathOptionName, "n", "The path where IPNS should be mounted."),
83
},
84
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
84
- cfg, err := cmdenv.GetConfig(env)
85
+ cfg, err := env.(*oldcmds.Context).GetConfig()
86
if err != nil {
87
return err
88
}
core/mock/mock.go
-3
@@ -69,9 +69,6 @@ func MockCmdsCtx() (commands.Context, error) {
69
70
return commands.Context{
71
ConfigRoot: "/tmp/.mockipfsconfig",
72
- LoadConfig: func(path string) (*config.Config, error) {
73
- return &conf, nil
74
- },
72
ConstructNode: func() (*core.IpfsNode, error) {
73
return node, nil
74
},
repo/fsrepo/fsrepo.go
-16
@@ -205,22 +205,6 @@ func checkInitialized(path string) error {
205
return nil
206
}
207
208
-// ConfigAt returns an error if the FSRepo at the given path is not
209
-// initialized. This function allows callers to read the config file even when
210
-// another process is running and holding the lock.
211
-func ConfigAt(repoPath string) (*config.Config, error) {
212
-
213
- // packageLock must be held to ensure that the Read is atomic.
214
- packageLock.Lock()
215
- defer packageLock.Unlock()
216
-
217
- configFilename, err := config.Filename(repoPath)
218
- if err != nil {
219
- return nil, err
220
- }
221
- return serialize.Load(configFilename)
222
-}
223
-
208
// configIsInitialized returns true if the repo is initialized at
209
// provided |path|.
210
func configIsInitialized(path string) bool {