Skip to content

Commit

Permalink
fix: redis plugin goroutine leak triggered by auto reload config mech…
Browse files Browse the repository at this point in the history
…anism
  • Loading branch information
zhiyuan-mojie authored and zhiyuanliu committed May 24, 2022
1 parent ab04f3a commit 767ab7b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
22 changes: 21 additions & 1 deletion plugins/inputs/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Redis struct {
Password string
tls.ClientConfig

Log telegraf.Logger
Log telegraf.Logger `toml:"-"`

clients []Client
connected bool
Expand All @@ -40,6 +40,7 @@ type Client interface {
Do(returnType string, args ...interface{}) (interface{}, error)
Info() *redis.StringCmd
BaseTags() map[string]string
Close() error
}

type RedisClient struct {
Expand Down Expand Up @@ -185,6 +186,10 @@ func (r *RedisClient) BaseTags() map[string]string {
return tags
}

func (r *RedisClient) Close() error {
return r.client.Close()
}

var replicationSlaveMetricPrefix = regexp.MustCompile(`^slave\d+`)

var Tracking = map[string]string{
Expand Down Expand Up @@ -686,3 +691,18 @@ func coerceType(value interface{}, typ reflect.Type) reflect.Value {
}
return reflect.ValueOf(value)
}

func (r *Redis) Start(acc telegraf.Accumulator) error {
r.acc = acc
return nil
}

//Stop close the client through ServiceInput interface Start/Stop methods impl.
func (r *Redis) Stop() {
for _, c := range r.clients {
err := c.Close()
if err != nil {
r.Log.Errorf("error closing client: %v", err)
}
}
}
4 changes: 4 additions & 0 deletions plugins/inputs/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (t *testClient) Do(_ string, _ ...interface{}) (interface{}, error) {
return 2, nil
}

func (t *testClient) Close() error {
return nil
}

func TestRedisConnectIntegration(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
Expand Down

0 comments on commit 767ab7b

Please sign in to comment.