From 1b2263699057de9e65d455b3ab645a1a604b0805 Mon Sep 17 00:00:00 2001 From: Edward Marshall Date: Fri, 23 Dec 2016 08:58:33 +0000 Subject: [PATCH] Updated getJson to close the response body correctly --- utils/utils.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/utils/utils.go b/utils/utils.go index 0b5410f..414536f 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -22,6 +22,7 @@ func GetJson(url string, accessKey string, secretKey string, target interface{}) log.Info("Scraping: ", url) client := &http.Client{} + req, err := http.NewRequest("GET", url, nil) req.SetBasicAuth(accessKey, secretKey) @@ -32,11 +33,17 @@ func GetJson(url string, accessKey string, secretKey string, target interface{}) panic(err) } + respFormatted := json.NewDecoder(resp.Body).Decode(target) + // Timings recorded as part of internal metrics elapsed := float64((time.Since(start)) / time.Microsecond) measure.FunctionDurations.WithLabelValues("hosts", "getJSON").Observe(elapsed) - return json.NewDecoder(resp.Body).Decode(target) + // Close the response body, the underlying Transport should then close the connection. + resp.Body.Close() + + // return formatted JSON + return respFormatted } // StacksURLCheck - Checks the API version for Rancher to determine the correct URL