Skip to content

Commit

Permalink
server: use a different timeout for http clients (#1574)
Browse files Browse the repository at this point in the history
* use a different timeout for http clients
  • Loading branch information
rleungx authored Jun 11, 2019
1 parent dadede4 commit bb0634b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion server/api/redirector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
14 changes: 10 additions & 4 deletions server/api/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
8 changes: 4 additions & 4 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
const (
requestTimeout = etcdutil.DefaultRequestTimeout
slowRequestTime = etcdutil.DefaultSlowRequestTime
clientTimeout = 30 * time.Second
clientTimeout = 3 * time.Second
)

// Version information.
Expand All @@ -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,
Expand Down Expand Up @@ -274,7 +274,7 @@ func InitHTTPClient(svr *Server) error {
return err
}

DialClient = &http.Client{
dialClient = &http.Client{
Timeout: clientTimeout,
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
Expand Down
5 changes: 1 addition & 4 deletions tools/pd-ctl/pdctl/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ import (
"net/url"
"os"
"strings"
"time"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.etcd.io/etcd/pkg/transport"
)

var (
dialClient = &http.Client{Timeout: 30 * time.Second}

dialClient = &http.Client{}
pingPrefix = "pd/ping"
)

Expand All @@ -49,7 +47,6 @@ func InitHTTPSClient(CAPath, CertPath, KeyPath string) error {
}

dialClient = &http.Client{
Timeout: 30 * time.Second,
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
Expand Down

0 comments on commit bb0634b

Please sign in to comment.