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

refactor(depedencies): use http client interface #2086

Merged
merged 1 commit into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 5 additions & 26 deletions dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package flux

import (
"context"
"net"
"net/http"
"time"

"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/dependencies/filesystem"
"github.com/influxdata/flux/dependencies/http"
"github.com/influxdata/flux/dependencies/secret"
"github.com/influxdata/flux/dependencies/url"
"github.com/influxdata/flux/internal/errors"
Expand All @@ -26,7 +24,7 @@ const dependenciesKey key = iota

type Dependencies interface {
Dependency
HTTPClient() (*http.Client, error)
HTTPClient() (http.Client, error)
FilesystemService() (filesystem.Service, error)
SecretService() (secret.Service, error)
URLValidator() (url.Validator, error)
Expand All @@ -39,13 +37,13 @@ type Deps struct {
}

type WrappedDeps struct {
HTTPClient *http.Client
HTTPClient http.Client
FilesystemService filesystem.Service
SecretService secret.Service
URLValidator url.Validator
}

func (d Deps) HTTPClient() (*http.Client, error) {
func (d Deps) HTTPClient() (http.Client, error) {
if d.Deps.HTTPClient != nil {
return d.Deps.HTTPClient, nil
}
Expand Down Expand Up @@ -85,31 +83,12 @@ func GetDependencies(ctx context.Context) Dependencies {
return deps.(Dependencies)
}

// 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 is deprecated
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 10 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
// Fields below are NOT part of Go's defaults
MaxIdleConnsPerHost: 100,
}
}

// NewDefaultDependencies produces a set of dependencies.
// Not all dependencies have valid defaults and will not be set.
func NewDefaultDependencies() Deps {
return Deps{
Deps: WrappedDeps{
HTTPClient: &http.Client{Transport: newDefaultTransport()},
HTTPClient: http.NewDefaultClient(),
// Default to having no filesystem, no secrets, and no url validation (always pass).
FilesystemService: nil,
SecretService: secret.EmptySecretService{},
Expand Down
32 changes: 32 additions & 0 deletions dependencies/http/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package http

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

type Client interface {
Do(*http.Request) (*http.Response, error)
}

// NewDefaultTransport creates a new transport with sane defaults.
func NewDefaultClient() *http.Client {
// These defaults are copied from http.DefaultTransport.
return &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
// DualStack is deprecated
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 10 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
// Fields below are NOT part of Go's defaults
MaxIdleConnsPerHost: 100,
},
}
}
10 changes: 10 additions & 0 deletions dependencies/http/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package http

import "testing"

func TestNewDefaultClient(t *testing.T) {
c := NewDefaultClient()
if c == nil {
t.Fail()
}
}