Unverified 提交 bc8a329a 作者: Hucg 提交者: GitHub

Update unixfs.go

use sync.Once instead
上级 fa8d4e85
...@@ -28,42 +28,27 @@ import ( ...@@ -28,42 +28,27 @@ import (
) )
type UnixfsAPI CoreAPI type UnixfsAPI CoreAPI
var nilNode *core.IpfsNode var nilNode *core.IpfsNode
var lock sync.Mutex var once sync.Once
func getOrCreateNilNode() (*core.IpfsNode,error) { func getOrCreateNilNode() (*core.IpfsNode,error) {
lock.Lock() once.Do(func() {
defer lock.Unlock()
if nilNode != nil { if nilNode != nil {
return nilNode,nil return
} }
node, err := core.NewNode(context.Background(), &core.BuildCfg{
node,err := core.NewNode(context.Background(), &core.BuildCfg{
//TODO: need this to be true or all files //TODO: need this to be true or all files
// hashed will be stored in memory! // hashed will be stored in memory!
NilRepo: true, NilRepo: true,
}) })
if err != nil { if err != nil {
return nil, err panic(err)
} }
nilNode = node nilNode = node
return nilNode,nil })
}
func CloseFakeRepo() error {
lock.Lock()
defer lock.Unlock()
if nilNode != nil { return nilNode, nil
if err := nilNode.Close(); err != nil {
return err
}
nilNode = nil
}
return nil
} }
// Add builds a merkledag node from a reader, adds it to the blockstore, // Add builds a merkledag node from a reader, adds it to the blockstore,
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论