Skip to content

Commit

Permalink
feat: retry renames
Browse files Browse the repository at this point in the history
Retry renames when the error wasn't due to a file not existing. This case can
come up on windows when some other process opens the file for reading while
we're trying to rename it.
  • Loading branch information
Stebalien committed Apr 17, 2020
1 parent 9b83d06 commit ea3fd05
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion flatfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,17 @@ func (fs *Datastore) renameAndUpdateDiskUsage(tmpPath, path string) error {
// Rename and add new file's diskUsage. If the rename fails,
// it will either a) Re-add the size of an existing file, which
// was sustracted before b) Add 0 if there is no existing file.
err = os.Rename(tmpPath, path)
for i := 0; i < RetryAttempts; i++ {
err = os.Rename(tmpPath, path)
// if there's no error, or the source file doesn't exist, abort.
if err == nil || os.IsNotExist(err) {
break
}
// Otherwise, this could be a transient error due to some other
// process holding open one of the files. Wait a bit and then
// retry.
time.Sleep(time.Duration(i+1) * RetryDelay)
}
fs.updateDiskUsage(path, true)
return err
}
Expand Down
2 changes: 1 addition & 1 deletion util_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func prefixAndSuffix(pattern string) (prefix, suffix string) {
return
}

func tempFile(dir, pattern string) (f *os.File, err error) {
func tempFileOnce(dir, pattern string) (f *os.File, err error) {
if dir == "" {
dir = os.TempDir()
}
Expand Down

0 comments on commit ea3fd05

Please sign in to comment.