Skip to content
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
7 changes: 4 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,9 @@ type Backoff func(min, max time.Duration, attemptNum int, resp *http.Response) t
// attempted. If overriding this, be sure to close the body if needed.
type ErrorHandler func(resp *http.Response, err error, numTries int) (*http.Response, error)

// PrepareRetry is called before retry operation. It can be used for example to re-sign the request
type PrepareRetry func(req *http.Request) error
// PrepareRetry is called before retry operation. It can be used for example to re-sign the request or
// update it depending on the previous failed response.
type PrepareRetry func(req *http.Request, resp *http.Response) error

// Client is used to make HTTP requests. It adds additional functionality
// like automatic retries to tolerate minor outages.
Expand Down Expand Up @@ -779,7 +780,7 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
req.Request = &httpreq

if c.PrepareRetry != nil {
if err := c.PrepareRetry(req.Request); err != nil {
if err := c.PrepareRetry(req.Request, resp); err != nil {
prepareErr = err
break
}
Expand Down
20 changes: 19 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,14 @@ func TestClient_Do_WithPrepareRetry(t *testing.T) {
}

var prepareChecks int
client.PrepareRetry = func(req *http.Request) error {
client.PrepareRetry = func(req *http.Request, resp *http.Response) error {
prepareChecks++
req.Header.Set("foo", strconv.Itoa(prepareChecks))

lastCount := resp.Header.Get("bar")
if lastCount != "" && lastCount != strconv.Itoa(prepareChecks) {
return errors.New("prepare check failed")
}
return nil
}

Expand Down Expand Up @@ -431,6 +436,19 @@ func TestClient_Do_WithPrepareRetry(t *testing.T) {
expectedChecks: 4,
expectedPrepareChecks: 1,
},
{
name: "request succeeds when matches with response",
handler: func(resp *http.Response) error {
if shouldSucceed {
resp.Header.Set("bar", strconv.Itoa(prepareChecks))
return nil
}
shouldSucceed = true
return errors.New("retryable failure")
},
expectedChecks: 4,
expectedPrepareChecks: 1,
},
}

for _, tt := range tests {
Expand Down