Skip to content

Commit

Permalink
Update unixfs.go
Browse files Browse the repository at this point in the history
use sync.Once instead
  • Loading branch information
hcg1314 authored Nov 18, 2019
1 parent fa8d4e8 commit bc8a329
Showing 1 changed file with 16 additions and 31 deletions.
47 changes: 16 additions & 31 deletions core/coreapi/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,27 @@ import (
)

type UnixfsAPI CoreAPI

var nilNode *core.IpfsNode
var lock sync.Mutex
var once sync.Once

func getOrCreateNilNode() (*core.IpfsNode,error) {
lock.Lock()
defer lock.Unlock()

if nilNode != nil {
return nilNode,nil
}

node,err := core.NewNode(context.Background(), &core.BuildCfg{
//TODO: need this to be true or all files
// hashed will be stored in memory!
NilRepo: true,
once.Do(func() {
if nilNode != nil {
return
}
node, err := core.NewNode(context.Background(), &core.BuildCfg{
//TODO: need this to be true or all files
// hashed will be stored in memory!
NilRepo: true,
})
if err != nil {
panic(err)
}
nilNode = node
})

if err != nil {
return nil, err
}

nilNode = node
return nilNode,nil
}

func CloseFakeRepo() error {
lock.Lock()
defer lock.Unlock()

if nilNode != nil {
if err := nilNode.Close(); err != nil {
return err
}
nilNode = nil
}
return nil
return nilNode, nil
}

// Add builds a merkledag node from a reader, adds it to the blockstore,
Expand Down

0 comments on commit bc8a329

Please sign in to comment.