Skip to content

Commit 493aa4c

Browse files
authored
Merge pull request #164 from hashicorp/docs
improve docs for response handlers
2 parents 98169fe + b0b7363 commit 493aa4c

File tree

2 files changed

+9
-21
lines changed

2 files changed

+9
-21
lines changed

README.md

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,6 @@ The returned response object is an `*http.Response`, the same thing you would
4545
usually get from `net/http`. Had the request failed one or more times, the above
4646
call would block and retry with exponential backoff.
4747

48-
## Retrying cases that fail after a seeming success
49-
50-
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.
51-
52-
A toy example (which will retry the full request and succeed on the second attempt) is shown below:
53-
54-
```go
55-
c := retryablehttp.NewClient()
56-
r := retryablehttp.NewRequest("GET", "://foo", nil)
57-
handlerShouldRetry := true
58-
r.SetResponseHandler(func(*http.Response) error {
59-
if !handlerShouldRetry {
60-
return nil
61-
}
62-
handlerShouldRetry = false
63-
return errors.New("retryable error")
64-
})
65-
```
66-
6748
## Getting a stdlib `*http.Client` with retries
6849

6950
It's possible to convert a `*retryablehttp.Client` directly to a `*http.Client`.

client.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,15 @@ var (
8080
type ReaderFunc func() (io.Reader, error)
8181

8282
// ResponseHandlerFunc is a type of function that takes in a Response, and does something with it.
83-
// It only runs if the initial part of the request was successful.
84-
// If an error is returned, the client's retry policy will be used to determine whether to retry the whole request.
83+
// The ResponseHandlerFunc is called when the HTTP client successfully receives a response and the
84+
// CheckRetry function indicates that a retry of the base request is not necessary.
85+
// If an error is returned from this function, the CheckRetry policy will be used to determine
86+
// whether to retry the whole request (including this handler).
87+
//
88+
// Make sure to check status codes! Even if the request was completed it may have a non-2xx status code.
89+
//
90+
// The response body is not automatically closed. It must be closed either by the ResponseHandlerFunc or
91+
// by the caller out-of-band. Failure to do so will result in a memory leak.
8592
type ResponseHandlerFunc func(*http.Response) error
8693

8794
// LenReader is an interface implemented by many in-memory io.Reader's. Used

0 commit comments

Comments
 (0)