resp.Body must be closed after function return

whether it's success or fail, otherwise it will cause memory leak
ref: https://golang.org/pkg/net/http/
This commit is contained in:
Jiajun Huang
2019-05-30 12:11:59 +08:00
parent 9f9c01b520
commit 75383a95b3
3 changed files with 131 additions and 131 deletions

View File

@@ -76,17 +76,16 @@ func reload() error {
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
} else {
if resp.StatusCode == 200 {
return nil
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return fmt.Errorf("code [%d], %s", resp.StatusCode, strings.TrimSpace(string(body)))
}
return nil
defer resp.Body.Close()
if resp.StatusCode == 200 {
return nil
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return fmt.Errorf("code [%d], %s", resp.StatusCode, strings.TrimSpace(string(body)))
}