diff --git a/CHANGELOG.md b/CHANGELOG.md index 46bd7b15..7a325216 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/client.go b/client.go index 94b87ccf..990c47d7 100644 --- a/client.go +++ b/client.go @@ -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 } diff --git a/internal/http/service.go b/internal/http/service.go index f8993b0d..83ad6b2d 100644 --- a/internal/http/service.go +++ b/internal/http/service.go @@ -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/") @@ -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, diff --git a/options.go b/options.go index 4d6eba0c..52128679 100644 --- a/options.go +++ b/options.go @@ -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 @@ -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 @@ -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} } diff --git a/options_test.go b/options_test.go new file mode 100644 index 00000000..4ff4e4a3 --- /dev/null +++ b/options_test.go @@ -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) + +}