Merge pull request #129 from jonboulle/exp
fix(pkg): simplify exponential backoff to avoid overflows
This commit is contained in:
commit
cd322863e9
@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
neturl "net/url"
|
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
|
// Fetches a given URL with support for exponential backoff and maximum retries
|
||||||
func (h *HttpClient) Get(rawurl string) ([]byte, error) {
|
func (h *HttpClient) Get(rawurl string) ([]byte, error) {
|
||||||
if rawurl == "" {
|
if rawurl == "" {
|
||||||
@ -84,6 +91,7 @@ func (h *HttpClient) Get(rawurl string) ([]byte, error) {
|
|||||||
Transport: transport,
|
Transport: transport,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
duration := 50 * time.Millisecond
|
||||||
for retry := 1; retry <= h.MaxRetries; retry++ {
|
for retry := 1; retry <= h.MaxRetries; retry++ {
|
||||||
log.Printf("Fetching data from %s. Attempt #%d", dataURL, 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())
|
log.Printf("Unable to fetch data: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
duration := time.Millisecond * time.Duration((math.Pow(float64(2), float64(retry)) * 100))
|
duration = expBackoff(duration, h.MaxBackoff)
|
||||||
if duration > h.MaxBackoff {
|
|
||||||
duration = h.MaxBackoff
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("Sleeping for %v...", duration)
|
log.Printf("Sleeping for %v...", duration)
|
||||||
|
|
||||||
time.Sleep(duration)
|
time.Sleep(duration)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,23 +3,38 @@ package pkg
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var expBackoffTests = []struct {
|
func TestExpBackoff(t *testing.T) {
|
||||||
count int
|
duration := time.Millisecond
|
||||||
body string
|
max := time.Hour
|
||||||
}{
|
for i := 0; i < math.MaxUint16; i++ {
|
||||||
{0, "number of attempts: 0"},
|
duration = expBackoff(duration, max)
|
||||||
{1, "number of attempts: 1"},
|
if duration < 0 {
|
||||||
{2, "number of attempts: 2"},
|
t.Fatalf("duration too small: %v %v", duration, i)
|
||||||
|
}
|
||||||
|
if duration > max {
|
||||||
|
t.Fatalf("duration too large: %v %v", duration, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test exponential backoff and that it continues retrying if a 5xx response is
|
// Test exponential backoff and that it continues retrying if a 5xx response is
|
||||||
// received
|
// received
|
||||||
func TestGetURLExpBackOff(t *testing.T) {
|
func TestGetURLExpBackOff(t *testing.T) {
|
||||||
|
var expBackoffTests = []struct {
|
||||||
|
count int
|
||||||
|
body string
|
||||||
|
}{
|
||||||
|
{0, "number of attempts: 0"},
|
||||||
|
{1, "number of attempts: 1"},
|
||||||
|
{2, "number of attempts: 2"},
|
||||||
|
}
|
||||||
client := NewHttpClient()
|
client := NewHttpClient()
|
||||||
|
|
||||||
for i, tt := range expBackoffTests {
|
for i, tt := range expBackoffTests {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user