fix issue 6760, adding with hash-only, high CPU usage.
hucg committed
Nov 15, 2019 at 14:58 UTC
9aa2f07f1f056f46844ead8db32cc457c6970b61
1 file changed
+27
-9
core/coreapi/unixfs.go
+27
-9
@@ -3,6 +3,7 @@ package coreapi
3
import (
4
"context"
5
"fmt"
6
+ "sync"
7
8
"github.com/ipfs/go-ipfs/core"
9
@@ -28,6 +29,28 @@ import (
29
30
type UnixfsAPI CoreAPI
31
32
+var nilNode *core.IpfsNode
33
+var once sync.Once
34
+
35
+func getOrCreateNilNode() (*core.IpfsNode,error) {
36
+ once.Do(func() {
37
+ if nilNode != nil {
38
+ return
39
+ }
40
+ node, err := core.NewNode(context.Background(), &core.BuildCfg{
41
+ //TODO: need this to be true or all files
42
+ // hashed will be stored in memory!
43
+ NilRepo: true,
44
+ })
45
+ if err != nil {
46
+ panic(err)
47
+ }
48
+ nilNode = node
49
+ })
50
+
51
+ return nilNode, nil
52
+}
53
+
54
// Add builds a merkledag node from a reader, adds it to the blockstore,
55
// and returns the key representing that node.
56
func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options.UnixfsAddOption) (path.Resolved, error) {
@@ -61,18 +84,13 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options
84
pinning := api.pinning
85
86
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
- })
87
+ node, err := getOrCreateNilNode()
88
if err != nil {
89
return nil, err
90
}
72
- defer nilnode.Close()
73
- addblockstore = nilnode.Blockstore
74
- exch = nilnode.Exchange
75
- pinning = nilnode.Pinning
91
+ addblockstore = node.Blockstore
92
+ exch = node.Exchange
93
+ pinning = node.Pinning
94
}
95
96
bserv := blockservice.New(addblockstore, exch) // hash security 001