From 2b688d28f2785af7a7fbea66808d56dcdabcb494 Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Tue, 10 Sep 2019 14:57:52 +0800 Subject: [PATCH] address comments Signed-off-by: Ryan Leung --- .gitignore | 1 + server/api/redirector.go | 24 ++++++++++++++---------- server/api/util.go | 6 +----- tests/pdctl/cluster/cluster_test.go | 1 + tests/pdctl/region/region_test.go | 1 + 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 7f16d2e8a40..e100d144118 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ tags vendor default* *.bak +.vscode/ diff --git a/server/api/redirector.go b/server/api/redirector.go index 43df4b18287..be53331b2a6 100644 --- a/server/api/redirector.go +++ b/server/api/redirector.go @@ -19,6 +19,7 @@ import ( "net/http" "net/url" "strings" + "sync" "github.com/pingcap/log" "github.com/pingcap/pd/server" @@ -35,6 +36,8 @@ const ( errRedirectToNotLeader = "redirect to not leader" ) +var initHTTPClientOnce sync.Once + type redirector struct { s *server.Server } @@ -69,12 +72,20 @@ func (h *redirector) ServeHTTP(w http.ResponseWriter, r *http.Request, next http http.Error(w, err.Error(), http.StatusInternalServerError) return } - tlsConfig, err := h.s.GetSecurityConfig().ToTLSConfig() + initHTTPClientOnce.Do(func() { + var tlsConfig *tls.Config + tlsConfig, err = h.s.GetSecurityConfig().ToTLSConfig() + dialClient = &http.Client{ + Transport: &http.Transport{ + TLSClientConfig: tlsConfig, + }, + } + }) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - newCustomReverseProxies(urls, tlsConfig).ServeHTTP(w, r) + newCustomReverseProxies(urls).ServeHTTP(w, r) } type customReverseProxies struct { @@ -82,14 +93,7 @@ type customReverseProxies struct { client *http.Client } -func newCustomReverseProxies(urls []url.URL, cfg *tls.Config) *customReverseProxies { - dialClient = &http.Client{ - Transport: &http.Transport{ - TLSClientConfig: cfg, - DisableKeepAlives: true, - }, - } - +func newCustomReverseProxies(urls []url.URL) *customReverseProxies { p := &customReverseProxies{ client: dialClient, } diff --git a/server/api/util.go b/server/api/util.go index bae81ae379d..57881c2d087 100644 --- a/server/api/util.go +++ b/server/api/util.go @@ -28,11 +28,7 @@ import ( ) // dialClient used to dail http request. -var dialClient = &http.Client{ - Transport: &http.Transport{ - DisableKeepAlives: true, - }, -} +var dialClient = &http.Client{} // Respond to the client about the given error, integrating with errcode.ErrorCode. // diff --git a/tests/pdctl/cluster/cluster_test.go b/tests/pdctl/cluster/cluster_test.go index e1c2148d622..5dbaadccc2d 100644 --- a/tests/pdctl/cluster/cluster_test.go +++ b/tests/pdctl/cluster/cluster_test.go @@ -71,6 +71,7 @@ func (s *clusterTestSuite) TestClusterAndPing(c *C) { temp.Close() os.Stdout = old out, _ := ioutil.ReadFile(fname) + os.Remove(fname) c.Assert(strings.Contains(string(out), "no such file or directory"), IsTrue) // cluster status diff --git a/tests/pdctl/region/region_test.go b/tests/pdctl/region/region_test.go index c916e2a6c2e..dacb279f95d 100644 --- a/tests/pdctl/region/region_test.go +++ b/tests/pdctl/region/region_test.go @@ -68,6 +68,7 @@ func (s *regionTestSuite) TestRegionKeyFormat(c *C) { temp.Close() os.Stdout = old out, _ := ioutil.ReadFile(fname) + os.Remove(fname) c.Assert(strings.Contains(string(out), "unknown flag"), IsFalse) }