Skip to content

Commit

Permalink
fix goroutine leak when tls enabled (#3081) (#3083)
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>

Co-authored-by: DanielZhangQD <36026334+DanielZhangQD@users.noreply.github.com>
  • Loading branch information
ti-srebot and DanielZhangQD authored Aug 6, 2020
1 parent b22f180 commit e4d8e03
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
3 changes: 2 additions & 1 deletion pkg/autoscaler/autoscaler/query/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
26 changes: 13 additions & 13 deletions pkg/controller/tidb_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/pdapi/pdapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
}
}
Expand Down

0 comments on commit e4d8e03

Please sign in to comment.