|
|
@ -130,7 +130,8 @@ func (l *Lock) Lock(stopCh <-chan struct{}) (<-chan struct{}, error) { |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
l.sessionRenew = make(chan struct{}) |
|
|
|
l.sessionRenew = make(chan struct{}) |
|
|
|
l.lockSession = s |
|
|
|
l.lockSession = s |
|
|
|
go l.renewSession(s, l.sessionRenew) |
|
|
|
session := l.c.Session() |
|
|
|
|
|
|
|
go session.RenewPeriodic(l.opts.SessionTTL, s, nil, l.sessionRenew) |
|
|
|
|
|
|
|
|
|
|
|
// If we fail to acquire the lock, cleanup the session
|
|
|
|
// If we fail to acquire the lock, cleanup the session
|
|
|
|
defer func() { |
|
|
|
defer func() { |
|
|
@ -302,30 +303,6 @@ func (l *Lock) lockEntry(session string) *KVPair { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// renewSession is a long running routine that maintians a session
|
|
|
|
|
|
|
|
// by doing a periodic Session renewal.
|
|
|
|
|
|
|
|
func (l *Lock) renewSession(id string, doneCh chan struct{}) { |
|
|
|
|
|
|
|
session := l.c.Session() |
|
|
|
|
|
|
|
ttl, _ := time.ParseDuration(l.opts.SessionTTL) |
|
|
|
|
|
|
|
for { |
|
|
|
|
|
|
|
select { |
|
|
|
|
|
|
|
case <-time.After(ttl / 2): |
|
|
|
|
|
|
|
entry, _, err := session.Renew(id, nil) |
|
|
|
|
|
|
|
if err != nil || entry == nil { |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Handle the server updating the TTL
|
|
|
|
|
|
|
|
ttl, _ = time.ParseDuration(entry.TTL) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case <-doneCh: |
|
|
|
|
|
|
|
// Attempt a session destroy
|
|
|
|
|
|
|
|
session.Destroy(id, nil) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// monitorLock is a long running routine to monitor a lock ownership
|
|
|
|
// monitorLock is a long running routine to monitor a lock ownership
|
|
|
|
// It closes the stopCh if we lose our leadership.
|
|
|
|
// It closes the stopCh if we lose our leadership.
|
|
|
|
func (l *Lock) monitorLock(session string, stopCh chan struct{}) { |
|
|
|
func (l *Lock) monitorLock(session string, stopCh chan struct{}) { |
|
|
|