fix issue 6760, adding with hash-only, high CPU usage.
hucg committed
Nov 15, 2019 at 14:58 UTC
6a171a0b61b9a4487d9f9642236f19e2a92a95aa
2 files changed
+43
-9
cmd/ipfs/daemon.go
+1
@@ -354,6 +354,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
354
// We wait for the node to close first, as the node has children
355
// that it will wait for before closing, such as the API server.
356
node.Close()
357
+ coreapi.CloseFakeRepo()
358
359
select {
360
case <-req.Context.Done():
core/coreapi/unixfs.go
+42
-9
@@ -3,6 +3,7 @@ package coreapi
3
import (
4
"context"
5
"fmt"
6
+ "sync"
7
8
"github.com/ipfs/go-ipfs/core"
9
@@ -27,6 +28,43 @@ import (
28
)
29
30
type UnixfsAPI CoreAPI
31
+var nilNode *core.IpfsNode
32
+var lock sync.Mutex
33
+
34
+func getOrCreateNilNode() (*core.IpfsNode,error) {
35
+ lock.Lock()
36
+ defer lock.Unlock()
37
+
38
+ if nilNode != nil {
39
+ return nilNode,nil
40
+ }
41
+
42
+ node,err := core.NewNode(context.Background(), &core.BuildCfg{
43
+ //TODO: need this to be true or all files
44
+ // hashed will be stored in memory!
45
+ NilRepo: true,
46
+ })
47
+
48
+ if err != nil {
49
+ return nil, err
50
+ }
51
+
52
+ nilNode = node
53
+ return nilNode,nil
54
+}
55
+
56
+func CloseFakeRepo() error {
57
+ lock.Lock()
58
+ defer lock.Unlock()
59
+
60
+ if nilNode != nil {
61
+ if err := nilNode.Close(); err != nil {
62
+ return err
63
+ }
64
+ nilNode = nil
65
+ }
66
+ return nil
67
+}
68
69
// Add builds a merkledag node from a reader, adds it to the blockstore,
70
// and returns the key representing that node.
@@ -61,18 +99,13 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options
99
pinning := api.pinning
100
101
if settings.OnlyHash {
64
- nilnode, err := core.NewNode(ctx, &core.BuildCfg{
65
- //TODO: need this to be true or all files
66
- // hashed will be stored in memory!
67
- NilRepo: true,
68
- })
102
+ node, err := getOrCreateNilNode()
103
if err != nil {
104
return nil, err
105
}
72
- defer nilnode.Close()
73
- addblockstore = nilnode.Blockstore
74
- exch = nilnode.Exchange
75
- pinning = nilnode.Pinning
106
+ addblockstore = node.Blockstore
107
+ exch = node.Exchange
108
+ pinning = node.Pinning
109
}
110
111
bserv := blockservice.New(addblockstore, exch) // hash security 001