HTTP backend client cleanup

1. Don't need to close response body twice
2. Pass data as []byte, no need to wrap in a reader
This commit is contained in:
Mikhail Mazurskiy 2021-05-27 23:27:03 +10:00
parent abf7f3416b
commit cf0254ae58
No known key found for this signature in database
GPG key ID: FA7917C48932DD55

View file

@ -38,14 +38,13 @@ type httpClient struct {
}
func (c *httpClient) httpRequest(method string, url *url.URL, data *[]byte, what string) (*http.Response, error) {
// If we have data we need a reader
var reader io.Reader = nil
var body interface{}
if data != nil {
reader = bytes.NewReader(*data)
body = *data
}
// Create the request
req, err := retryablehttp.NewRequest(method, url.String(), reader)
req, err := retryablehttp.NewRequest(method, url.String(), body)
if err != nil {
return nil, fmt.Errorf("Failed to make %s HTTP request: %s", what, err)
}
@ -97,7 +96,6 @@ func (c *httpClient) Lock(info *statemgr.LockInfo) (string, error) {
case http.StatusForbidden:
return "", fmt.Errorf("HTTP remote state endpoint invalid auth")
case http.StatusConflict, http.StatusLocked:
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("HTTP remote state already locked, failed to read body")