-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
reduce mem. usage of unused and update staticcheck #1063
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2019 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package robustio | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
) | ||
|
||
const errFileNotFound = syscall.ENOENT | ||
|
||
// isEphemeralError returns true if err may be resolved by waiting. | ||
func isEphemeralError(err error) bool { | ||
switch werr := err.(type) { | ||
case *os.PathError: | ||
err = werr.Err | ||
case *os.LinkError: | ||
err = werr.Err | ||
case *os.SyscallError: | ||
err = werr.Err | ||
|
||
} | ||
if errno, ok := err.(syscall.Errno); ok { | ||
return errno == errFileNotFound | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2019 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build windows darwin | ||
|
||
package robustio | ||
|
||
import ( | ||
"io/ioutil" | ||
"math/rand" | ||
"os" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
const arbitraryTimeout = 500 * time.Millisecond | ||
|
||
const ERROR_SHARING_VIOLATION = 32 | ||
|
||
// retry retries ephemeral errors from f up to an arbitrary timeout | ||
// to work around filesystem flakiness on Windows and Darwin. | ||
func retry(f func() (err error, mayRetry bool)) error { | ||
var ( | ||
bestErr error | ||
lowestErrno syscall.Errno | ||
start time.Time | ||
nextSleep time.Duration = 1 * time.Millisecond | ||
) | ||
for { | ||
err, mayRetry := f() | ||
if err == nil || !mayRetry { | ||
return err | ||
} | ||
|
||
if errno, ok := err.(syscall.Errno); ok && (lowestErrno == 0 || errno < lowestErrno) { | ||
bestErr = err | ||
lowestErrno = errno | ||
} else if bestErr == nil { | ||
bestErr = err | ||
} | ||
|
||
if start.IsZero() { | ||
start = time.Now() | ||
} else if d := time.Since(start) + nextSleep; d >= arbitraryTimeout { | ||
break | ||
} | ||
time.Sleep(nextSleep) | ||
nextSleep += time.Duration(rand.Int63n(int64(nextSleep))) | ||
} | ||
|
||
return bestErr | ||
} | ||
|
||
// rename is like os.Rename, but retries ephemeral errors. | ||
// | ||
// On windows it wraps os.Rename, which (as of 2019-06-04) uses MoveFileEx with | ||
// MOVEFILE_REPLACE_EXISTING. | ||
// | ||
// Windows also provides a different system call, ReplaceFile, | ||
// that provides similar semantics, but perhaps preserves more metadata. (The | ||
// documentation on the differences between the two is very sparse.) | ||
// | ||
// Empirical error rates with MoveFileEx are lower under modest concurrency, so | ||
// for now we're sticking with what the os package already provides. | ||
func rename(oldpath, newpath string) (err error) { | ||
return retry(func() (err error, mayRetry bool) { | ||
err = os.Rename(oldpath, newpath) | ||
return err, isEphemeralError(err) | ||
}) | ||
} | ||
|
||
// readFile is like ioutil.ReadFile, but retries ephemeral errors. | ||
func readFile(filename string) ([]byte, error) { | ||
var b []byte | ||
err := retry(func() (err error, mayRetry bool) { | ||
b, err = ioutil.ReadFile(filename) | ||
|
||
// Unlike in rename, we do not retry errFileNotFound here: it can occur | ||
// as a spurious error, but the file may also genuinely not exist, so the | ||
// increase in robustness is probably not worth the extra latency. | ||
|
||
return err, isEphemeralError(err) && err != errFileNotFound | ||
}) | ||
return b, err | ||
} | ||
|
||
func removeAll(path string) error { | ||
return retry(func() (err error, mayRetry bool) { | ||
err = os.RemoveAll(path) | ||
return err, isEphemeralError(err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😞 why can't we have nice things?
I like the "robustio" package though (interesting)
(Also, sorry for the noise; just glancing over the code changes, and learned about this macOS bug)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep.
Also, it's copy-pasted code from Go source