Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions libgoal/lockedFile.go
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 {

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

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)

Copy link
Copy Markdown
Contributor

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.

var savedError error
for repeatCounter := 0; repeatCounter < maxRepeats; repeatCounter++ {
if savedError = lockFunc(); savedError == nil {
break
}
time.Sleep(sleepInterval)
}
return savedError
}
32 changes: 17 additions & 15 deletions libgoal/walletHandles.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package libgoal

import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
)
Expand All @@ -31,14 +30,21 @@ type walletHandles struct {
Handles map[string]string
}

func readLocked(path string) ([]byte, error) {
lf := newLockedFile(path)
return lf.read()
}

func writeLocked(path string, data []byte, perm os.FileMode) error {
lf := newLockedFile(path)
return lf.write(data, perm)
}

func (whs *walletHandles) loadFromDisk(cacheDir string) error {
cachePath, err := walletHandlesCachePath(cacheDir)
if err != nil {
return err
}
_, err = os.Stat(cachePath)
path := walletHandlesCachePath(cacheDir)
_, err := os.Stat(path)
if !os.IsNotExist(err) {
raw, err := ioutil.ReadFile(cachePath)
raw, err := readLocked(path)
if err != nil {
return err
}
Expand All @@ -56,20 +62,16 @@ func (whs *walletHandles) dumpToDisk(cacheDir string) error {
return err
}

path, err := walletHandlesCachePath(cacheDir)
if err != nil {
return err
}

err = ioutil.WriteFile(path, raw, 0600)
path := walletHandlesCachePath(cacheDir)
err = writeLocked(path, raw, 0600)
if err != nil {
return err
}
return nil
}

func walletHandlesCachePath(cacheDir string) (string, error) {
return filepath.Join(cacheDir, walletHandlesJSONName), nil
func walletHandlesCachePath(cacheDir string) string {
return filepath.Join(cacheDir, walletHandlesJSONName)
}

func loadWalletHandleFromDisk(walletID []byte, cacheDir string) ([]byte, error) {
Expand Down