From 74dd3be3a561db3e70d9b28735ea8b23a7cb4b90 Mon Sep 17 00:00:00 2001 From: Patrick Hemmer Date: Tue, 2 May 2017 14:06:49 -0400 Subject: [PATCH] fix close on closed socket_writer (#2748) --- plugins/outputs/socket_writer/socket_writer.go | 10 ++++++++++ plugins/outputs/socket_writer/socket_writer_test.go | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/plugins/outputs/socket_writer/socket_writer.go b/plugins/outputs/socket_writer/socket_writer.go index 32baa40c4..503130c62 100644 --- a/plugins/outputs/socket_writer/socket_writer.go +++ b/plugins/outputs/socket_writer/socket_writer.go @@ -124,6 +124,16 @@ func (sw *SocketWriter) Write(metrics []telegraf.Metric) error { return nil } +// Close closes the connection. Noop if already closed. +func (sw *SocketWriter) Close() error { + if sw.Conn == nil { + return nil + } + err := sw.Conn.Close() + sw.Conn = nil + return err +} + func newSocketWriter() *SocketWriter { s, _ := serializers.NewInfluxSerializer() return &SocketWriter{ diff --git a/plugins/outputs/socket_writer/socket_writer_test.go b/plugins/outputs/socket_writer/socket_writer_test.go index 6be2b0905..0c5747d31 100644 --- a/plugins/outputs/socket_writer/socket_writer_test.go +++ b/plugins/outputs/socket_writer/socket_writer_test.go @@ -143,7 +143,7 @@ func TestSocketWriter_Write_err(t *testing.T) { // close the socket to generate an error lconn.Close() - sw.Close() + sw.Conn.Close() err = sw.Write(metrics) require.Error(t, err) assert.Nil(t, sw.Conn)