Skip to content

Commit

Permalink
make lock creation mutable
Browse files Browse the repository at this point in the history
Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
  • Loading branch information
butonic committed Feb 17, 2023
1 parent de56c0e commit bbdb712
Showing 1 changed file with 15 additions and 26 deletions.
41 changes: 15 additions & 26 deletions pkg/storage/utils/filelocks/filelocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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++ {
Expand Down

0 comments on commit bbdb712

Please sign in to comment.