-
Notifications
You must be signed in to change notification settings - Fork 51
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
Issue 4558 from Core CLI: Adding flock
#358
base: dev
Are you sure you want to change the base?
Changes from 9 commits
5b287e1
0326ab6
a110d60
28b7dff
1e1c94d
3d21fa8
1e04bf2
c6cc095
0e1ead2
a8a3f6d
f5a3605
0790985
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import ( | |
"path/filepath" | ||
|
||
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/common/file_helpers" | ||
"github.com/gofrs/flock" | ||
) | ||
|
||
const ( | ||
|
@@ -26,11 +27,13 @@ type Persistor interface { | |
|
||
type DiskPersistor struct { | ||
filePath string | ||
fileLock *flock.Flock | ||
} | ||
|
||
func NewDiskPersistor(path string) DiskPersistor { | ||
return DiskPersistor{ | ||
filePath: path, | ||
fileLock: flock.New(path), | ||
} | ||
} | ||
|
||
|
@@ -44,14 +47,26 @@ func (dp DiskPersistor) Load(data DataInterface) error { | |
return err | ||
} | ||
|
||
if err != nil { | ||
err = dp.write(data) | ||
if err != nil { // strange: requiring an error (to allow write attempt to continue), as long as it is not a permission error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Strange code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is strange but the docs strongly recommend using TryLock instead due to the potential blocking issue. Can we try that and see if it resolves this issue
|
||
err = dp.lockedWrite(data) | ||
} | ||
return err | ||
} | ||
|
||
func (dp DiskPersistor) lockedWrite(data DataInterface) error { | ||
lockErr := dp.fileLock.Lock() // provide a file lock, in addition to the RW mutex (in calling functions), just while dp.write is called | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: For readability can we move the comments above line 57 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure! |
||
if lockErr != nil { | ||
return lockErr | ||
} | ||
writeErr := dp.write(data) | ||
if writeErr != nil { | ||
return writeErr | ||
} | ||
return dp.fileLock.Unlock() | ||
} | ||
|
||
func (dp DiskPersistor) Save(data DataInterface) error { | ||
return dp.write(data) | ||
return dp.lockedWrite(data) | ||
} | ||
|
||
func (dp DiskPersistor) read(data DataInterface) error { | ||
|
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.
Nit: For readability can we move the comment above line 157.
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.
Sure!