-
Notifications
You must be signed in to change notification settings - Fork 535
Fix concurrent access to wallet handles cache in goal #654
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
Merged
tsachiherman
merged 2 commits into
algorand:master
from
pzbitskiy:pavel/wallet-cache-adv-lock
Dec 20, 2019
Merged
Changes from all commits
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 hidden or 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,121 @@ | ||
| // Copyright (C) 2019 Algorand, Inc. | ||
| // This file is part of go-algorand | ||
| // | ||
| // go-algorand is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as | ||
| // published by the Free Software Foundation, either version 3 of the | ||
| // License, or (at your option) any later version. | ||
| // | ||
| // go-algorand is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with go-algorand. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| package libgoal | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io/ioutil" | ||
| "os" | ||
| "syscall" | ||
| "time" | ||
| ) | ||
|
|
||
| // Platform-dependant locker implementation | ||
| // How to extend | ||
| // 1. Create two new files locker.go and locker_platform.go | ||
| // 2. Put appropriate build tags | ||
| // 3. Move unixLocker implementation and `newLockedFile` method to locker.go | ||
| // 4. Implement platform-specific locker in locker_platform.go | ||
| // 5. Ensure `newLockedFile` sets platform-specific locker | ||
| // so that lockedFile.read and lockedFile.write work correctly | ||
|
|
||
| type locker interface { | ||
| tryRLock(fd *os.File) error | ||
| tryLock(fd *os.File) error | ||
| unlock(fd *os.File) error | ||
| } | ||
|
|
||
| type unixLocker struct { | ||
| } | ||
|
|
||
| func (f *unixLocker) tryRLock(fd *os.File) error { | ||
| return syscall.Flock(int(fd.Fd()), syscall.LOCK_SH|syscall.LOCK_NB) | ||
| } | ||
|
|
||
| func (f *unixLocker) tryLock(fd *os.File) error { | ||
| return syscall.Flock(int(fd.Fd()), syscall.LOCK_EX|syscall.LOCK_NB) | ||
| } | ||
|
|
||
| func (f *unixLocker) unlock(fd *os.File) error { | ||
| return syscall.Flock(int(fd.Fd()), syscall.LOCK_UN) | ||
| } | ||
|
|
||
| // lockedFile implementation | ||
| // It uses non-blocking acquisition with repeats | ||
| // and supposed to be platform-agnostic with appropriate locker implementation. | ||
| // Each platform needs own specific `newLockedFile` | ||
| const maxRepeats = 10 | ||
| const sleepInterval = 10 * time.Millisecond | ||
|
|
||
| type lockedFile struct { | ||
| path string | ||
| locker locker | ||
| } | ||
|
|
||
| func newLockedFile(path string) *lockedFile { | ||
| return &lockedFile{ | ||
| path: path, | ||
| locker: &unixLocker{}, | ||
| } | ||
| } | ||
|
|
||
| func (f *lockedFile) read() ([]byte, error) { | ||
| fd, err := os.Open(f.path) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer fd.Close() | ||
|
|
||
| lockFunc := func() error { return f.locker.tryRLock(fd) } | ||
| err = attemptLock(lockFunc) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("Can't acquire lock for %s: %s", f.path, err.Error()) | ||
| } | ||
| defer f.locker.unlock(fd) | ||
|
|
||
| return ioutil.ReadAll(fd) | ||
| } | ||
|
|
||
| func (f *lockedFile) write(data []byte, perm os.FileMode) error { | ||
| fd, err := os.OpenFile(f.path, os.O_WRONLY|os.O_CREATE, perm) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer fd.Close() | ||
|
|
||
| lockFunc := func() error { return f.locker.tryLock(fd) } | ||
| err = attemptLock(lockFunc) | ||
| if err != nil { | ||
| return fmt.Errorf("Can't acquire lock for %s: %s", f.path, err.Error()) | ||
| } | ||
| defer f.locker.unlock(fd) | ||
|
|
||
| fd.Truncate(0) | ||
| _, err = fd.Write(data) | ||
| return err | ||
| } | ||
|
|
||
| func attemptLock(lockFunc func() error) error { | ||
| var savedError error | ||
| for repeatCounter := 0; repeatCounter < maxRepeats; repeatCounter++ { | ||
| if savedError = lockFunc(); savedError == nil { | ||
| break | ||
| } | ||
| time.Sleep(sleepInterval) | ||
| } | ||
| return savedError | ||
| } | ||
This file contains hidden or 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
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.
make it a member of lockedFile and avoid passing arguments.
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.
Disagree, this is temp runtime info, not a part of the object state (although it is discussable in this particular case)
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.
I think that it makes it over-complicated and hurts readability of this code.. but no logical issues here, so I'll merge that in.