|
|
|
@ -42,6 +42,7 @@ type LiveLock struct { |
|
|
|
self string |
|
|
|
lc *LockClient |
|
|
|
owner string |
|
|
|
renewInterval time.Duration |
|
|
|
} |
|
|
|
|
|
|
|
// NewShortLivedLock creates a lock with a 5-second duration
|
|
|
|
@ -60,7 +61,11 @@ func (lc *LockClient) NewShortLivedLock(key string, owner string) (lock *LiveLoc |
|
|
|
} |
|
|
|
|
|
|
|
// StartLongLivedLock starts a goroutine to lock the key and returns immediately.
|
|
|
|
func (lc *LockClient) StartLongLivedLock(key string, owner string, onLockOwnerChange func(newLockOwner string)) (lock *LiveLock) { |
|
|
|
func (lc *LockClient) StartLongLivedLock(key string, owner string, renewInterval time.Duration, onLockOwnerChange func(newLockOwner string)) (lock *LiveLock) { |
|
|
|
// Default to lock_manager.RenewInterval if zero duration is passed
|
|
|
|
if renewInterval == 0 { |
|
|
|
renewInterval = lock_manager.RenewInterval |
|
|
|
} |
|
|
|
lock = &LiveLock{ |
|
|
|
key: key, |
|
|
|
hostFiler: lc.seedFiler, |
|
|
|
@ -69,6 +74,7 @@ func (lc *LockClient) StartLongLivedLock(key string, owner string, onLockOwnerCh |
|
|
|
grpcDialOption: lc.grpcDialOption, |
|
|
|
self: owner, |
|
|
|
lc: lc, |
|
|
|
renewInterval: renewInterval, |
|
|
|
} |
|
|
|
go func() { |
|
|
|
isLocked := false |
|
|
|
@ -104,9 +110,9 @@ func (lc *LockClient) StartLongLivedLock(key string, owner string, onLockOwnerCh |
|
|
|
return |
|
|
|
default: |
|
|
|
if isLocked { |
|
|
|
time.Sleep(5 * lock_manager.RenewInterval) |
|
|
|
time.Sleep(5 * lock.renewInterval) |
|
|
|
} else { |
|
|
|
time.Sleep(lock_manager.RenewInterval) |
|
|
|
time.Sleep(lock.renewInterval) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|