fix: redis plugin goroutine leak triggered by auto reload config mechanism (#11143)

This commit is contained in:
zhiyuan-mojie 2022-05-26 02:58:12 +08:00 committed by GitHub
parent f4eefeadcc
commit ae98e7f1f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -37,7 +37,7 @@ type Redis struct {
Password string
tls.ClientConfig
Log telegraf.Logger
Log telegraf.Logger `toml:"-"`
clients []Client
connected bool
@ -47,6 +47,7 @@ type Client interface {
Do(returnType string, args ...interface{}) (interface{}, error)
Info() *redis.StringCmd
BaseTags() map[string]string
Close() error
}
type RedisClient struct {
@ -192,6 +193,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{
@ -697,3 +702,17 @@ func coerceType(value interface{}, typ reflect.Type) reflect.Value {
}
return reflect.ValueOf(value)
}
func (r *Redis) Start(telegraf.Accumulator) error {
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)
}
}
}

View File

@ -29,6 +29,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")