Skip to content

Commit

Permalink
Fix multiple redis server bug, do not cache the TCP connections
Browse files Browse the repository at this point in the history
Fixes #178
  • Loading branch information
sparrc committed Sep 10, 2015
1 parent 81f4aa9 commit 0f3d8e0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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]

Expand Down
52 changes: 23 additions & 29 deletions plugins/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import (

type Redis struct {
Servers []string

c net.Conn
buf []byte
}

var sampleConfig = `
Expand Down Expand Up @@ -112,41 +109,38 @@ 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
}
_, _, 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)
}
fmt.Println("Connecting to Redis server: ", addr.Host)
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()

if addr.User != nil {
pwd, set := addr.User.Password()
if set && pwd != "" {
c.Write([]byte(fmt.Sprintf("AUTH %s\r\n", pwd)))
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)
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:])
}
line, err := rdr.ReadString('\n')
if err != nil {
return err
}
if line[0] != '+' {
return fmt.Errorf("%s", strings.TrimSpace(line)[1:])
}
}

r.c = c
}

r.c.Write([]byte("info\r\n"))
c.Write([]byte("info\r\n"))

rdr := bufio.NewReader(r.c)
rdr := bufio.NewReader(c)

line, err := rdr.ReadString('\n')
if err != nil {
Expand Down

0 comments on commit 0f3d8e0

Please sign in to comment.