From bb0634b0e6deb9d8373f1b70a6d9906a48f3dd92 Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Tue, 11 Jun 2019 16:44:42 +0800 Subject: [PATCH] server: use a different timeout for http clients (#1574) * use a different timeout for http clients --- server/api/redirector.go | 2 +- server/api/util.go | 14 ++++++++++---- server/server.go | 2 +- server/util.go | 8 ++++---- tools/pd-ctl/pdctl/command/global.go | 5 +---- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/server/api/redirector.go b/server/api/redirector.go index 58ddbc90ea3..f51afc005c4 100644 --- a/server/api/redirector.go +++ b/server/api/redirector.go @@ -79,7 +79,7 @@ type customReverseProxies struct { func newCustomReverseProxies(urls []url.URL) *customReverseProxies { p := &customReverseProxies{ - client: server.DialClient, + client: dialClient, } p.urls = append(p.urls, urls...) diff --git a/server/api/util.go b/server/api/util.go index 565288ea565..bed7abf8ad1 100644 --- a/server/api/util.go +++ b/server/api/util.go @@ -23,11 +23,17 @@ import ( "github.com/pingcap/errcode" log "github.com/pingcap/log" "github.com/pingcap/pd/pkg/apiutil" - "github.com/pingcap/pd/server" "github.com/pkg/errors" "github.com/unrolled/render" ) +// dialClient used to dail http request. +var dialClient = &http.Client{ + Transport: &http.Transport{ + DisableKeepAlives: true, + }, +} + // Respond to the client about the given error, integrating with errcode.ErrorCode. // // Important: if the `err` is just an error and not an errcode.ErrorCode (given by errors.Cause), @@ -81,7 +87,7 @@ func readJSON(r io.ReadCloser, data interface{}) error { } func postJSON(url string, data []byte) error { - resp, err := server.DialClient.Post(url, "application/json", bytes.NewBuffer(data)) + resp, err := dialClient.Post(url, "application/json", bytes.NewBuffer(data)) if err != nil { return errors.WithStack(err) } @@ -101,7 +107,7 @@ func doDelete(url string) error { if err != nil { return err } - res, err := server.DialClient.Do(req) + res, err := dialClient.Do(req) if err != nil { return err } @@ -110,7 +116,7 @@ func doDelete(url string) error { } func doGet(url string) (*http.Response, error) { - resp, err := server.DialClient.Get(url) + resp, err := dialClient.Get(url) if err != nil { return nil, errors.WithStack(err) } diff --git a/server/server.go b/server/server.go index f538a17f1de..0462214f520 100644 --- a/server/server.go +++ b/server/server.go @@ -838,7 +838,7 @@ func (s *Server) CheckHealth(members []*pdpb.Member) map[uint64]*pdpb.Member { unhealthMembers := make(map[uint64]*pdpb.Member) for _, member := range members { for _, cURL := range member.ClientUrls { - resp, err := DialClient.Get(fmt.Sprintf("%s%s", cURL, healthURL)) + resp, err := dialClient.Get(fmt.Sprintf("%s%s", cURL, healthURL)) if resp != nil { resp.Body.Close() } diff --git a/server/util.go b/server/util.go index 6bf444f1699..f2a5c6b9703 100644 --- a/server/util.go +++ b/server/util.go @@ -35,7 +35,7 @@ import ( const ( requestTimeout = etcdutil.DefaultRequestTimeout slowRequestTime = etcdutil.DefaultSlowRequestTime - clientTimeout = 30 * time.Second + clientTimeout = 3 * time.Second ) // Version information. @@ -46,8 +46,8 @@ var ( PDGitBranch = "None" ) -// DialClient used to dail http request. -var DialClient = &http.Client{ +// dialClient used to dail http request. +var dialClient = &http.Client{ Timeout: clientTimeout, Transport: &http.Transport{ DisableKeepAlives: true, @@ -274,7 +274,7 @@ func InitHTTPClient(svr *Server) error { return err } - DialClient = &http.Client{ + dialClient = &http.Client{ Timeout: clientTimeout, Transport: &http.Transport{ TLSClientConfig: tlsConfig, diff --git a/tools/pd-ctl/pdctl/command/global.go b/tools/pd-ctl/pdctl/command/global.go index f6f2c6a1ceb..a8d7b1dca09 100644 --- a/tools/pd-ctl/pdctl/command/global.go +++ b/tools/pd-ctl/pdctl/command/global.go @@ -23,7 +23,6 @@ import ( "net/url" "os" "strings" - "time" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -31,8 +30,7 @@ import ( ) var ( - dialClient = &http.Client{Timeout: 30 * time.Second} - + dialClient = &http.Client{} pingPrefix = "pd/ping" ) @@ -49,7 +47,6 @@ func InitHTTPSClient(CAPath, CertPath, KeyPath string) error { } dialClient = &http.Client{ - Timeout: 30 * time.Second, Transport: &http.Transport{ TLSClientConfig: tlsConfig, },