mirror of
https://github.com/status-im/consul.git
synced 2025-01-10 13:55:55 +00:00
Merge pull request #912 from rojojo23/renew-periodic
modify RenewPeriodic to retry failed Renew until TTL elapses
This commit is contained in:
commit
db134f6190
@ -1,6 +1,7 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -131,19 +132,32 @@ func (s *Session) RenewPeriodic(initialTTL string, id string, q *WriteOptions, d
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
waitDur := ttl / 2
|
||||||
|
lastRenewTime := time.Now()
|
||||||
|
var lastErr error
|
||||||
for {
|
for {
|
||||||
|
if time.Since(lastRenewTime) > ttl {
|
||||||
|
return lastErr
|
||||||
|
}
|
||||||
select {
|
select {
|
||||||
case <-time.After(ttl / 2):
|
case <-time.After(waitDur):
|
||||||
entry, _, err := s.Renew(id, q)
|
entry, _, err := s.Renew(id, q)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
waitDur = time.Second
|
||||||
|
lastErr = err
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
if entry == nil {
|
if entry == nil {
|
||||||
return nil
|
waitDur = time.Second
|
||||||
|
lastErr = fmt.Errorf("No SessionEntry returned")
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle the server updating the TTL
|
// Handle the server updating the TTL
|
||||||
ttl, _ = time.ParseDuration(entry.TTL)
|
ttl, _ = time.ParseDuration(entry.TTL)
|
||||||
|
waitDur = ttl / 2
|
||||||
|
lastRenewTime = time.Now()
|
||||||
|
|
||||||
case <-doneCh:
|
case <-doneCh:
|
||||||
// Attempt a session destroy
|
// Attempt a session destroy
|
||||||
|
Loading…
x
Reference in New Issue
Block a user