Skip to content
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

kmartin/Add support for RFC1123 Date/Time in Retry-After header. #130

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,12 +470,14 @@ func baseRetryPolicy(resp *http.Response, err error) (bool, error) {
// (HTTP Code 429) is found in the resp parameter. Hence it will return the number of
// seconds the server states it may be ready to process more requests from this client.
func DefaultBackoff(min, max time.Duration, attemptNum int, resp *http.Response) time.Duration {
if resp != nil {
if resp.StatusCode == http.StatusTooManyRequests {
if s, ok := resp.Header["Retry-After"]; ok {
if sleep, err := strconv.ParseInt(s[0], 10, 64); err == nil {
return time.Second * time.Duration(sleep)
}
if resp != nil && resp.StatusCode == http.StatusTooManyRequests {
if s, ok := resp.Header["Retry-After"]; ok {
if sleep, err := strconv.ParseInt(s[0], 10, 64); err == nil {
return time.Second * time.Duration(sleep)
}

if after, err := time.Parse(time.RFC1123, s[0]); err == nil {
return after.Sub(time.Now())
}
}
}
Expand Down
57 changes: 40 additions & 17 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"io/ioutil"
"math"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -524,34 +525,56 @@ func TestClient_CheckRetry(t *testing.T) {
}

func TestClient_DefaultBackoff429TooManyRequest(t *testing.T) {
var header string
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Retry-After", "2")
w.Header().Set("Retry-After", header)
http.Error(w, "test_429_body", http.StatusTooManyRequests)
}))
defer ts.Close()

client := NewClient()

var retryAfter time.Duration
retryable := false

client.CheckRetry = func(_ context.Context, resp *http.Response, err error) (bool, error) {
retryable, _ = DefaultRetryPolicy(context.Background(), resp, err)
retryAfter = DefaultBackoff(client.RetryWaitMin, client.RetryWaitMax, 1, resp)
return false, nil
type tcase struct {
name string
header string
}

_, err := client.Get(ts.URL)
if err != nil {
t.Fatalf("expected no errors since retryable")
cases := []tcase{
{
name: "RFC1123_datetime",
header: time.Now().Add(2 * time.Second).Format(time.RFC1123),
},
{
name: "numeric_duration",
header: "2",
},
}

if !retryable {
t.Fatal("Since 429 is recoverable, the default policy shall return true")
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
var retryAfter time.Duration
retryable := false

client.CheckRetry = func(_ context.Context, resp *http.Response, err error) (bool, error) {
retryable, _ = DefaultRetryPolicy(context.Background(), resp, err)
retryAfter = DefaultBackoff(client.RetryWaitMin, client.RetryWaitMax, 1, resp)
return false, nil
}

header = tt.header

_, err := client.Get(ts.URL)
if err != nil {
t.Fatalf("expected no errors since retryable")
}

if !retryable {
t.Fatal("Since 429 is recoverable, the default policy shall return true")
}

if retryAfter != 2*time.Second {
t.Fatalf("The header Retry-After specified 2 seconds, and shall not be %d seconds", retryAfter/time.Second)
if math.Ceil(retryAfter.Seconds()) != 2 {
t.Fatalf("The header Retry-After specified 2 seconds, and shall not be %.0f seconds", math.Ceil(retryAfter.Seconds()))
}
})
}
}

Expand Down