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

feat: Configurable http request timout #100

Merged
merged 4 commits into from
Apr 24, 2020
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 1.1.0
## 1.1.0
### Features
1. [#100](https://github.com/influxdata/influxdb-client-go/pull/100) HTTP request timeout made configurable
1. [#99](https://github.com/influxdata/influxdb-client-go/pull/99) Organizations API and Users API
1. [#96](https://github.com/influxdata/influxdb-client-go/pull/96) Authorization API

Expand Down
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func NewClientWithOptions(serverUrl string, authToken string, options *Options)
serverUrl: serverUrl,
options: options,
writeApis: make([]WriteApi, 0, 5),
httpService: ihttp.NewService(serverUrl, "Token "+authToken, options.tlsConfig),
httpService: ihttp.NewService(serverUrl, "Token "+authToken, options.tlsConfig, options.httpRequestTimeout),
}
return client
}
Expand Down
4 changes: 2 additions & 2 deletions internal/http/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (s *serviceImpl) ServerApiUrl() string {
type RequestCallback func(req *http.Request)
type ResponseCallback func(resp *http.Response) error

func NewService(serverUrl, authorization string, tlsConfig *tls.Config) Service {
func NewService(serverUrl, authorization string, tlsConfig *tls.Config, httpRequestTimeout uint) Service {
apiUrl, err := url.Parse(serverUrl)
if err == nil {
apiUrl, err = apiUrl.Parse("/api/v2/")
Expand All @@ -54,7 +54,7 @@ func NewService(serverUrl, authorization string, tlsConfig *tls.Config) Service
serverApiUrl: serverUrl,
authorization: authorization,
client: &http.Client{
Timeout: time.Second * 20,
Timeout: time.Second * time.Duration(httpRequestTimeout),
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
Expand Down
17 changes: 15 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// Options holds configuration properties for communicating with InfluxDB server
type Options struct {
// Maximum number of points sent to server in single request. Default 1000
// Maximum number of points sent to server in single request. Default 5000
batchSize uint
// Interval, in ms, in which is buffer flushed if it has not been already written (by reaching batch size) . Default 1000ms
flushInterval uint
Expand All @@ -30,6 +30,8 @@ type Options struct {
useGZip bool
// TLS configuration for secure connection. Default nil
tlsConfig *tls.Config
// HTTP request timeout in sec. Default 20
httpRequestTimeout uint
}

// BatchSize returns size of batch
Expand Down Expand Up @@ -132,7 +134,18 @@ func (o *Options) SetTlsConfig(tlsConfig *tls.Config) *Options {
return o
}

// HttpRequestTimeout returns HTTP request timeout
func (o *Options) HttpRequestTimeout() uint {
return o.httpRequestTimeout
}

// SetHttpRequestTimeout sets HTTP request timeout in sec
func (o *Options) SetHttpRequestTimeout(httpRequestTimeout uint) *Options {
o.httpRequestTimeout = httpRequestTimeout
return o
}

// DefaultOptions returns Options object with default values
func DefaultOptions() *Options {
return &Options{batchSize: 1000, maxRetries: 3, retryInterval: 1000, flushInterval: 1000, precision: time.Nanosecond, useGZip: false, retryBufferLimit: 10000}
return &Options{batchSize: 5000, maxRetries: 3, retryInterval: 1000, flushInterval: 1000, precision: time.Nanosecond, useGZip: false, retryBufferLimit: 10000, httpRequestTimeout: 20}
}
49 changes: 49 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package influxdb2

import (
"context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)

func TestTimeout(t *testing.T) {
response := `,result,table,_start,_stop,_time,_value,_field,_measurement,a,b,
,,0,2020-02-17T22:19:49.747562847Z,2020-02-18T22:19:49.747562847Z,2020-02-18T10:34:08.135814545Z,1.4,f,test,1,adsfasdf
,,0,2020-02-17T22:19:49.747562847Z,2020-02-18T22:19:49.747562847Z,2020-02-18T22:08:44.850214724Z,6.6,f,test,1,adsfasdf
`
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(100 * time.Millisecond)
if r.Method == http.MethodPost {
w.Header().Set("Content-Type", "text/csv")
w.WriteHeader(http.StatusOK)
time.Sleep(2 * time.Second)
_, err := w.Write([]byte(response))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
}
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()
client := NewClientWithOptions(server.URL, "a", DefaultOptions().SetHttpRequestTimeout(1))
queryApi := client.QueryApi("org")

_, err := queryApi.QueryRaw(context.Background(), "flux", nil)
require.NotNil(t, err)
assert.True(t, strings.Contains(err.Error(), "Client.Timeout exceeded"))

client = NewClientWithOptions(server.URL, "a", DefaultOptions().SetHttpRequestTimeout(5))
queryApi = client.QueryApi("org")

result, err := queryApi.QueryRaw(context.Background(), "flux", nil)
require.Nil(t, err)
assert.Equal(t, response, result)

}