Skip to content
Open
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
38 changes: 35 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
"sync"
"time"

cleanhttp "github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/go-cleanhttp"
)

var (
Expand Down Expand Up @@ -645,8 +645,7 @@ func PassthroughErrorHandler(resp *http.Response, err error, _ int) (*http.Respo
return resp, err
}

// Do wraps calling an HTTP method with retries.
func (c *Client) Do(req *Request) (*http.Response, error) {
func (c *Client) do(req *Request) (*http.Response, error) {
c.clientInit.Do(func() {
if c.HTTPClient == nil {
c.HTTPClient = cleanhttp.DefaultPooledClient()
Expand Down Expand Up @@ -825,6 +824,39 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
req.Method, redactURL(req.URL), attempt, err)
}

type drainReadCloser struct {
io.ReadCloser
read bool
}

func (rc *drainReadCloser) Read(p []byte) (n int, err error) {
n, err = rc.ReadCloser.Read(p)
if err == io.EOF {
rc.read = true
}
return
}

func (rc *drainReadCloser) Close() error {
if !rc.read {
// Use same drain logic as in drainBody
io.Copy(io.Discard, io.LimitReader(rc.ReadCloser, respReadLimit))
}
return rc.ReadCloser.Close()
}

// Do wraps calling an HTTP method with retries.
func (c *Client) Do(req *Request) (*http.Response, error) {
resp, err := c.do(req)
if err != nil {
return nil, err
}
if resp != nil && resp.Body != nil {
resp.Body = &drainReadCloser{ReadCloser: resp.Body}
}
return resp, nil
}

// Try to read the response body so we can reuse this connection.
func (c *Client) drainBody(body io.ReadCloser) {
defer body.Close()
Expand Down