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 {
}
defer resp.Body.Close()
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
}

View File

@ -78,11 +78,12 @@ func status() error {
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
} else {
}
if resp.StatusCode != 200 {
return fmt.Errorf("admin api status code [%d]", resp.StatusCode)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
@ -148,6 +149,6 @@ func status() error {
tbl.Print()
fmt.Println("")
}
}
return nil
}

View File

@ -28,7 +28,7 @@ func GetProxyStatus(statusAddr string, user string, passwd string, name string)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return status, err
} else {
}
if resp.StatusCode != 200 {
return status, fmt.Errorf("admin api status code [%d]", resp.StatusCode)
}
@ -72,7 +72,7 @@ func GetProxyStatus(statusAddr string, user string, passwd string, name string)
return &s, nil
}
}
}
return status, errors.New("no proxy status found")
}
@ -87,13 +87,13 @@ func ReloadConf(reloadAddr string, user string, passwd string) error {
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
} else {
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("admin api status code [%d]", resp.StatusCode)
}
defer resp.Body.Close()
io.Copy(ioutil.Discard, resp.Body)
}
return nil
}