From e27a735da25abe9a9744e516841186534d0dd997 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 22 Aug 2016 10:15:46 +0200 Subject: [PATCH] Do not sleep between first attempt or between successful attempts --- helper/resource/state.go | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/helper/resource/state.go b/helper/resource/state.go index afa758699..f6a2f1e5c 100644 --- a/helper/resource/state.go +++ b/helper/resource/state.go @@ -2,7 +2,6 @@ package resource import ( "log" - "math" "time" ) @@ -72,26 +71,33 @@ func (conf *StateChangeConf) WaitForState() (interface{}, error) { // Wait for the delay time.Sleep(conf.Delay) + wait := 100 * time.Millisecond + var err error - var wait time.Duration - for tries := 0; ; tries++ { - // Wait between refreshes using an exponential backoff - // If a poll interval has been specified, choose that interval - if conf.PollInterval > 0 && conf.PollInterval < 180*time.Second { - wait = conf.PollInterval - } else { - wait = time.Duration(math.Pow(2, float64(tries))) * - 100 * time.Millisecond - if wait < conf.MinTimeout { - wait = conf.MinTimeout - } else if wait > 10*time.Second { - wait = 10 * time.Second + for first := true; ; first = false { + if !first { + // If a poll interval has been specified, choose that interval. + // Otherwise bound the default value. + if conf.PollInterval > 0 && conf.PollInterval < 180*time.Second { + wait = conf.PollInterval + } else { + if wait < conf.MinTimeout { + wait = conf.MinTimeout + } else if wait > 10*time.Second { + wait = 10 * time.Second + } + } + + log.Printf("[TRACE] Waiting %s before next try", wait) + time.Sleep(wait) + + // Wait between refreshes using exponential backoff, except when + // waiting for the target state to reoccur. + if targetOccurence == 0 { + wait *= 2 } } - log.Printf("[TRACE] Waiting %s before next try", wait) - time.Sleep(wait) - var currentState string result, currentState, err = conf.Refresh() if err != nil {