From bc8a329a74c7be4030258a6482aacb098f087913 Mon Sep 17 00:00:00 2001 From: Hucg <41573766+hcg1314@users.noreply.github.com> Date: Mon, 18 Nov 2019 17:26:14 +0800 Subject: [PATCH] Update unixfs.go use sync.Once instead --- core/coreapi/unixfs.go | 47 ++++++++++++++---------------------------- 1 file changed, 16 insertions(+), 31 deletions(-) diff --git a/core/coreapi/unixfs.go b/core/coreapi/unixfs.go index ab736cd63..1e4af80f6 100644 --- a/core/coreapi/unixfs.go +++ b/core/coreapi/unixfs.go @@ -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,