Commit ea3fd057 authored by Steven Allen's avatar Steven Allen

feat: retry renames

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.
parent 9b83d068
...@@ -368,7 +368,17 @@ func (fs *Datastore) renameAndUpdateDiskUsage(tmpPath, path string) error { ...@@ -368,7 +368,17 @@ func (fs *Datastore) renameAndUpdateDiskUsage(tmpPath, path string) error {
// Rename and add new file's diskUsage. If the rename fails, // Rename and add new file's diskUsage. If the rename fails,
// it will either a) Re-add the size of an existing file, which // 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. // 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) fs.updateDiskUsage(path, true)
return err return err
} }
......
...@@ -51,7 +51,7 @@ func prefixAndSuffix(pattern string) (prefix, suffix string) { ...@@ -51,7 +51,7 @@ func prefixAndSuffix(pattern string) (prefix, suffix string) {
return return
} }
func tempFile(dir, pattern string) (f *os.File, err error) { func tempFileOnce(dir, pattern string) (f *os.File, err error) {
if dir == "" { if dir == "" {
dir = os.TempDir() dir = os.TempDir()
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment