Skip to content

Commit

Permalink
Merge pull request #165 from jsternberg/set-http-client
Browse files Browse the repository at this point in the history
feat: allow overriding the http.Client for the http service
  • Loading branch information
vlastahajek authored Jul 29, 2020
2 parents d53b738 + 2028a4e commit 945b8fb
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## 1.5.0 [In progress]
1. [#165](https://github.com/influxdata/influxdb-client-go/pull/165) Allow overriding the http.Client for the http service.

## 1.4.0 [2020-07-17]
### Breaking changes
Expand Down
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 {
o.httpClient = &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(),
},
}
}
return o.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 *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
17 changes: 14 additions & 3 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package influxdb2_test
import (
"context"
"crypto/tls"
influxdb2 "github.com/influxdata/influxdb-client-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

influxdb2 "github.com/influxdata/influxdb-client-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDefaultOptions(t *testing.T) {
Expand Down Expand Up @@ -52,8 +53,18 @@ func TestSettingsOptions(t *testing.T) {
assert.Equal(t, uint(7), opts.MaxRetries())
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.(*http.Transport).TLSClientConfig)
}
assert.Equal(t, uint(3), opts.LogLevel())
assert.Len(t, opts.WriteOptions().DefaultTags(), 1)

client := &http.Client{
Transport: &http.Transport{},
}
opts.SetHTTPClient(client)
assert.Equal(t, client, opts.HTTPClient())
}

func TestTimeout(t *testing.T) {
Expand Down

0 comments on commit 945b8fb

Please sign in to comment.