From eb3050029061191d6ba3fb5c1f8d4d9eed495602 Mon Sep 17 00:00:00 2001 From: DanielZhangQD <36026334+DanielZhangQD@users.noreply.github.com> Date: Thu, 6 Aug 2020 11:30:34 +0800 Subject: [PATCH] fix goroutine leak when tls enabled (#3081) --- pkg/autoscaler/autoscaler/query/external.go | 3 ++- pkg/controller/http_client.go | 2 +- pkg/controller/tidb_control.go | 26 ++++++++++----------- pkg/pdapi/pdapi.go | 6 ++++- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/pkg/autoscaler/autoscaler/query/external.go b/pkg/autoscaler/autoscaler/query/external.go index a2420a1bbd..fbb5e53115 100644 --- a/pkg/autoscaler/autoscaler/query/external.go +++ b/pkg/autoscaler/autoscaler/query/external.go @@ -88,7 +88,8 @@ func getClient(endpoint *v1alpha1.ExternalEndpoint, kubecli kubernetes.Interface return nil, err } tr := &http.Transport{ - TLSClientConfig: tlsConfig, + TLSClientConfig: tlsConfig, + DisableKeepAlives: true, } client = &http.Client{ Timeout: defaultTimeout, diff --git a/pkg/controller/http_client.go b/pkg/controller/http_client.go index 234218763b..540fb86952 100644 --- a/pkg/controller/http_client.go +++ b/pkg/controller/http_client.go @@ -61,7 +61,7 @@ func (hc *httpClient) getHTTPClient(tc *v1alpha1.TidbCluster) (*http.Client, err RootCAs: rootCAs, Certificates: []tls.Certificate{tlsCert}, } - httpClient.Transport = &http.Transport{TLSClientConfig: config} + httpClient.Transport = &http.Transport{TLSClientConfig: config, DisableKeepAlives: true} return httpClient, nil } diff --git a/pkg/controller/tidb_control.go b/pkg/controller/tidb_control.go index 222904d655..30be570c85 100644 --- a/pkg/controller/tidb_control.go +++ b/pkg/controller/tidb_control.go @@ -88,14 +88,14 @@ func (tdc *defaultTiDBControl) GetInfo(tc *v1alpha1.TidbCluster, ordinal int32) return nil, 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 - } body, err := ioutil.ReadAll(res.Body) if err != nil { return nil, err } + if res.StatusCode != http.StatusOK { + errMsg := fmt.Errorf(fmt.Sprintf("Error response %s:%v URL: %s", string(body), res.StatusCode, url)) + return nil, errMsg + } info := DBInfo{} err = json.Unmarshal(body, &info) if err != nil { @@ -121,14 +121,14 @@ func (tdc *defaultTiDBControl) GetSettings(tc *v1alpha1.TidbCluster, ordinal int return nil, 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 - } body, err := ioutil.ReadAll(res.Body) if err != nil { return nil, err } + if res.StatusCode != http.StatusOK { + errMsg := fmt.Errorf(fmt.Sprintf("Error response %s:%v URL: %s", string(body), res.StatusCode, url)) + return nil, errMsg + } info := config.Config{} err = json.Unmarshal(body, &info) if err != nil { @@ -142,16 +142,16 @@ func getBodyOK(httpClient *http.Client, apiURL string) ([]byte, error) { if err != nil { return nil, err } - if res.StatusCode >= 400 { - errMsg := fmt.Errorf(fmt.Sprintf("Error response %v URL %s", res.StatusCode, apiURL)) - return nil, errMsg - } - defer httputil.DeferClose(res.Body) body, err := ioutil.ReadAll(res.Body) if err != nil { return nil, err } + if res.StatusCode >= 400 { + errMsg := fmt.Errorf(fmt.Sprintf("Error response %s:%v URL %s", string(body), res.StatusCode, apiURL)) + return nil, errMsg + } + return body, err } diff --git a/pkg/pdapi/pdapi.go b/pkg/pdapi/pdapi.go index 0f91aaa1d9..d4f5fb4b01 100644 --- a/pkg/pdapi/pdapi.go +++ b/pkg/pdapi/pdapi.go @@ -114,11 +114,15 @@ type pdClient struct { // NewPDClient returns a new PDClient func NewPDClient(url string, timeout time.Duration, tlsConfig *tls.Config) PDClient { + var disableKeepalive bool + if tlsConfig != nil { + disableKeepalive = true + } return &pdClient{ url: url, httpClient: &http.Client{ Timeout: timeout, - Transport: &http.Transport{TLSClientConfig: tlsConfig}, + Transport: &http.Transport{TLSClientConfig: tlsConfig, DisableKeepAlives: disableKeepalive}, }, } }