Skip to content

Commit

Permalink
change Deferclose func (#653)
Browse files Browse the repository at this point in the history
(cherry picked from commit d1c0202)
  • Loading branch information
xiaojingchen authored and weekface committed Jul 29, 2019
1 parent da1c520 commit f437b0a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
8 changes: 4 additions & 4 deletions pkg/controller/tidb_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (tdc *defaultTiDBControl) ResignDDLOwner(tc *v1alpha1.TidbCluster, ordinal
if err != nil {
return false, err
}
defer httputil.DeferClose(res.Body, &err)
defer httputil.DeferClose(res.Body)
if res.StatusCode == http.StatusOK {
return false, nil
}
Expand All @@ -116,7 +116,7 @@ func (tdc *defaultTiDBControl) GetInfo(tc *v1alpha1.TidbCluster, ordinal int32)
if err != nil {
return nil, err
}
defer httputil.DeferClose(res.Body, &err)
defer httputil.DeferClose(res.Body)
if res.StatusCode != http.StatusOK {
errMsg := fmt.Errorf(fmt.Sprintf("Error response %v URL: %s", res.StatusCode, url))
return nil, errMsg
Expand Down Expand Up @@ -147,7 +147,7 @@ func (tdc *defaultTiDBControl) GetSettings(tc *v1alpha1.TidbCluster, ordinal int
if err != nil {
return nil, err
}
defer httputil.DeferClose(res.Body, &err)
defer httputil.DeferClose(res.Body)
if res.StatusCode != http.StatusOK {
errMsg := fmt.Errorf(fmt.Sprintf("Error response %v URL: %s", res.StatusCode, url))
return nil, errMsg
Expand All @@ -174,7 +174,7 @@ func (tdc *defaultTiDBControl) getBodyOK(apiURL string) ([]byte, error) {
return nil, errMsg
}

defer httputil.DeferClose(res.Body, &err)
defer httputil.DeferClose(res.Body)
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
Expand Down
12 changes: 7 additions & 5 deletions pkg/httputil/httputil.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"io"
"io/ioutil"
"net/http"

"github.com/golang/glog"
)

// DeferClose captures the error returned from closing (if an error occurs).
// DeferClose captures and prints the error from closing (if an error occurs).
// This is designed to be used in a defer statement.
func DeferClose(c io.Closer, err *error) {
if cerr := c.Close(); cerr != nil && *err == nil {
*err = cerr
func DeferClose(c io.Closer) {
if err := c.Close(); err != nil {
glog.Error(err)
}
}

Expand All @@ -31,7 +33,7 @@ func GetBodyOK(httpClient *http.Client, apiURL string) ([]byte, error) {
if err != nil {
return nil, err
}
defer DeferClose(res.Body, &err)
defer DeferClose(res.Body)
if res.StatusCode >= 400 {
errMsg := fmt.Errorf(fmt.Sprintf("Error response %v URL %s", res.StatusCode, apiURL))
return nil, errMsg
Expand Down
16 changes: 8 additions & 8 deletions pkg/pdapi/pdapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func (pc *pdClient) DeleteStore(storeID uint64) error {
if err != nil {
return err
}
defer httputil.DeferClose(res.Body, &err)
defer httputil.DeferClose(res.Body)

// Remove an offline store should returns http.StatusOK
if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusNotFound {
Expand Down Expand Up @@ -365,7 +365,7 @@ func (pc *pdClient) DeleteMemberByID(memberID uint64) error {
if err != nil {
return err
}
defer httputil.DeferClose(res.Body, &err)
defer httputil.DeferClose(res.Body)
if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusNotFound {
return nil
}
Expand Down Expand Up @@ -397,7 +397,7 @@ func (pc *pdClient) DeleteMember(name string) error {
if err != nil {
return err
}
defer httputil.DeferClose(res.Body, &err)
defer httputil.DeferClose(res.Body)
if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusNotFound {
return nil
}
Expand All @@ -415,7 +415,7 @@ func (pc *pdClient) SetStoreLabels(storeID uint64, labels map[string]string) (bo
if err != nil {
return false, err
}
defer httputil.DeferClose(res.Body, &err)
defer httputil.DeferClose(res.Body)
if res.StatusCode == http.StatusOK {
return true, nil
}
Expand All @@ -434,7 +434,7 @@ func (pc *pdClient) BeginEvictLeader(storeID uint64) error {
if err != nil {
return err
}
defer httputil.DeferClose(res.Body, &err)
defer httputil.DeferClose(res.Body)
if res.StatusCode == http.StatusOK {
return nil
}
Expand Down Expand Up @@ -471,7 +471,7 @@ func (pc *pdClient) EndEvictLeader(storeID uint64) error {
if err != nil {
return err
}
defer httputil.DeferClose(res.Body, &err)
defer httputil.DeferClose(res.Body)
if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusNotFound {
return nil
}
Expand Down Expand Up @@ -541,7 +541,7 @@ func (pc *pdClient) TransferPDLeader(memberName string) error {
if err != nil {
return err
}
defer httputil.DeferClose(res.Body, &err)
defer httputil.DeferClose(res.Body)
if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusNotFound {
return nil
}
Expand All @@ -554,7 +554,7 @@ func (pc *pdClient) getBodyOK(apiURL string) ([]byte, error) {
if err != nil {
return nil, err
}
defer httputil.DeferClose(res.Body, &err)
defer httputil.DeferClose(res.Body)
if res.StatusCode >= 400 {
errMsg := fmt.Errorf(fmt.Sprintf("Error response %v URL %s", res.StatusCode, apiURL))
return nil, errMsg
Expand Down

0 comments on commit f437b0a

Please sign in to comment.