Skip to content

Commit

Permalink
addressing comments v2
Browse files Browse the repository at this point in the history
  • Loading branch information
gavriel-hc committed Apr 13, 2022
1 parent 8efde52 commit 5bd1a6f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
33 changes: 19 additions & 14 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ var (
// ReaderFunc is the type of function that can be given natively to NewRequest
type ReaderFunc func() (io.Reader, error)

// ResponseHandlingFunc is a type of function that takes in a Response, and does something with it.
// ResponseHandlerFunc is a type of function that takes in a Response, and does something with it.
// It only runs if the initial part of the request was successful.
// If an error is returned, the client's retry policy will be used to determine whether to retry the whole request.
type ResponseHandlingFunc func(*http.Response) error
type ResponseHandlerFunc func(*http.Response) error

// LenReader is an interface implemented by many in-memory io.Reader's. Used
// for automatically sending the right Content-Length header when possible.
Expand All @@ -96,7 +96,7 @@ type Request struct {
// used to rewind the request data in between retries.
body ReaderFunc

responseHandler ResponseHandlingFunc
responseHandler ResponseHandlerFunc

// Embed an HTTP request directly. This makes a *Request act exactly
// like an *http.Request so that all meta methods are supported.
Expand All @@ -114,7 +114,7 @@ func (r *Request) WithContext(ctx context.Context) *Request {
}

// SetResponseHandler allows setting the response handler.
func (r *Request) SetResponseHandler(fn ResponseHandlingFunc) {
func (r *Request) SetResponseHandler(fn ResponseHandlerFunc) {
r.responseHandler = fn
}

Expand Down Expand Up @@ -589,6 +589,7 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
var doErr, respErr, checkErr error

for i := 0; ; i++ {
doErr, respErr = nil, nil
attempt++

// Always rewind the request body when non-nil.
Expand Down Expand Up @@ -619,12 +620,23 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
// Attempt the request
resp, doErr = c.HTTPClient.Do(req.Request)

if doErr != nil {
// Check if we should continue with retries.
shouldRetry, checkErr = c.CheckRetry(req.Context(), resp, doErr)
if !shouldRetry && doErr == nil && req.responseHandler != nil {
respErr = req.responseHandler(resp)
shouldRetry, checkErr = c.CheckRetry(req.Context(), resp, respErr)
}

err := doErr
if respErr != nil {
err = respErr
}
if err != nil {
switch v := logger.(type) {
case LeveledLogger:
v.Error("request failed", "error", doErr, "method", req.Method, "url", req.URL)
v.Error("request failed", "error", err, "method", req.Method, "url", req.URL)
case Logger:
v.Printf("[ERR] %s %s request failed: %v", req.Method, req.URL, doErr)
v.Printf("[ERR] %s %s request failed: %v", req.Method, req.URL, err)
}
} else {
// Call this here to maintain the behavior of logging all requests,
Expand All @@ -642,13 +654,6 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
}
}

// Check if we should continue with retries.
shouldRetry, checkErr = c.CheckRetry(req.Context(), resp, doErr)
if !shouldRetry && doErr == nil && req.responseHandler != nil {
respErr = req.responseHandler(resp)
shouldRetry, checkErr = c.CheckRetry(req.Context(), resp, respErr)
}

if !shouldRetry {
break
}
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func TestClient_Do_WithResponseHandler(t *testing.T) {
var shouldSucceed bool
tests := []struct {
name string
handler ResponseHandlingFunc
handler ResponseHandlerFunc
expectedChecks int // often 2x number of attempts since we check twice
err string
}{
Expand Down

0 comments on commit 5bd1a6f

Please sign in to comment.