Skip to content
This repository has been archived by the owner on Oct 31, 2021. It is now read-only.

Commit

Permalink
EFGS: Tweak ThrottlingAwareClient
Browse files Browse the repository at this point in the history
Better reporting of unsupported retry-after header.
5s default backoff.
  • Loading branch information
jendakol committed Mar 16, 2021
1 parent 79b0b10 commit dc0225c
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions internal/utils/http/throttling-aware-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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) {
Expand Down

0 comments on commit dc0225c

Please sign in to comment.