Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change Deferclose func #653

Merged
merged 4 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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