Skip to content

Commit cf0254a

Browse files
committed
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
1 parent abf7f34 commit cf0254a

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

internal/backend/remote-state/http/client.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,13 @@ type httpClient struct {
3838
}
3939

4040
func (c *httpClient) httpRequest(method string, url *url.URL, data *[]byte, what string) (*http.Response, error) {
41-
// If we have data we need a reader
42-
var reader io.Reader = nil
41+
var body interface{}
4342
if data != nil {
44-
reader = bytes.NewReader(*data)
43+
body = *data
4544
}
4645

4746
// Create the request
48-
req, err := retryablehttp.NewRequest(method, url.String(), reader)
47+
req, err := retryablehttp.NewRequest(method, url.String(), body)
4948
if err != nil {
5049
return nil, fmt.Errorf("Failed to make %s HTTP request: %s", what, err)
5150
}
@@ -97,7 +96,6 @@ func (c *httpClient) Lock(info *statemgr.LockInfo) (string, error) {
9796
case http.StatusForbidden:
9897
return "", fmt.Errorf("HTTP remote state endpoint invalid auth")
9998
case http.StatusConflict, http.StatusLocked:
100-
defer resp.Body.Close()
10199
body, err := ioutil.ReadAll(resp.Body)
102100
if err != nil {
103101
return "", fmt.Errorf("HTTP remote state already locked, failed to read body")

0 commit comments

Comments
 (0)