From 379a69d7a405b8577ff14ffd3becba9eaa560e62 Mon Sep 17 00:00:00 2001 From: Robert Johnson Date: Wed, 6 May 2015 16:40:45 -0700 Subject: [PATCH] modify RenewPeriodic to retry failed Renews until TTL elapses --- api/session.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/api/session.go b/api/session.go index 63baa90e93..a99da511d6 100644 --- a/api/session.go +++ b/api/session.go @@ -1,6 +1,7 @@ package api import ( + "fmt" "time" ) @@ -131,19 +132,32 @@ func (s *Session) RenewPeriodic(initialTTL string, id string, q *WriteOptions, d if err != nil { return err } + + waitDur := ttl / 2 + lastRenewTime := time.Now() + var lastErr error for { + if time.Since(lastRenewTime) > ttl { + return lastErr + } select { - case <-time.After(ttl / 2): + case <-time.After(waitDur): entry, _, err := s.Renew(id, q) if err != nil { - return err + waitDur = time.Second + lastErr = err + continue } if entry == nil { - return nil + waitDur = time.Second + lastErr = fmt.Errorf("No SessionEntry returned") + continue } // Handle the server updating the TTL ttl, _ = time.ParseDuration(entry.TTL) + waitDur = ttl / 2 + lastRenewTime = time.Now() case <-doneCh: // Attempt a session destroy