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

Include client timeout configuration #2684

Merged
merged 1 commit into from
Jun 5, 2015
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- [1997](https://github.com/influxdb/influxdb/pull/1997): Update SELECT * to return tag values.
- [2599](https://github.com/influxdb/influxdb/issues/2599): Add "epoch" URL param and return JSON time values as epoch instead of date strings.
- [2682](https://github.com/influxdb/influxdb/issues/2682): Adding pr checklist to CONTRIBUTING.md
- [2684](https://github.com/influxdb/influxdb/pull/2684): Include client timeout configuration. Thanks @vladlopes

### Bugfixes
- [2635](https://github.com/influxdb/influxdb/issues/2635): Fix querying against boolean field in WHERE clause.
Expand Down
4 changes: 3 additions & 1 deletion client/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ type Query struct {
// URL: The URL of the server connecting to.
// Username/Password are optional. They will be passed via basic auth if provided.
// UserAgent: If not provided, will default "InfluxDBClient",
//Timeout: If not provided, will default to 0 (no timeout)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: whitespace before the letter 'T'.

type Config struct {
URL url.URL
Username string
Password string
UserAgent string
Timeout time.Duration
}

// Client is used to make calls to the server.
Expand All @@ -45,7 +47,7 @@ func NewClient(c Config) (*Client, error) {
url: c.URL,
username: c.Username,
password: c.Password,
httpClient: http.DefaultClient,
httpClient: &http.Client{Timeout: c.Timeout},
userAgent: c.UserAgent,
}
if client.userAgent == "" {
Expand Down
32 changes: 32 additions & 0 deletions client/influxdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,35 @@ func TestBatchPoints_Normal(t *testing.T) {
t.Errorf("failed to unmarshal nanosecond data: %s", err.Error())
}
}

func TestClient_Timeout(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(1 * time.Second)
var data influxdb.Response
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(data)
}))
defer ts.Close()

u, _ := url.Parse(ts.URL)
config := client.Config{URL: *u, Timeout: 500 * time.Millisecond}
c, err := client.NewClient(config)
if err != nil {
t.Fatalf("unexpected error. expected %v, actual %v", nil, err)
}

query := client.Query{}
_, err = c.Query(query)
if err == nil {
t.Fatalf("unexpected success. expected timeout error")
} else if !strings.Contains(err.Error(), "use of closed network connection") {
t.Fatalf("unexpected error. expected 'use of closed network connection' error, got %v", err)
}

confignotimeout := client.Config{URL: *u}
cnotimeout, err := client.NewClient(confignotimeout)
_, err = cnotimeout.Query(query)
if err != nil {
t.Fatalf("unexpected error. expected %v, actual %v", nil, err)
}
}