From fca5ed16a7216e3d7617d707c215d6f30ffe0734 Mon Sep 17 00:00:00 2001 From: xiaojingchen Date: Thu, 11 Jul 2019 10:54:21 +0800 Subject: [PATCH 1/2] change defer close func --- pkg/controller/tidb_control.go | 8 ++++---- pkg/httputil/httputil.go | 10 ++++++---- pkg/pdapi/pdapi.go | 16 ++++++++-------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/pkg/controller/tidb_control.go b/pkg/controller/tidb_control.go index d12bbedecd..5cd376b689 100644 --- a/pkg/controller/tidb_control.go +++ b/pkg/controller/tidb_control.go @@ -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 } @@ -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 @@ -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 @@ -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 diff --git a/pkg/httputil/httputil.go b/pkg/httputil/httputil.go index da4a2ee0cd..31e85b78a7 100644 --- a/pkg/httputil/httputil.go +++ b/pkg/httputil/httputil.go @@ -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). // 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) } } @@ -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 diff --git a/pkg/pdapi/pdapi.go b/pkg/pdapi/pdapi.go index 51b4fe7ee1..7690bb3cc1 100644 --- a/pkg/pdapi/pdapi.go +++ b/pkg/pdapi/pdapi.go @@ -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 { @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 From 9b52433d69c120f49c0b89b9bbaf1ae164dc7004 Mon Sep 17 00:00:00 2001 From: xiaojingchen Date: Thu, 11 Jul 2019 11:04:24 +0800 Subject: [PATCH 2/2] fix comment --- pkg/httputil/httputil.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/httputil/httputil.go b/pkg/httputil/httputil.go index 31e85b78a7..84b0c70780 100644 --- a/pkg/httputil/httputil.go +++ b/pkg/httputil/httputil.go @@ -9,7 +9,7 @@ import ( "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) { if err := c.Close(); err != nil {