Add lock conflict check

This commit is contained in:
Alexander Neumann
2015-06-28 13:52:22 +02:00
parent a217f51f2c
commit fba912440d
2 changed files with 90 additions and 0 deletions
+13
View File
@@ -52,6 +52,8 @@ func NewExclusiveLock(repo *repository.Repository) (*Lock, error) {
return newLock(repo, true)
}
const waitBeforeLockCheck = 200 * time.Millisecond
func newLock(repo *repository.Repository, excl bool) (*Lock, error) {
lock := &Lock{
Time: time.Now(),
@@ -78,6 +80,13 @@ func newLock(repo *repository.Repository, excl bool) (*Lock, error) {
return nil, err
}
time.Sleep(waitBeforeLockCheck)
if err = lock.checkForOtherLocks(); err != nil {
lock.Unlock()
return nil, ErrAlreadyLocked
}
return lock, nil
}
@@ -111,6 +120,10 @@ func (l *Lock) fillUserInfo() error {
// exclusive lock is found.
func (l *Lock) checkForOtherLocks() error {
return eachLock(l.repo, func(id backend.ID, lock *Lock, err error) error {
if id.Equal(l.lockID) {
return nil
}
// ignore locks that cannot be loaded
if err != nil {
return nil