helper/resource: exponential backoff

This commit is contained in:
Mitchell Hashimoto 2014-07-10 14:40:18 -07:00
parent 17b0280724
commit 9ab4a5bf88
1 changed files with 8 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"log" "log"
"math"
"time" "time"
) )
@ -43,7 +44,13 @@ func (conf *StateChangeConf) WaitForState() (i interface{}, err error) {
result := make(chan waitResult, 1) result := make(chan waitResult, 1)
go func() { go func() {
for { for tries := 0; ; tries++ {
// Wait between refreshes
wait := time.Duration(math.Pow(2, float64(tries))) *
100 * time.Millisecond
log.Printf("[TRACE] Waiting %s before next try", wait)
time.Sleep(wait)
var currentState string var currentState string
i, currentState, err = conf.Refresh() i, currentState, err = conf.Refresh()
if err != nil { if err != nil {
@ -87,9 +94,6 @@ func (conf *StateChangeConf) WaitForState() (i interface{}, err error) {
return return
} }
} }
// Wait between refreshes
time.Sleep(2 * time.Second)
} }
}() }()