fix(inputs.mongodb): Fix connection leak triggered by config reload (#12330)

Co-authored-by: amliu <amliu@qq.com>
This commit is contained in:
zhiyuan-mojie 2022-12-09 23:44:33 +08:00 committed by GitHub
parent 4717a03509
commit 375e089d12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 1 deletions

View File

@ -144,7 +144,16 @@ func (m *MongoDB) Start(telegraf.Accumulator) error {
return nil
}
func (m *MongoDB) Stop() {}
// Stop disconnect mongo connections when stop or reload
func (m *MongoDB) Stop() {
for _, server := range m.clients {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
if err := server.client.Disconnect(ctx); err != nil {
m.Log.Errorf("disconnecting from %q failed: %w", server, err)
}
cancel()
}
}
// Reads stats from all configured servers accumulates stats.
// Returns one of the errors encountered while gather stats (if any).