From f7a43179904510e8aae8fb8a296d6e7b1011e5a1 Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Thu, 10 Sep 2015 11:21:40 -0600 Subject: [PATCH] Fix multiple redis server bug, do not cache the TCP connections Fixes #178 --- CHANGELOG.md | 1 + plugins/redis/redis.go | 63 +++++++++++++++++++----------------------- 2 files changed, 29 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cea99e8d..e0888ec75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ will still be backwards compatible if only `url` is specified. ### Bugfixes - [#170](https://github.com/influxdb/telegraf/issues/170): Systemd support - [#175](https://github.com/influxdb/telegraf/issues/175): Set write precision before gathering metrics +- [#178](https://github.com/influxdb/telegraf/issues/178): redis plugin, multiple server thread hang bug ## v0.1.8 [2015-09-04] diff --git a/plugins/redis/redis.go b/plugins/redis/redis.go index 4a95772aa..30077bc4f 100644 --- a/plugins/redis/redis.go +++ b/plugins/redis/redis.go @@ -15,9 +15,6 @@ import ( type Redis struct { Servers []string - - c net.Conn - buf []byte } var sampleConfig = ` @@ -112,41 +109,37 @@ func (r *Redis) Gather(acc plugins.Accumulator) error { const defaultPort = "6379" func (r *Redis) gatherServer(addr *url.URL, acc plugins.Accumulator) error { - if r.c == nil { - - _, _, err := net.SplitHostPort(addr.Host) - if err != nil { - addr.Host = addr.Host + ":" + defaultPort - } - - c, err := net.Dial("tcp", addr.Host) - if err != nil { - return fmt.Errorf("Unable to connect to redis server '%s': %s", addr.Host, err) - } - - if addr.User != nil { - pwd, set := addr.User.Password() - if set && pwd != "" { - c.Write([]byte(fmt.Sprintf("AUTH %s\r\n", pwd))) - - rdr := bufio.NewReader(c) - - line, err := rdr.ReadString('\n') - if err != nil { - return err - } - if line[0] != '+' { - return fmt.Errorf("%s", strings.TrimSpace(line)[1:]) - } - } - } - - r.c = c + _, _, err := net.SplitHostPort(addr.Host) + if err != nil { + addr.Host = addr.Host + ":" + defaultPort } - r.c.Write([]byte("info\r\n")) + c, err := net.Dial("tcp", addr.Host) + if err != nil { + return fmt.Errorf("Unable to connect to redis server '%s': %s", addr.Host, err) + } + defer c.Close() - rdr := bufio.NewReader(r.c) + if addr.User != nil { + pwd, set := addr.User.Password() + if set && pwd != "" { + c.Write([]byte(fmt.Sprintf("AUTH %s\r\n", pwd))) + + rdr := bufio.NewReader(c) + + line, err := rdr.ReadString('\n') + if err != nil { + return err + } + if line[0] != '+' { + return fmt.Errorf("%s", strings.TrimSpace(line)[1:]) + } + } + } + + c.Write([]byte("info\r\n")) + + rdr := bufio.NewReader(c) line, err := rdr.ReadString('\n') if err != nil {