Skip to content

Commit

Permalink
Merge b6615bd into ddd358a
Browse files Browse the repository at this point in the history
  • Loading branch information
disksing committed Apr 24, 2017
2 parents ddd358a + b6615bd commit dc5cc57
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pd-client/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (c *conn) connectLeader(urls []string, interval time.Duration) {

func getLeader(urls []string) (*pdpb.Leader, error) {
for _, u := range urls {
client, err := apiutil.NewClient(u, connectPDTimeout)
client, err := apiutil.NewClient(u)
if err != nil {
continue
}
Expand Down
36 changes: 33 additions & 3 deletions pkg/apiutil/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package apiutil

import (
"net"
"net/http"
"net/url"
"time"
Expand All @@ -23,7 +24,8 @@ import (
)

const (
apiPrefix = "/pd/api/v1"
apiPrefix = "/pd/api/v1"
apiClientTimeout = time.Second * 3
)

// Client is a client to access PD APIs.
Expand All @@ -33,7 +35,7 @@ type Client struct {
}

// NewClient returns a client to access PD APIs.
func NewClient(addr string, timeout time.Duration) (*Client, error) {
func NewClient(addr string) (*Client, error) {
u, err := url.Parse(addr)
if err != nil {
return nil, errors.Trace(err)
Expand All @@ -45,8 +47,13 @@ func NewClient(addr string, timeout time.Duration) (*Client, error) {
u.Scheme = "http"
}

hc := clients[scheme]
if hc == nil {
hc = clients["http"]
}

client := &Client{
hc: NewHTTPClient(scheme, timeout),
hc: hc,
url: u.String(),
}
return client, nil
Expand All @@ -68,3 +75,26 @@ func (c *Client) GetLeader() (*pdpb.Leader, error) {
}
return leader, nil
}

var clients map[string]*http.Client

func init() {
httpClient := &http.Client{
Timeout: apiClientTimeout,
}
unixClient := &http.Client{
Timeout: apiClientTimeout,
Transport: &http.Transport{
Dial: func(_, addr string) (net.Conn, error) {
return net.Dial("unix", addr)
},
},
}

clients = map[string]*http.Client{
"http": httpClient,
"https": httpClient,
"unix": unixClient,
"unixs": unixClient,
}
}

0 comments on commit dc5cc57

Please sign in to comment.