From 0bb810d1263c935e1de343978cb5a19ab0c71b07 Mon Sep 17 00:00:00 2001 From: Simon Pasquier Date: Tue, 27 Nov 2018 14:58:27 +0100 Subject: [PATCH] discovery/marathon: fix leaked connections (#4915) Signed-off-by: Simon Pasquier --- discovery/marathon/marathon.go | 22 +++++----------------- web/web.go | 1 + 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/discovery/marathon/marathon.go b/discovery/marathon/marathon.go index a5ce1aff5..b32eb175a 100644 --- a/discovery/marathon/marathon.go +++ b/discovery/marathon/marathon.go @@ -353,30 +353,18 @@ func fetchApps(client *http.Client, url string) (*AppList, error) { if err != nil { return nil, err } + defer resp.Body.Close() if (resp.StatusCode < 200) || (resp.StatusCode >= 300) { return nil, fmt.Errorf("Non 2xx status '%v' response during marathon service discovery", resp.StatusCode) } - body, err := ioutil.ReadAll(resp.Body) + var apps AppList + err = json.NewDecoder(resp.Body).Decode(&apps) if err != nil { - return nil, err + return nil, fmt.Errorf("%q: %v", url, err) } - - apps, err := parseAppJSON(body) - if err != nil { - return nil, fmt.Errorf("%v in %s", err, url) - } - return apps, nil -} - -func parseAppJSON(body []byte) (*AppList, error) { - apps := &AppList{} - err := json.Unmarshal(body, apps) - if err != nil { - return nil, err - } - return apps, nil + return &apps, nil } // RandomAppsURL randomly selects a server from an array and creates diff --git a/web/web.go b/web/web.go index 858b140cb..f4336cfb2 100644 --- a/web/web.go +++ b/web/web.go @@ -530,6 +530,7 @@ func (h *Handler) consoles(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusNotFound) return } + defer file.Close() text, err := ioutil.ReadAll(file) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError)