Skip to content

Commit

Permalink
Merge pull request #1920 from weaveworks/probe-client-timeouts
Browse files Browse the repository at this point in the history
Add timeouts to the probe http client
  • Loading branch information
paulbellamy authored Oct 13, 2016
2 parents 1d3ae95 + 002770d commit 8da5ca3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
25 changes: 9 additions & 16 deletions probe/appclient/app_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/rpc"
"net/url"
Expand All @@ -13,6 +12,7 @@ import (

log "github.com/Sirupsen/logrus"
"github.com/gorilla/websocket"
"github.com/hashicorp/go-cleanhttp"
"github.com/ugorji/go/codec"

"github.com/weaveworks/scope/common/xfer"
Expand All @@ -22,7 +22,6 @@ const (
httpClientTimeout = 4 * time.Second
initialBackoff = 1 * time.Second
maxBackoff = 60 * time.Second
dialTimeout = 5 * time.Second
)

// AppClient is a client to an app for dealing with controls.
Expand All @@ -41,7 +40,7 @@ type appClient struct {

quit chan struct{}
mtx sync.Mutex
client http.Client
client *http.Client
wsDialer websocket.Dialer
appID string
hostname string
Expand All @@ -63,26 +62,20 @@ type appClient struct {

// NewAppClient makes a new appClient.
func NewAppClient(pc ProbeConfig, hostname string, target url.URL, control xfer.ControlHandler) (AppClient, error) {
httpTransport, err := pc.getHTTPTransport(hostname)
if err != nil {
return nil, err
}

httpTransport.Dial = func(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, dialTimeout)
}
httpTransport := pc.getHTTPTransport(hostname)
httpClient := cleanhttp.DefaultClient()
httpClient.Transport = httpTransport
httpClient.Timeout = httpClientTimeout

return &appClient{
ProbeConfig: pc,
quit: make(chan struct{}),
hostname: hostname,
target: target,
client: http.Client{
Transport: httpTransport,
Timeout: httpClientTimeout,
},
client: httpClient,
wsDialer: websocket.Dialer{
TLSClientConfig: httpTransport.TLSClientConfig,
TLSClientConfig: httpTransport.TLSClientConfig,
HandshakeTimeout: httpClientTimeout,
},
conns: map[string]xfer.Websocket{},
readers: make(chan io.Reader, 2),
Expand Down
30 changes: 20 additions & 10 deletions probe/appclient/probe_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ import (
"crypto/x509"
"fmt"
"io"
"net"
"net/http"
"time"

"github.com/certifi/gocertifi"
"github.com/hashicorp/go-cleanhttp"

"github.com/weaveworks/scope/common/xfer"
)

const (
dialTimeout = 5 * time.Second
)

var certPool *x509.CertPool

func init() {
Expand Down Expand Up @@ -43,17 +51,19 @@ func (pc ProbeConfig) authorizedRequest(method string, urlStr string, body io.Re
return req, err
}

func (pc ProbeConfig) getHTTPTransport(hostname string) (*http.Transport, error) {
func (pc ProbeConfig) getHTTPTransport(hostname string) *http.Transport {
transport := cleanhttp.DefaultTransport()
transport.DialContext = (&net.Dialer{
Timeout: dialTimeout,
KeepAlive: 30 * time.Second,
}).DialContext
if pc.Insecure {
return &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}, nil
}

return &http.Transport{
TLSClientConfig: &tls.Config{
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
} else {
transport.TLSClientConfig = &tls.Config{
RootCAs: certPool,
ServerName: hostname,
},
}, nil
}
}
return transport
}

0 comments on commit 8da5ca3

Please sign in to comment.