Skip to content

Commit 6a330d7

Browse files
ashvindeodharYongli Chen
authored andcommitted
Reset lock retry count if lock file timestamp changes (#290)
1 parent 69fde1c commit 6a330d7

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

store/json.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ func (kvs *jsonFileStore) Lock(block bool) error {
141141
lockPerm := os.FileMode(0664) + os.FileMode(os.ModeExclusive)
142142

143143
// Try to acquire the lock file.
144-
for i := 0; ; i++ {
144+
var lockRetryCount uint
145+
var modTimeCur time.Time
146+
var modTimePrev time.Time
147+
for lockRetryCount < lockMaxRetries {
145148
lockFile, err = os.OpenFile(lockName, os.O_CREATE|os.O_EXCL|os.O_RDWR, lockPerm)
146149
if err == nil {
147150
break
@@ -151,12 +154,24 @@ func (kvs *jsonFileStore) Lock(block bool) error {
151154
return ErrNonBlockingLockIsAlreadyLocked
152155
}
153156

154-
if i == lockMaxRetries {
155-
return ErrTimeoutLockingStore
157+
// Reset the lock retry count if the timestamp for the lock file changes.
158+
if fileInfo, err := os.Stat(lockName); err == nil {
159+
modTimeCur = fileInfo.ModTime()
160+
if !modTimeCur.Equal(modTimePrev) {
161+
lockRetryCount = 0
162+
}
163+
modTimePrev = modTimeCur
156164
}
157165

158166
time.Sleep(lockRetryDelay)
167+
168+
lockRetryCount++
159169
}
170+
171+
if lockRetryCount == lockMaxRetries {
172+
return ErrTimeoutLockingStore
173+
}
174+
160175
defer lockFile.Close()
161176

162177
// Write the process ID for easy identification.

0 commit comments

Comments
 (0)