Skip to content

Commit

Permalink
fix README toy example
Browse files Browse the repository at this point in the history
  • Loading branch information
gavriel-hc committed Apr 13, 2022
1 parent 78bf734 commit 8efde52
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,18 @@ call would block and retry with exponential backoff.

It's possible for a request to succeed in the sense that the expected response headers are received, but then to encounter network-level errors while reading the response body. In go-retryablehttp's most basic usage, this error would not be retryable, due to the out-of-band handling of the response body. In some cases it may be desirable to handle the response body as part of the retryable operation.

In such a case, you can use `DoWithResponseHandler` (or `GetWithResponseHandler`) rather than `Do` (or `Get`). A toy example (which will retry the full request and succeed on the second attempt) is shown below:
A toy example (which will retry the full request and succeed on the second attempt) is shown below:

```go
c := retryablehttp.NewClient()
handlerShouldRetry := false
c.GetWithResponseHandler("/foo", func(*http.Response) bool {
handlerShouldRetry = !handlerShouldRetry
return handlerShouldRetry
r := retryablehttp.NewRequest("GET", "://foo", nil)
handlerShouldRetry := true
r.SetResponseHandler(func(*http.Response) error {
if !handlerShouldRetry {
return nil
}
handlerShouldRetry = false
return errors.New("retryable error")
})
```

Expand Down

0 comments on commit 8efde52

Please sign in to comment.