-
Notifications
You must be signed in to change notification settings - Fork 80
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
httpbp: Fix Retries middleware #647
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"sync" | ||
"sync/atomic" | ||
"testing" | ||
|
@@ -256,11 +257,22 @@ func TestClientErrorWrapper(t *testing.T) { | |
}) | ||
} | ||
|
||
func unwrapRetryErrors(err error) []error { | ||
var errs interface { | ||
error | ||
|
||
Unwrap() []error | ||
} | ||
if errors.As(err, &errs) { | ||
return errs.Unwrap() | ||
} | ||
return []error{err} | ||
} | ||
|
||
func TestRetry(t *testing.T) { | ||
t.Run("retry for timeout", func(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this subtest is removed because we don't really retry for timeouts (there's no timeout budget left for any retries), the expected attempt in this subtest is also 1. |
||
const timeout = time.Millisecond * 10 | ||
t.Run("retry for HTTP 500", func(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
time.Sleep(timeout * 10) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
})) | ||
defer server.Close() | ||
|
||
|
@@ -274,78 +286,117 @@ func TestRetry(t *testing.T) { | |
attempts = n + 1 | ||
}), | ||
)(http.DefaultTransport), | ||
Timeout: timeout, | ||
} | ||
_, err := client.Get(server.URL) | ||
u, err := url.Parse(server.URL) | ||
if err != nil { | ||
t.Fatalf("Failed to parse url %q: %v", server.URL, err) | ||
} | ||
req := &http.Request{ | ||
Method: http.MethodPost, | ||
URL: u, | ||
|
||
// Explicitly set Body to http.NoBody and GetBody to nil, | ||
// This request should not cause Retries middleware to be skipped. | ||
Body: http.NoBody, | ||
GetBody: nil, | ||
} | ||
_, err = client.Do(req) | ||
if err == nil { | ||
t.Fatalf("expected error to be non-nil") | ||
} | ||
expected := uint(1) | ||
expected := uint(2) | ||
if attempts != expected { | ||
t.Errorf("expected %d, actual: %d", expected, attempts) | ||
} | ||
errs := unwrapRetryErrors(err) | ||
if len(errs) != int(expected) { | ||
t.Errorf("Expected %d retry erros, got %+v", expected, errs) | ||
} | ||
for i, err := range errs { | ||
var ce *ClientError | ||
if errors.As(err, &ce) { | ||
if got, want := ce.StatusCode, http.StatusInternalServerError; got != want { | ||
t.Errorf("#%d: status got %d want %d", i, got, want) | ||
} | ||
} else { | ||
t.Errorf("#%d: %#v is not of type *httpbp.ClientError", i, err) | ||
} | ||
} | ||
}) | ||
|
||
t.Run("retry for HTTP 500", func(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
t.Run("retry POST+HTTPS request", func(t *testing.T) { | ||
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
b, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
expected := "{}" | ||
got := string(b) | ||
if got != expected { | ||
t.Errorf("expected %q, got: %q", expected, got) | ||
} | ||
t.Logf("Full body: %q", got) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
})) | ||
defer server.Close() | ||
|
||
var attempts uint | ||
client := &http.Client{ | ||
Transport: Retries( | ||
DefaultMaxErrorReadAhead, | ||
retry.Attempts(2), | ||
retry.OnRetry(func(n uint, err error) { | ||
// set number of attempts to check if retries were attempted | ||
attempts = n + 1 | ||
}), | ||
)(http.DefaultTransport), | ||
} | ||
_, err := client.Get(server.URL) | ||
t.Log(server.URL) | ||
client := server.Client() | ||
client.Transport = Retries( | ||
DefaultMaxErrorReadAhead, | ||
retry.Attempts(2), | ||
retry.OnRetry(func(n uint, err error) { | ||
// set number of attempts to check if retries were attempted | ||
attempts = n + 1 | ||
}), | ||
)(client.Transport) | ||
_, err := client.Post(server.URL, "application/json", bytes.NewBufferString("{}")) | ||
if err == nil { | ||
t.Fatalf("expected error to be non-nil") | ||
} | ||
expected := uint(2) | ||
if attempts != expected { | ||
t.Errorf("expected %d, actual: %d", expected, attempts) | ||
} | ||
errs := unwrapRetryErrors(err) | ||
if len(errs) != int(expected) { | ||
t.Errorf("Expected %d retry erros, got %+v", expected, errs) | ||
} | ||
for i, err := range errs { | ||
var ce *ClientError | ||
if errors.As(err, &ce) { | ||
if got, want := ce.StatusCode, http.StatusInternalServerError; got != want { | ||
t.Errorf("#%d: status got %d want %d", i, got, want) | ||
} | ||
} else { | ||
t.Errorf("#%d: %#v is not of type *httpbp.ClientError", i, err) | ||
} | ||
} | ||
}) | ||
|
||
t.Run("retry POST request", func(t *testing.T) { | ||
t.Run("skip retry for wrongly constructed request", func(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
b, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
expected := "{}" | ||
got := string(b) | ||
if got != expected { | ||
t.Errorf("expected %q, got: %q", expected, got) | ||
} | ||
w.WriteHeader(http.StatusInternalServerError) | ||
})) | ||
defer server.Close() | ||
|
||
var attempts uint | ||
client := &http.Client{ | ||
Transport: Retries( | ||
DefaultMaxErrorReadAhead, | ||
retry.Attempts(2), | ||
retry.OnRetry(func(n uint, err error) { | ||
// set number of attempts to check if retries were attempted | ||
attempts = n + 1 | ||
t.Errorf("Retry not skipped. OnRetry called with (%d, %v)", n, err) | ||
}), | ||
)(http.DefaultTransport), | ||
} | ||
_, err := client.Post(server.URL, "application/json", bytes.NewBufferString("{}")) | ||
if err == nil { | ||
t.Fatalf("expected error to be non-nil") | ||
req, err := http.NewRequest(http.MethodGet, server.URL, bytes.NewBufferString("{}")) | ||
if err != nil { | ||
t.Fatalf("Failed to create http request: %v", err) | ||
} | ||
expected := uint(2) | ||
if attempts != expected { | ||
t.Errorf("expected %d, actual: %d", expected, attempts) | ||
req.GetBody = nil | ||
if _, err := client.Do(req); err == nil { | ||
t.Fatalf("expected error to be non-nil") | ||
} | ||
}) | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔕 do you want to this as a once or do it on a rate limit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will only log once per request (it's not inside the retry loop), so if it's logged multiple times that means users have multiple requests constructed incorrectly, which I think it's warranted to log multiple times (for each incorrect request)
and we are using
slog
, so if users really want to rate-limit it/suppress it they can do so in their slog handler :)wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤷