|
5 | 5 | package robustio |
6 | 6 |
|
7 | 7 | import ( |
8 | | - "io/ioutil" |
9 | | - "math/rand" |
10 | 8 | "os" |
11 | 9 | "syscall" |
12 | | - "time" |
13 | 10 | ) |
14 | 11 |
|
15 | | -const arbitraryTimeout = 500 * time.Millisecond |
16 | | - |
17 | | -const ERROR_SHARING_VIOLATION = 32 |
18 | | - |
19 | | -// retry retries ephemeral errors from f up to an arbitrary timeout |
20 | | -// to work around spurious filesystem errors on Windows |
21 | | -func retry(f func() (err error, mayRetry bool)) error { |
22 | | - var ( |
23 | | - bestErr error |
24 | | - lowestErrno syscall.Errno |
25 | | - start time.Time |
26 | | - nextSleep time.Duration = 1 * time.Millisecond |
27 | | - ) |
28 | | - for { |
29 | | - err, mayRetry := f() |
30 | | - if err == nil || !mayRetry { |
31 | | - return err |
32 | | - } |
33 | | - |
34 | | - if errno, ok := err.(syscall.Errno); ok && (lowestErrno == 0 || errno < lowestErrno) { |
35 | | - bestErr = err |
36 | | - lowestErrno = errno |
37 | | - } else if bestErr == nil { |
38 | | - bestErr = err |
39 | | - } |
40 | | - |
41 | | - if start.IsZero() { |
42 | | - start = time.Now() |
43 | | - } else if d := time.Since(start) + nextSleep; d >= arbitraryTimeout { |
44 | | - break |
45 | | - } |
46 | | - time.Sleep(nextSleep) |
47 | | - nextSleep += time.Duration(rand.Int63n(int64(nextSleep))) |
48 | | - } |
49 | | - |
50 | | - return bestErr |
51 | | -} |
52 | | - |
53 | | -// rename is like os.Rename, but retries ephemeral errors. |
54 | | -// |
55 | | -// It wraps os.Rename, which (as of 2019-06-04) uses MoveFileEx with |
56 | | -// MOVEFILE_REPLACE_EXISTING. |
57 | | -// |
58 | | -// Windows also provides a different system call, ReplaceFile, |
59 | | -// that provides similar semantics, but perhaps preserves more metadata. (The |
60 | | -// documentation on the differences between the two is very sparse.) |
61 | | -// |
62 | | -// Empirical error rates with MoveFileEx are lower under modest concurrency, so |
63 | | -// for now we're sticking with what the os package already provides. |
64 | | -func rename(oldpath, newpath string) (err error) { |
65 | | - return retry(func() (err error, mayRetry bool) { |
66 | | - err = os.Rename(oldpath, newpath) |
67 | | - return err, isEphemeralError(err) |
68 | | - }) |
69 | | -} |
70 | | - |
71 | | -// readFile is like ioutil.ReadFile, but retries ephemeral errors. |
72 | | -func readFile(filename string) ([]byte, error) { |
73 | | - var b []byte |
74 | | - err := retry(func() (err error, mayRetry bool) { |
75 | | - b, err = ioutil.ReadFile(filename) |
76 | | - |
77 | | - // Unlike in rename, we do not retry ERROR_FILE_NOT_FOUND here: it can occur |
78 | | - // as a spurious error, but the file may also genuinely not exist, so the |
79 | | - // increase in robustness is probably not worth the extra latency. |
80 | | - |
81 | | - return err, isEphemeralError(err) && err != syscall.ERROR_FILE_NOT_FOUND |
82 | | - }) |
83 | | - return b, err |
84 | | -} |
85 | | - |
86 | | -func removeAll(path string) error { |
87 | | - return retry(func() (err error, mayRetry bool) { |
88 | | - err = os.RemoveAll(path) |
89 | | - return err, isEphemeralError(err) |
90 | | - }) |
91 | | -} |
| 12 | +const errFileNotFound = syscall.ERROR_FILE_NOT_FOUND |
92 | 13 |
|
93 | 14 | // isEphemeralError returns true if err may be resolved by waiting. |
94 | 15 | func isEphemeralError(err error) bool { |
| 16 | + switch werr := err.(type) { |
| 17 | + case *os.PathError: |
| 18 | + err = werr.Err |
| 19 | + case *os.LinkError: |
| 20 | + err = werr.Err |
| 21 | + case *os.SyscallError: |
| 22 | + err = werr.Err |
| 23 | + } |
95 | 24 | if errno, ok := err.(syscall.Errno); ok { |
96 | 25 | switch errno { |
97 | 26 | case syscall.ERROR_ACCESS_DENIED, |
|
0 commit comments