From e800f3bf236d839c6597411503b164c45eec5640 Mon Sep 17 00:00:00 2001 From: folbrich Date: Sat, 4 Mar 2017 16:25:41 -0700 Subject: [PATCH 1/2] Close response body and empty it to avoid resource leaks and make use of keep-alive --- api/internal/bus/client.go | 1 + api/internal/bus/connect.go | 1 + api/internal/service/service.go | 9 ++++++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/api/internal/bus/client.go b/api/internal/bus/client.go index 9ac6d47..a0fb1e4 100644 --- a/api/internal/bus/client.go +++ b/api/internal/bus/client.go @@ -61,6 +61,7 @@ func (c *Client) makeRequest(url, method string, body []byte) ([]byte, error) { if err != nil { return nil, fmt.Errorf("request failed: %s", err) } + defer response.Body.Close() responseBody, err := ioutil.ReadAll(response.Body) if err != nil { diff --git a/api/internal/bus/connect.go b/api/internal/bus/connect.go index 1e02697..78e106f 100644 --- a/api/internal/bus/connect.go +++ b/api/internal/bus/connect.go @@ -56,6 +56,7 @@ func openSession(url string, body io.Reader, httpClient *http.Client) (sessionID if err != nil { return "", err } + defer response.Body.Close() var sessionResponse struct{ SessionID string } responseBody, err := ioutil.ReadAll(response.Body) diff --git a/api/internal/service/service.go b/api/internal/service/service.go index 93972a3..06c01bc 100644 --- a/api/internal/service/service.go +++ b/api/internal/service/service.go @@ -3,6 +3,8 @@ package service import ( "errors" "fmt" + "io" + "io/ioutil" "net" "net/http" "os" @@ -135,7 +137,12 @@ func (s *Service) checkStatus() bool { client := &http.Client{} request, _ := http.NewRequest("GET", fmt.Sprintf("%s/status", s.url), nil) response, err := client.Do(request) - if err == nil && response.StatusCode == 200 { + if err != nil { + return false + } + defer response.Body.Close() + io.Copy(ioutil.Discard, response.Body) + if response.StatusCode == 200 { return true } return false From 807bcd94f50bd880088e08b45c0cf7b3db03864b Mon Sep 17 00:00:00 2001 From: folbrich Date: Tue, 7 Mar 2017 20:35:55 -0700 Subject: [PATCH 2/2] Reading the whole body before closing is no longer necessary --- api/internal/service/service.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/api/internal/service/service.go b/api/internal/service/service.go index 06c01bc..cf25daf 100644 --- a/api/internal/service/service.go +++ b/api/internal/service/service.go @@ -3,8 +3,6 @@ package service import ( "errors" "fmt" - "io" - "io/ioutil" "net" "net/http" "os" @@ -141,7 +139,6 @@ func (s *Service) checkStatus() bool { return false } defer response.Body.Close() - io.Copy(ioutil.Discard, response.Body) if response.StatusCode == 200 { return true }