Skip to content

Commit

Permalink
server: use a different timeout for http clients (#1574) (#1576)
Browse files Browse the repository at this point in the history
* use a different timeout for http clients
  • Loading branch information
rleungx authored and disksing committed Jul 2, 2019
1 parent 7df2fde commit 43a6ff8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
11 changes: 6 additions & 5 deletions pdctl/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ import (
)

var (
dialClient = &http.Client{}

dialClient = &http.Client{}
pingPrefix = "pd/ping"
errInvalidAddr = errors.New("Invalid pd address, Cannot get connect to it")
)
Expand All @@ -47,9 +46,11 @@ func InitHTTPSClient(CAPath, CertPath, KeyPath string) error {
return errors.Trace(err)
}

dialClient = &http.Client{Transport: &http.Transport{
TLSClientConfig: tlsConfig,
}}
dialClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
}

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion server/api/redirector.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type customReverseProxies struct {

func newCustomReverseProxies(urls []url.URL) *customReverseProxies {
p := &customReverseProxies{
client: server.DialClient,
client: dialClient,
}

p.urls = append(p.urls, urls...)
Expand Down
14 changes: 10 additions & 4 deletions server/api/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ import (
"net/http"

"github.com/juju/errors"
"github.com/pingcap/pd/server"
)

// dialClient used to dail http request.
var dialClient = &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
},
}

func readJSON(r io.ReadCloser, data interface{}) error {
defer r.Close()

Expand All @@ -40,7 +46,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.Trace(err)
}
Expand All @@ -60,7 +66,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
}
Expand All @@ -69,7 +75,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.Trace(err)
}
Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,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()
}
Expand Down
19 changes: 12 additions & 7 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ const (
defaultLogMaxAge = 28 // days
defaultLogLevel = log.InfoLevel

logDirMode = 0755
logDirMode = 0755
clientTimeout = 3 * time.Second
)

// Version information.
Expand All @@ -50,8 +51,9 @@ 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,
},
Expand Down Expand Up @@ -265,9 +267,12 @@ func InitHTTPClient(svr *Server) error {
return errors.Trace(err)
}

DialClient = &http.Client{Transport: &http.Transport{
TLSClientConfig: tlsConfig,
DisableKeepAlives: true,
}}
dialClient = &http.Client{
Timeout: clientTimeout,
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
DisableKeepAlives: true,
},
}
return nil
}

0 comments on commit 43a6ff8

Please sign in to comment.