From dc0225c976815872a997f987f40f959de1d5f70a Mon Sep 17 00:00:00 2001 From: Jan Kolena Date: Mon, 15 Mar 2021 20:04:11 +0100 Subject: [PATCH] EFGS: Tweak ThrottlingAwareClient Better reporting of unsupported retry-after header. 5s default backoff. --- internal/utils/http/throttling-aware-client.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/utils/http/throttling-aware-client.go b/internal/utils/http/throttling-aware-client.go index db66091b..1e6811f1 100644 --- a/internal/utils/http/throttling-aware-client.go +++ b/internal/utils/http/throttling-aware-client.go @@ -9,6 +9,8 @@ import ( ) import "github.com/hashicorp/go-retryablehttp" +const defaultRetryDuration time.Duration = 5 + //NewThrottlingAwareClient Wraps given client and handles retries on HTTP 429. func NewThrottlingAwareClient(httpClient *http.Client, requestLogger func(format string, args ...interface{})) *http.Client { client := retryablehttp.NewClient() @@ -25,19 +27,20 @@ func NewThrottlingAwareClient(httpClient *http.Client, requestLogger func(format client.Backoff = func(min, max time.Duration, attemptNum int, resp *http.Response) time.Duration { if resp == nil { requestLogger("Error while parsing retry-after header: response is nil!") - return 0 + return defaultRetryDuration } retryAfter, err := time.Parse(time.RFC1123, resp.Header.Get("retry-after")) if err != nil { requestLogger("Error while parsing retry-after header: %v", err) - return 0 + requestLogger("Retrieved response: %+v", resp) + return defaultRetryDuration } // Add random 5-10s delay to reduce the contention retryAfter = retryAfter.Add(time.Second * time.Duration(5+rand.Intn(5))) - var duration time.Duration = 0 + var duration = defaultRetryDuration now := time.Now() if retryAfter.After(now) {