Skip to content

Commit

Permalink
feat: allow overriding the http.Client for the http service
Browse files Browse the repository at this point in the history
This allows overriding the http.Client for the http service. If the
client is overridden, the other options are ignored. This allows further
customization such as modifying the underlying http.Transport or the
`CheckRedirect` function.
  • Loading branch information
jsternberg committed Jul 28, 2020
1 parent d53b738 commit 126997f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 14 deletions.
35 changes: 35 additions & 0 deletions api/http/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,51 @@ package http

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

// Options holds http configuration properties for communicating with InfluxDB server
type Options struct {
// HTTP client. Default is http.DefaultClient.
httpClient *http.Client
// TLS configuration for secure connection. Default nil
tlsConfig *tls.Config
// HTTP request timeout in sec. Default 20
httpRequestTimeout uint
}

// HTTPClient returns the http.Client that is configured to be used
// for HTTP requests. It will return the one that has been set using
// SetHTTPClient or it will construct a default client using the
// other configured options.
func (o *Options) HTTPClient() *http.Client {
if o.httpClient != nil {
return o.httpClient
}
return &http.Client{
Timeout: time.Second * time.Duration(o.HTTPRequestTimeout()),
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 5 * time.Second,
TLSClientConfig: o.TLSConfig(),
},
}
}

// SetHTTPClient will configure the http.Client that is used
// for HTTP requests. If set to nil, an HTTPClient will be
// generated.
//
// Setting the HTTPClient will cause the other HTTP options
// to be ignored.
func (o *Options) SetHTTPClient(c *http.Client) {
o.httpClient = c
}

// TLSConfig returns tls.Config
func (o *Options) TLSConfig() *tls.Config {
return o.tlsConfig
Expand Down
16 changes: 15 additions & 1 deletion api/http/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ package http_test

import (
"crypto/tls"
nethttp "net/http"
"testing"
"time"

"github.com/influxdata/influxdb-client-go/api/http"
"github.com/stretchr/testify/assert"
"testing"
)

func TestDefaultOptions(t *testing.T) {
Expand All @@ -26,4 +29,15 @@ func TestOptionsSetting(t *testing.T) {
SetHTTPRequestTimeout(50)
assert.Equal(t, tlsConfig, opts.TLSConfig())
assert.Equal(t, uint(50), opts.HTTPRequestTimeout())
if client := opts.HTTPClient(); assert.NotNil(t, client) {
assert.Equal(t, 50*time.Second, client.Timeout)
assert.Equal(t, tlsConfig, client.Transport.(*nethttp.Transport).TLSClientConfig)
}

client := &nethttp.Client{
Transport: &nethttp.Transport{},
}
opts = http.DefaultOptions()
opts.SetHTTPClient(client)
assert.Equal(t, client, opts.HTTPClient())
}
13 changes: 1 addition & 12 deletions internal/http/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import (
"io"
"io/ioutil"
"mime"
"net"
"net/http"
"net/url"
"strconv"
"time"

http2 "github.com/influxdata/influxdb-client-go/api/http"
)
Expand Down Expand Up @@ -61,16 +59,7 @@ func NewService(serverURL, authorization string, httpOptions *http2.Options) Ser
serverAPIURL: serverAPIURL,
serverURL: serverURL,
authorization: authorization,
client: &http.Client{
Timeout: time.Second * time.Duration(httpOptions.HTTPRequestTimeout()),
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 5 * time.Second,
TLSClientConfig: httpOptions.TLSConfig(),
},
},
client: httpOptions.HTTPClient(),
}
}

Expand Down
22 changes: 21 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ package influxdb2

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

"github.com/influxdata/influxdb-client-go/api/http"
"github.com/influxdata/influxdb-client-go/api/write"
"time"
)

// Options holds configuration properties for communicating with InfluxDB server
Expand Down Expand Up @@ -110,6 +112,24 @@ func (o *Options) SetUseGZip(useGZip bool) *Options {
return o
}

// HTTPClient returns the http.Client that is configured to be used
// for HTTP requests. It will return the one that has been set using
// SetHTTPClient or it will construct a default client using the
// other configured options.
func (o *Options) HTTPClient() *nethttp.Client {
return o.httpOptions.HTTPClient()
}

// SetHTTPClient will configure the http.Client that is used
// for HTTP requests. If set to nil, an HTTPClient will be
// generated.
//
// Setting the HTTPClient will cause the other HTTP options
// to be ignored.
func (o *Options) SetHTTPClient(c *nethttp.Client) {
o.httpOptions.SetHTTPClient(c)
}

// TLSConfig returns TLS config
func (o *Options) TLSConfig() *tls.Config {
return o.HTTPOptions().TLSConfig()
Expand Down

0 comments on commit 126997f

Please sign in to comment.