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 72f0cfa
Showing 1 changed file with 11 additions and 1 deletion.
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

0 comments on commit 72f0cfa

Please sign in to comment.