Skip to content

Commit

Permalink
feat: read harder
Browse files Browse the repository at this point in the history
Instead of retrying in get only, retry everywhere we read a file.
  • Loading branch information
Stebalien committed Apr 10, 2020
1 parent 319998e commit 303e019
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 21 deletions.
18 changes: 0 additions & 18 deletions flatfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,24 +652,6 @@ func (fs *Datastore) putMany(data map[datastore.Key][]byte) error {
}

func (fs *Datastore) Get(key datastore.Key) (value []byte, err error) {
value, err = fs.get(key)

// Fallback retry for temporary error.
if err != nil && isTooManyFDError(err) {
for i := 0; i < 6; i++ {
time.Sleep(time.Duration(i+1) * RetryDelay)

value, err = fs.get(key)
if err == nil || !isTooManyFDError(err) {
break
}
}
}

return
}

func (fs *Datastore) get(key datastore.Key) (value []byte, err error) {
// Can't exist in datastore.
if !keyIsValid(key) {
return nil, datastore.ErrNotFound
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/ipfs/go-ds-flatfs

require (
github.com/alexbrainman/goissue34681 v0.0.0-20191006012335-3fc7a47baff5 // indirect
github.com/alexbrainman/goissue34681 v0.0.0-20191006012335-3fc7a47baff5
github.com/ipfs/go-datastore v0.4.4
github.com/ipfs/go-log v1.0.3
github.com/jbenet/goprocess v0.1.4
Expand Down
13 changes: 13 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package flatfs
import (
"io"
"os"
"time"
)

// From: http://stackoverflow.com/questions/30697324/how-to-check-if-directory-on-path-is-empty
Expand All @@ -19,3 +20,15 @@ func DirIsEmpty(name string) (bool, error) {
}
return false, err // Either not empty or error, suits both cases
}

func readFile(filename string) (data []byte, err error) {
// Fallback retry for temporary error.
for i := 0; i < 6; i++ {
data, err = readFileOnce(filename)
if err == nil || !isTooManyFDError(err) {
break
}
time.Sleep(time.Duration(i+1) * RetryDelay)
}
return data, err
}
2 changes: 1 addition & 1 deletion util_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ func tempFile(dir, pattern string) (*os.File, error) {
return ioutil.TempFile(dir, pattern)
}

func readFile(filename string) ([]byte, error) {
func readFileOnce(filename string) ([]byte, error) {
return ioutil.ReadFile(filename)
}
2 changes: 1 addition & 1 deletion util_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func tempFile(dir, pattern string) (f *os.File, err error) {
return
}

func readFile(filename string) ([]byte, error) {
func readFileOnce(filename string) ([]byte, error) {
f, err := goissue34681.Open(filename)
if err != nil {
return nil, err
Expand Down

0 comments on commit 303e019

Please sign in to comment.