Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <[email protected]>
  • Loading branch information
rleungx committed Sep 10, 2019
1 parent c51839d commit 2b688d2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ tags
vendor
default*
*.bak
.vscode/
24 changes: 14 additions & 10 deletions server/api/redirector.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"net/http"
"net/url"
"strings"
"sync"

"github.com/pingcap/log"
"github.com/pingcap/pd/server"
Expand All @@ -35,6 +36,8 @@ const (
errRedirectToNotLeader = "redirect to not leader"
)

var initHTTPClientOnce sync.Once

type redirector struct {
s *server.Server
}
Expand Down Expand Up @@ -69,27 +72,28 @@ 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 {
urls []url.URL
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,
}
Expand Down
6 changes: 1 addition & 5 deletions server/api/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down
1 change: 1 addition & 0 deletions tests/pdctl/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/pdctl/region/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 2b688d2

Please sign in to comment.