fix(pkg): simplify exponential backoff to avoid overflows

This commit is contained in:
Jonathan Boulle
2014-05-29 11:03:15 -07:00
parent e317c7eb9a
commit 269a658d4b
2 changed files with 32 additions and 14 deletions

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"io/ioutil"
"log"
"math"
"net"
"net/http"
neturl "net/url"
@@ -43,6 +42,14 @@ func NewHttpClient() *HttpClient {
}
}
func expBackoff(interval, max time.Duration) time.Duration {
interval = interval * 2
if interval > max {
interval = max
}
return interval
}
// Fetches a given URL with support for exponential backoff and maximum retries
func (h *HttpClient) Get(rawurl string) ([]byte, error) {
if rawurl == "" {
@@ -84,6 +91,7 @@ func (h *HttpClient) Get(rawurl string) ([]byte, error) {
Transport: transport,
}
duration := 50 * time.Millisecond
for retry := 1; retry <= h.MaxRetries; retry++ {
log.Printf("Fetching data from %s. Attempt #%d", dataURL, retry)
@@ -106,13 +114,8 @@ func (h *HttpClient) Get(rawurl string) ([]byte, error) {
log.Printf("Unable to fetch data: %s", err.Error())
}
duration := time.Millisecond * time.Duration((math.Pow(float64(2), float64(retry)) * 100))
if duration > h.MaxBackoff {
duration = h.MaxBackoff
}
duration = expBackoff(duration, h.MaxBackoff)
log.Printf("Sleeping for %v...", duration)
time.Sleep(duration)
}