Closing all idle connections in docker input plugin (#9243)

This prevents error "too many open files" in most cases
This commit is contained in:
Vyacheslav Stepanov 2021-06-15 07:23:39 +03:00 committed by GitHub
parent 1d4b8d62f5
commit 905b22cac9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 3 deletions

View File

@ -23,6 +23,7 @@ type Client interface {
ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error)
TaskList(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error) TaskList(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error)
NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error) NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error)
Close() error
} }
func NewEnvClient() (Client, error) { func NewEnvClient() (Client, error) {
@ -76,3 +77,6 @@ func (c *SocketClient) TaskList(ctx context.Context, options types.TaskListOptio
func (c *SocketClient) NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error) { func (c *SocketClient) NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error) {
return c.client.NodeList(ctx, options) return c.client.NodeList(ctx, options)
} }
func (c *SocketClient) Close() error {
return c.client.Close()
}

View File

@ -213,6 +213,9 @@ func (d *Docker) Gather(acc telegraf.Accumulator) error {
d.client = c d.client = c
} }
// Close any idle connections in the end of gathering
defer d.client.Close()
// Create label filters if not already created // Create label filters if not already created
if !d.filtersCreated { if !d.filtersCreated {
err := d.createLabelFilters() err := d.createLabelFilters()

View File

@ -26,6 +26,7 @@ type MockClient struct {
ServiceListF func(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) ServiceListF func(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error)
TaskListF func(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error) TaskListF func(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error)
NodeListF func(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error) NodeListF func(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error)
CloseF func() error
} }
func (c *MockClient) Info(ctx context.Context) (types.Info, error) { func (c *MockClient) Info(ctx context.Context) (types.Info, error) {
@ -75,6 +76,10 @@ func (c *MockClient) NodeList(
return c.NodeListF(ctx, options) return c.NodeListF(ctx, options)
} }
func (c *MockClient) Close() error {
return c.CloseF()
}
var baseClient = MockClient{ var baseClient = MockClient{
InfoF: func(context.Context) (types.Info, error) { InfoF: func(context.Context) (types.Info, error) {
return info, nil return info, nil
@ -97,6 +102,9 @@ var baseClient = MockClient{
NodeListF: func(context.Context, types.NodeListOptions) ([]swarm.Node, error) { NodeListF: func(context.Context, types.NodeListOptions) ([]swarm.Node, error) {
return NodeList, nil return NodeList, nil
}, },
CloseF: func() error {
return nil
},
} }
func newClient(_ string, _ *tls.Config) (Client, error) { func newClient(_ string, _ *tls.Config) (Client, error) {
@ -279,6 +287,9 @@ func TestDocker_WindowsMemoryContainerStats(t *testing.T) {
NodeListF: func(context.Context, types.NodeListOptions) ([]swarm.Node, error) { NodeListF: func(context.Context, types.NodeListOptions) ([]swarm.Node, error) {
return NodeList, nil return NodeList, nil
}, },
CloseF: func() error {
return nil
},
}, nil }, nil
}, },
} }