Update unixfs.go
use sync.Once instead
Hucg committed
Nov 18, 2019 at 17:26 UTC
bc8a329a74c7be4030258a6482aacb098f087913
1 file changed
+16
-31
core/coreapi/unixfs.go
+16
-31
@@ -28,42 +28,27 @@ import (
28
)
29
30
type UnixfsAPI CoreAPI
31
+
32
var nilNode *core.IpfsNode
32
-var lock sync.Mutex
33
+var once sync.Once
34
35
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,
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
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
51
+ return nilNode, nil
52
}
53
54
// Add builds a merkledag node from a reader, adds it to the blockstore,