From 126997f842b695b3dfdd8701dfd53fd41e3e7102 Mon Sep 17 00:00:00 2001 From: "Jonathan A. Sternberg" Date: Tue, 28 Jul 2020 12:11:53 -0500 Subject: [PATCH] feat: allow overriding the http.Client for the http service 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. --- api/http/options.go | 35 +++++++++++++++++++++++++++++++++++ api/http/options_test.go | 16 +++++++++++++++- internal/http/service.go | 13 +------------ options.go | 22 +++++++++++++++++++++- 4 files changed, 72 insertions(+), 14 deletions(-) diff --git a/api/http/options.go b/api/http/options.go index e8377d05..696c184a 100644 --- a/api/http/options.go +++ b/api/http/options.go @@ -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 diff --git a/api/http/options_test.go b/api/http/options_test.go index d27388df..d8e24203 100644 --- a/api/http/options_test.go +++ b/api/http/options_test.go @@ -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) { @@ -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()) } diff --git a/internal/http/service.go b/internal/http/service.go index 34206c26..f5bac8ac 100644 --- a/internal/http/service.go +++ b/internal/http/service.go @@ -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" ) @@ -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(), } } diff --git a/options.go b/options.go index a9096eba..efbfd117 100644 --- a/options.go +++ b/options.go @@ -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 @@ -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()