diff --git a/pkg/storage/utils/filelocks/filelocks.go b/pkg/storage/utils/filelocks/filelocks.go index 1c04cb6bfc1..b1f62716e2a 100644 --- a/pkg/storage/utils/filelocks/filelocks.go +++ b/pkg/storage/utils/filelocks/filelocks.go @@ -60,9 +60,20 @@ func SetLockCycleDurationFactor(v int) { } // getMutexedFlock returns a new Flock struct for the given file. -// If there is already one in the local store, it returns nil. -// The caller has to wait until it can get a new one out of this -// mehtod. +// If no Flock exists for the given path it is cerated +func getMutexedFlock(file string) *flock.Flock { + // Is there lock already? + if val, ok := _localLocks.Load(file); ok { + // There is already a lock for this file do not create a new one + return val.(*flock.Flock) + } + + // Create a new Flock struct for the given path + l := flock.New(file) + _localLocks.Store(file, l) + return l + +} // releaseMutexedFlock releases a Flock object that was acquired // before by the getMutexedFlock function. @@ -84,29 +95,7 @@ func acquireLock(file string, write bool) (*flock.Flock, error) { return nil, ErrPathEmpty } - var flock *flock.Flock - for i := 1; i <= _lockCyclesValue; i++ { - // Is there lock already? - if l, ok := _localLocks.Load(file); ok { - return l.(*flock.Flock) - } - - // Acquire the write log on the target node first. - l := flock.New(file) - _localLocks.Store(file, l) - return l - - flock = getMutexedFlock(n) - if flock != nil && !write { - break - } - w := time.Duration(i*_lockCycleDurationFactor) * time.Millisecond - - time.Sleep(w) - } - if flock == nil { - return nil, ErrAcquireLockFailed - } + flock := getMutexedFlock(n) var ok bool for i := 1; i <= _lockCyclesValue; i++ {