Skip to content

Commit 126997f

Browse files
committed
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.
1 parent d53b738 commit 126997f

File tree

4 files changed

+72
-14
lines changed

4 files changed

+72
-14
lines changed

api/http/options.go

+35
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,51 @@ package http
77

88
import (
99
"crypto/tls"
10+
"net"
11+
"net/http"
12+
"time"
1013
)
1114

1215
// Options holds http configuration properties for communicating with InfluxDB server
1316
type Options struct {
17+
// HTTP client. Default is http.DefaultClient.
18+
httpClient *http.Client
1419
// TLS configuration for secure connection. Default nil
1520
tlsConfig *tls.Config
1621
// HTTP request timeout in sec. Default 20
1722
httpRequestTimeout uint
1823
}
1924

25+
// HTTPClient returns the http.Client that is configured to be used
26+
// for HTTP requests. It will return the one that has been set using
27+
// SetHTTPClient or it will construct a default client using the
28+
// other configured options.
29+
func (o *Options) HTTPClient() *http.Client {
30+
if o.httpClient != nil {
31+
return o.httpClient
32+
}
33+
return &http.Client{
34+
Timeout: time.Second * time.Duration(o.HTTPRequestTimeout()),
35+
Transport: &http.Transport{
36+
DialContext: (&net.Dialer{
37+
Timeout: 5 * time.Second,
38+
}).DialContext,
39+
TLSHandshakeTimeout: 5 * time.Second,
40+
TLSClientConfig: o.TLSConfig(),
41+
},
42+
}
43+
}
44+
45+
// SetHTTPClient will configure the http.Client that is used
46+
// for HTTP requests. If set to nil, an HTTPClient will be
47+
// generated.
48+
//
49+
// Setting the HTTPClient will cause the other HTTP options
50+
// to be ignored.
51+
func (o *Options) SetHTTPClient(c *http.Client) {
52+
o.httpClient = c
53+
}
54+
2055
// TLSConfig returns tls.Config
2156
func (o *Options) TLSConfig() *tls.Config {
2257
return o.tlsConfig

api/http/options_test.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ package http_test
66

77
import (
88
"crypto/tls"
9+
nethttp "net/http"
10+
"testing"
11+
"time"
12+
913
"github.com/influxdata/influxdb-client-go/api/http"
1014
"github.com/stretchr/testify/assert"
11-
"testing"
1215
)
1316

1417
func TestDefaultOptions(t *testing.T) {
@@ -26,4 +29,15 @@ func TestOptionsSetting(t *testing.T) {
2629
SetHTTPRequestTimeout(50)
2730
assert.Equal(t, tlsConfig, opts.TLSConfig())
2831
assert.Equal(t, uint(50), opts.HTTPRequestTimeout())
32+
if client := opts.HTTPClient(); assert.NotNil(t, client) {
33+
assert.Equal(t, 50*time.Second, client.Timeout)
34+
assert.Equal(t, tlsConfig, client.Transport.(*nethttp.Transport).TLSClientConfig)
35+
}
36+
37+
client := &nethttp.Client{
38+
Transport: &nethttp.Transport{},
39+
}
40+
opts = http.DefaultOptions()
41+
opts.SetHTTPClient(client)
42+
assert.Equal(t, client, opts.HTTPClient())
2943
}

internal/http/service.go

+1-12
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ import (
1111
"io"
1212
"io/ioutil"
1313
"mime"
14-
"net"
1514
"net/http"
1615
"net/url"
1716
"strconv"
18-
"time"
1917

2018
http2 "github.com/influxdata/influxdb-client-go/api/http"
2119
)
@@ -61,16 +59,7 @@ func NewService(serverURL, authorization string, httpOptions *http2.Options) Ser
6159
serverAPIURL: serverAPIURL,
6260
serverURL: serverURL,
6361
authorization: authorization,
64-
client: &http.Client{
65-
Timeout: time.Second * time.Duration(httpOptions.HTTPRequestTimeout()),
66-
Transport: &http.Transport{
67-
DialContext: (&net.Dialer{
68-
Timeout: 5 * time.Second,
69-
}).DialContext,
70-
TLSHandshakeTimeout: 5 * time.Second,
71-
TLSClientConfig: httpOptions.TLSConfig(),
72-
},
73-
},
62+
client: httpOptions.HTTPClient(),
7463
}
7564
}
7665

options.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ package influxdb2
66

77
import (
88
"crypto/tls"
9+
nethttp "net/http"
10+
"time"
11+
912
"github.com/influxdata/influxdb-client-go/api/http"
1013
"github.com/influxdata/influxdb-client-go/api/write"
11-
"time"
1214
)
1315

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

115+
// HTTPClient returns the http.Client that is configured to be used
116+
// for HTTP requests. It will return the one that has been set using
117+
// SetHTTPClient or it will construct a default client using the
118+
// other configured options.
119+
func (o *Options) HTTPClient() *nethttp.Client {
120+
return o.httpOptions.HTTPClient()
121+
}
122+
123+
// SetHTTPClient will configure the http.Client that is used
124+
// for HTTP requests. If set to nil, an HTTPClient will be
125+
// generated.
126+
//
127+
// Setting the HTTPClient will cause the other HTTP options
128+
// to be ignored.
129+
func (o *Options) SetHTTPClient(c *nethttp.Client) {
130+
o.httpOptions.SetHTTPClient(c)
131+
}
132+
113133
// TLSConfig returns TLS config
114134
func (o *Options) TLSConfig() *tls.Config {
115135
return o.HTTPOptions().TLSConfig()

0 commit comments

Comments
 (0)