Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: fix health status with tls enabled #969

Merged
merged 8 commits into from
Mar 5, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
reuse dialClient in redriector
Connor1996 committed Mar 2, 2018
commit f4bec2939fd2905002e23adcb985a37e5077f0db
16 changes: 3 additions & 13 deletions server/api/redirector.go
Original file line number Diff line number Diff line change
@@ -14,7 +14,6 @@
package api

import (
"crypto/tls"
"io/ioutil"
"net/http"
"net/url"
@@ -68,26 +67,17 @@ func (h *redirector) ServeHTTP(w http.ResponseWriter, r *http.Request, next http
return
}

tlsConfig, err := h.s.GetSecurityConfig().ToTLSConfig()
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, tlsConfig *tls.Config) *customReverseProxies {
func newCustomReverseProxies(urls []url.URL) *customReverseProxies {
p := &customReverseProxies{
client: &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
},
client: dialClient,
}

p.urls = append(p.urls, urls...)
7 changes: 5 additions & 2 deletions server/api/server.go
Original file line number Diff line number Diff line change
@@ -24,6 +24,8 @@ import (

const apiPrefix = "/pd"

var dialClient = &http.Client{}

// NewHandler creates a HTTP handler for API.
func NewHandler(svr *server.Server) http.Handler {
engine := negroni.New()
@@ -49,8 +51,9 @@ func InitHTTPClient(svr *server.Server) error {
return errors.Trace(err)
}

client = &http.Client{Transport: &http.Transport{
TLSClientConfig: tlsConfig,
dialClient = &http.Client{Transport: &http.Transport{
TLSClientConfig: tlsConfig,
DisableKeepAlives: true,
}}
return nil
}
8 changes: 3 additions & 5 deletions server/api/util.go
Original file line number Diff line number Diff line change
@@ -23,8 +23,6 @@ import (
"github.com/juju/errors"
)

var client = &http.Client{}

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

@@ -41,7 +39,7 @@ func readJSON(r io.ReadCloser, data interface{}) error {
}

func postJSON(url string, data []byte) error {
resp, err := client.Post(url, "application/json", bytes.NewBuffer(data))
resp, err := dialClient.Post(url, "application/json", bytes.NewBuffer(data))
if err != nil {
return errors.Trace(err)
}
@@ -61,7 +59,7 @@ func doDelete(url string) error {
if err != nil {
return err
}
res, err := client.Do(req)
res, err := dialClient.Do(req)
if err != nil {
return err
}
@@ -74,7 +72,7 @@ func doGet(url string) (*http.Response, error) {
if err != nil {
return nil, err
}
resp, err := client.Do(req)
resp, err := dialClient.Do(req)
if err != nil {
return nil, errors.Trace(err)
}