Skip to content

Add a new RateLimitLinearJitterBackoff policy #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,23 @@ func LinearJitterBackoff(min, max time.Duration, attemptNum int, resp *http.Resp
return time.Duration(jitterMin * int64(attemptNum))
}

// RateLimitLinearJitterBackoff wraps the retryablehttp.LinearJitterBackoff.
// It first checks if the response status code is http.StatusTooManyRequests
// (HTTP Code 429) or http.StatusServiceUnavailable (HTTP Code 503). If it is
// and the response contains a Retry-After response header, it will wait the
// amount of time specified by the header. Otherwise, this calls
// LinearJitterBackoff.
func RateLimitLinearJitterBackoff(min, max time.Duration, attemptNum int, resp *http.Response) time.Duration {
if resp != nil {
if resp.StatusCode == http.StatusTooManyRequests || resp.StatusCode == http.StatusServiceUnavailable {
if sleep, ok := parseRetryAfterHeader(resp.Header["Retry-After"]); ok {
return sleep
}
}
}
return LinearJitterBackoff(min, max, attemptNum, resp)
}

// PassthroughErrorHandler is an ErrorHandler that directly passes through the
// values from the net/http library for the final request. The body is not
// closed.
Expand Down
107 changes: 107 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,113 @@ func TestClient_PostForm(t *testing.T) {
resp.Body.Close()
}

func TestBackoff_RateLimitLinearJitterBackoff(t *testing.T) {
testCases := []struct {
name string
min time.Duration
max time.Duration
headers http.Header
responseCode int
expect time.Duration
}{
{
name: "429 no retry header",
min: time.Second,
max: time.Second,
headers: http.Header{},
responseCode: http.StatusTooManyRequests,
expect: time.Second,
},
{
name: "503 no retry header",
min: time.Second,
max: time.Second,
headers: http.Header{},
responseCode: http.StatusServiceUnavailable,
expect: time.Second,
},
{
name: "429 retry header",
min: time.Second,
max: time.Second,
headers: http.Header{
"Retry-After": []string{"2"},
},
responseCode: http.StatusTooManyRequests,
expect: 2 * time.Second,
},
{
name: "503 retry header",
min: time.Second,
max: time.Second,
headers: http.Header{
"Retry-After": []string{"2"},
},
responseCode: http.StatusServiceUnavailable,
expect: 2 * time.Second,
},
{
name: "502 ignore retry header",
min: time.Second,
max: time.Second,
headers: http.Header{
"Retry-After": []string{"2"},
},
responseCode: http.StatusBadGateway,
expect: time.Second,
},
{
name: "502 no retry header",
min: time.Second,
max: time.Second,
headers: http.Header{},
responseCode: http.StatusBadGateway,
expect: time.Second,
},
{
name: "429 retry header with jitter",
min: time.Second,
max: 5 * time.Second,
headers: http.Header{
"Retry-After": []string{"2"},
},
responseCode: http.StatusTooManyRequests,
expect: 2 * time.Second,
},
{
name: "429 retry header less than min",
min: 5 * time.Second,
max: 10 * time.Second,
headers: http.Header{
"Retry-After": []string{"2"},
},
responseCode: http.StatusTooManyRequests,
expect: 2 * time.Second,
},
{
name: "429 retry header in range",
min: time.Second,
max: 10 * time.Second,
headers: http.Header{
"Retry-After": []string{"2"},
},
responseCode: http.StatusTooManyRequests,
expect: 2 * time.Second,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := RateLimitLinearJitterBackoff(tc.min, tc.max, 0, &http.Response{
StatusCode: tc.responseCode,
Header: tc.headers,
})
if got != tc.expect {
t.Fatalf("expected %s, got %s", tc.expect, got)
}
})
}
}

func TestBackoff(t *testing.T) {
type tcase struct {
min time.Duration
Expand Down
Loading