Skip to content

Commit

Permalink
fix(http): use default transport consitently
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielc committed Mar 7, 2019
1 parent 3086452 commit 3ae1ea1
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 43 deletions.
10 changes: 4 additions & 6 deletions client/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"time"

"github.com/influxdata/influxdb/influxql"
khttp "github.com/influxdata/kapacitor/http"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -176,12 +177,9 @@ func New(conf Config) (*Client, error) {
var tr *http.Transport

if rt == nil {
tr = &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: conf.InsecureSkipVerify,
},
}
tr = khttp.NewDefaultTransportWithTLS(&tls.Config{
InsecureSkipVerify: conf.InsecureSkipVerify,
})
if conf.TLSConfig != nil {
tr.TLSClientConfig = conf.TLSConfig
}
Expand Down
34 changes: 34 additions & 0 deletions http/transport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package http

import (
"crypto/tls"
"net"
"net/http"
"time"
)

// NewDefaultTransport creates a new transport with sane defaults.
func NewDefaultTransport() *http.Transport {
// These defaults are copied from http.DefaultTransport.
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
// Below are changes from http.DefaultTransport
MaxIdleConnsPerHost: 100, // increased from 2, services tend to connect to a single host
}
}

// NewDefaultTransportWithTLS creates a new transport with the specified TLS configuration.
func NewDefaultTransportWithTLS(tlsConfig *tls.Config) *http.Transport {
t := NewDefaultTransport()
t.TLSClientConfig = tlsConfig
return t
}
5 changes: 2 additions & 3 deletions influxdb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

imodels "github.com/influxdata/influxdb/models"
khttp "github.com/influxdata/kapacitor/http"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -123,9 +124,7 @@ func NewHTTPClient(conf Config) (*HTTPClient, error) {
return nil, errors.Wrap(err, "invalid URLs")
}
if conf.Transport == nil {
conf.Transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
}
conf.Transport = khttp.NewDefaultTransport()
}
c := &HTTPClient{
config: conf,
Expand Down
11 changes: 3 additions & 8 deletions services/alerta/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/influxdata/kapacitor/alert"
khttp "github.com/influxdata/kapacitor/http"
"github.com/influxdata/kapacitor/keyvalue"
"github.com/influxdata/kapacitor/models"
"github.com/pkg/errors"
Expand Down Expand Up @@ -45,10 +46,7 @@ func NewService(c Config, d Diagnostic) *Service {
}
s.configValue.Store(c)
s.clientValue.Store(&http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{InsecureSkipVerify: c.InsecureSkipVerify},
},
Transport: khttp.NewDefaultTransportWithTLS(&tls.Config{InsecureSkipVerify: c.InsecureSkipVerify}),
})
return s
}
Expand Down Expand Up @@ -128,10 +126,7 @@ func (s *Service) Update(newConfig []interface{}) error {
} else {
s.configValue.Store(c)
s.clientValue.Store(&http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{InsecureSkipVerify: c.InsecureSkipVerify},
},
Transport: khttp.NewDefaultTransportWithTLS(&tls.Config{InsecureSkipVerify: c.InsecureSkipVerify}),
})
}

Expand Down
6 changes: 2 additions & 4 deletions services/influxdb/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/cenkalti/backoff"
"github.com/influxdata/influxdb/influxql"
"github.com/influxdata/influxdb/models"
khttp "github.com/influxdata/kapacitor/http"
"github.com/influxdata/kapacitor/influxdb"
"github.com/influxdata/kapacitor/keyvalue"
"github.com/influxdata/kapacitor/server/vars"
Expand Down Expand Up @@ -501,10 +502,7 @@ func httpConfig(c Config) (influxdb.Config, error) {
if err != nil {
return influxdb.Config{}, errors.Wrap(err, "invalid TLS options")
}
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: tlsConfig,
}
tr := khttp.NewDefaultTransportWithTLS(tlsConfig)
var credentials influxdb.Credentials
if c.Username != "" {
credentials = influxdb.Credentials{
Expand Down
11 changes: 3 additions & 8 deletions services/k8s/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"sync"
"sync/atomic"

khttp "github.com/influxdata/kapacitor/http"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -123,10 +124,7 @@ func New(c Config) (Client, error) {
config: c,
urls: urls,
client: &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: c.TLSConfig,
},
Transport: khttp.NewDefaultTransportWithTLS(c.TLSConfig),
},
}, nil
}
Expand Down Expand Up @@ -168,10 +166,7 @@ func (c *httpClient) Update(new Config) error {

if old.TLSConfig != new.TLSConfig {
c.client = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: new.TLSConfig,
},
Transport: khttp.NewDefaultTransportWithTLS(new.TLSConfig),
}
}
return nil
Expand Down
11 changes: 3 additions & 8 deletions services/slack/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sync"

"github.com/influxdata/kapacitor/alert"
khttp "github.com/influxdata/kapacitor/http"
"github.com/influxdata/kapacitor/keyvalue"
"github.com/influxdata/kapacitor/tlsconfig"
"github.com/pkg/errors"
Expand All @@ -37,10 +38,7 @@ func NewWorkspace(c Config) (*Workspace, error) {
}

cl := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: tlsConfig,
},
Transport: khttp.NewDefaultTransportWithTLS(tlsConfig),
}

return &Workspace{
Expand Down Expand Up @@ -68,10 +66,7 @@ func (w *Workspace) Update(c Config) error {
}

cl := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: tlsConfig,
},
Transport: khttp.NewDefaultTransportWithTLS(tlsConfig),
}

w.client = cl
Expand Down
9 changes: 3 additions & 6 deletions services/swarm/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
khttp "github.com/influxdata/kapacitor/http"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -52,9 +53,7 @@ func New(c Config) (Client, error) {
config: c,
urls: urls,
client: &http.Client{
Transport: &http.Transport{
TLSClientConfig: c.TLSConfig,
},
Transport: khttp.NewDefaultTransportWithTLS(c.TLSConfig),
},
}, nil
}
Expand Down Expand Up @@ -92,9 +91,7 @@ func (c *httpClient) Update(new Config) error {

if old.TLSConfig != new.TLSConfig {
c.client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: new.TLSConfig,
},
Transport: khttp.NewDefaultTransportWithTLS(new.TLSConfig),
}
}
return nil
Expand Down

0 comments on commit 3ae1ea1

Please sign in to comment.