Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
fix: use sync.Mutex instead of sync.Once to avoid race
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Aug 22, 2023
1 parent 8e37330 commit 3206099
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type Shell struct {
url string
httpcli gohttp.Client

versionOnce sync.Once
version *semver.Version
versionMu sync.Mutex
version *semver.Version
}

func NewLocalShell() *Shell {
Expand Down Expand Up @@ -130,33 +130,34 @@ func NewShellWithClient(url string, client *gohttp.Client) *Shell {
// multipart requests is %-encoded. Before this version, its sent raw.
var encodedAbsolutePathVersion = semver.MustParse("0.23.0-dev")

func (s *Shell) loadRemoteVersion() error {
func (s *Shell) loadRemoteVersion() (*semver.Version, error) {
s.versionMu.Lock()
defer s.versionMu.Unlock()

if s.version == nil {
version, _, err := s.Version()
if err != nil {
return err
return nil, err
}

remoteVersion, err := semver.New(version)
if err != nil {
return err
return nil, err
}

s.versionOnce.Do(func() {
s.version = remoteVersion
})
s.version = remoteVersion
}

return nil
return s.version, nil
}

func (s *Shell) newMultiFileReader(dir files.Directory) (*files.MultiFileReader, error) {
err := s.loadRemoteVersion()
version, err := s.loadRemoteVersion()
if err != nil {
return nil, err
}

return files.NewMultiFileReader(dir, true, s.version.LT(encodedAbsolutePathVersion)), nil
return files.NewMultiFileReader(dir, true, version.LT(encodedAbsolutePathVersion)), nil
}

func (s *Shell) SetTimeout(d time.Duration) {
Expand Down Expand Up @@ -406,7 +407,10 @@ func (s *Shell) PatchData(root string, set bool, data interface{}) (string, erro

fr := files.NewReaderFile(read)
slf := files.NewSliceDirectory([]files.DirEntry{files.FileEntry("", fr)})
fileReader := files.NewMultiFileReader(slf, true, s.version.LT(encodedAbsolutePathVersion))
fileReader, err := s.newMultiFileReader(slf)
if err != nil {
return "", err
}

var out object
return out.Hash, s.Request("object/patch/"+cmd, root).
Expand Down Expand Up @@ -508,7 +512,10 @@ func (s *Shell) BlockPut(block []byte, format, mhtype string, mhlen int) (string

fr := files.NewBytesFile(block)
slf := files.NewSliceDirectory([]files.DirEntry{files.FileEntry("", fr)})
fileReader := files.NewMultiFileReader(slf, true, s.version.LT(encodedAbsolutePathVersion))
fileReader, err := s.newMultiFileReader(slf)
if err != nil {
return "", err
}

return out.Key, s.Request("block/put").
Option("mhtype", mhtype).
Expand Down Expand Up @@ -545,7 +552,10 @@ func (s *Shell) ObjectPut(obj *IpfsObject) (string, error) {

fr := files.NewReaderFile(&data)
slf := files.NewSliceDirectory([]files.DirEntry{files.FileEntry("", fr)})
fileReader := files.NewMultiFileReader(slf, true, s.version.LT(encodedAbsolutePathVersion))
fileReader, err := s.newMultiFileReader(slf)
if err != nil {
return "", err
}

var out object
return out.Hash, s.Request("object/put").
Expand Down

0 comments on commit 3206099

Please sign in to comment.