diff --git a/influxdb.go b/influxdb.go index fd05cf5afd..4827e0ce25 100644 --- a/influxdb.go +++ b/influxdb.go @@ -2,6 +2,7 @@ package influxdb import ( "bytes" + "compress/gzip" "encoding/json" "fmt" "io" @@ -11,12 +12,13 @@ import ( ) type Client struct { - host string - username string - password string - database string - httpClient *http.Client - schema string + host string + username string + password string + database string + httpClient *http.Client + schema string + compression bool } type ClientConfig struct { @@ -60,7 +62,11 @@ func NewClient(config *ClientConfig) (*Client, error) { if config.IsSecure { schema = "https" } - return &Client{host, username, password, database, config.HttpClient, schema}, nil + return &Client{host, username, password, database, config.HttpClient, schema, true}, nil +} + +func (self *Client) DisableCompression() { + self.compression = false } func (self *Client) getUrl(path string) string { @@ -330,7 +336,26 @@ func (self *Client) writeSeriesCommon(series []*Series, options map[string]strin for name, value := range options { url += fmt.Sprintf("&%s=%s", name, value) } - resp, err := self.httpClient.Post(url, "application/json", bytes.NewBuffer(data)) + var b *bytes.Buffer + if self.compression { + b = bytes.NewBuffer(nil) + w := gzip.NewWriter(b) + if _, err := w.Write(data); err != nil { + return err + } + w.Flush() + w.Close() + } else { + b = bytes.NewBuffer(data) + } + req, err := http.NewRequest("POST", url, b) + if err != nil { + return err + } + if self.compression { + req.Header.Set("Content-Encoding", "gzip") + } + resp, err := self.httpClient.Do(req) return responseToError(resp, err, true) } @@ -341,7 +366,14 @@ func (self *Client) Query(query string, precision ...TimePrecision) ([]*Series, url += "&time_precision=" + string(precision[0]) } url += "&q=" + escapedQuery - resp, err := self.httpClient.Get(url) + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return nil, err + } + if !self.compression { + req.Header.Set("Accept-Encoding", "identity") + } + resp, err := self.httpClient.Do(req) err = responseToError(resp, err, false) if err != nil { return nil, err diff --git a/influxdb_test.go b/influxdb_test.go index 84c1723b01..99134bcc35 100644 --- a/influxdb_test.go +++ b/influxdb_test.go @@ -5,29 +5,51 @@ import ( ) func TestClient(t *testing.T) { + internalTest(t, true) +} + +func TestClientWithoutCompression(t *testing.T) { + internalTest(t, false) +} + +func internalTest(t *testing.T, compression bool) { client, err := NewClient(&ClientConfig{}) if err != nil { t.Error(err) } - if err := client.CreateClusterAdmin("admin", "password"); err != nil { - t.Error(err) - } - admins, err := client.GetClusterAdminList() if err != nil { t.Error(err) } + if len(admins) == 1 { + if err := client.CreateClusterAdmin("admin", "password"); err != nil { + t.Error(err) + } + } + + admins, err = client.GetClusterAdminList() + if err != nil { + t.Error(err) + } + if len(admins) != 2 { t.Error("more than two admins returned") } - if err := client.CreateDatabase("foobar"); err != nil { + dbs, err := client.GetDatabaseList() + if err != nil { t.Error(err) } - dbs, err := client.GetDatabaseList() + if len(dbs) == 0 { + if err := client.CreateDatabase("foobar"); err != nil { + t.Error(err) + } + } + + dbs, err = client.GetDatabaseList() if err != nil { t.Error(err) } @@ -36,19 +58,26 @@ func TestClient(t *testing.T) { t.Errorf("List of databases don't match") } - if err := client.CreateDatabaseUser("foobar", "dbuser", "pass"); err != nil { - t.Error(err) - } - - if err := client.AlterDatabasePrivilege("foobar", "dbuser", true); err != nil { - t.Error(err) - } - users, err := client.GetDatabaseUserList("foobar") if err != nil { t.Error(err) } + if len(users) == 0 { + if err := client.CreateDatabaseUser("foobar", "dbuser", "pass"); err != nil { + t.Error(err) + } + + if err := client.AlterDatabasePrivilege("foobar", "dbuser", true); err != nil { + t.Error(err) + } + } + + users, err = client.GetDatabaseUserList("foobar") + if err != nil { + t.Error(err) + } + if len(users) != 1 { t.Error("more than one user returned") } @@ -59,12 +88,21 @@ func TestClient(t *testing.T) { Database: "foobar", }) + if !compression { + client.DisableCompression() + } + if err != nil { t.Error(err) } + name := "ts9" + if !compression { + name = "ts9_uncompressed" + } + series := &Series{ - Name: "ts9", + Name: name, Columns: []string{"value"}, Points: [][]interface{}{ []interface{}{1.0}, @@ -74,7 +112,7 @@ func TestClient(t *testing.T) { t.Error(err) } - result, err := client.Query("select * from ts9") + result, err := client.Query("select * from " + name) if err != nil { t.Error(err) }