From f4c2977cb5952d3490962fd255754d49239a3729 Mon Sep 17 00:00:00 2001 From: HUANG Wei Date: Thu, 17 Mar 2016 16:10:36 +0800 Subject: [PATCH] Close the UDP connection in Stop() of statsd input plugin. If not, when doing reload, we may listen to the same port, we'll get error about listen to already used address. --- plugins/inputs/statsd/statsd.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/plugins/inputs/statsd/statsd.go b/plugins/inputs/statsd/statsd.go index a16e78b5c..f237ac783 100644 --- a/plugins/inputs/statsd/statsd.go +++ b/plugins/inputs/statsd/statsd.go @@ -65,6 +65,8 @@ type Statsd struct { // bucket -> influx templates Templates []string + + listener *net.UDPConn } func NewStatsd() *Statsd { @@ -246,13 +248,14 @@ func (s *Statsd) Start(_ telegraf.Accumulator) error { // udpListen starts listening for udp packets on the configured port. func (s *Statsd) udpListen() error { + var err error address, _ := net.ResolveUDPAddr("udp", s.ServiceAddress) - listener, err := net.ListenUDP("udp", address) + s.listener, err = net.ListenUDP("udp", address) if err != nil { log.Fatalf("ERROR: ListenUDP - %s", err) } - defer listener.Close() - log.Println("Statsd listener listening on: ", listener.LocalAddr().String()) + defer s.listener.Close() + log.Println("Statsd listener listening on: ", s.listener.LocalAddr().String()) for { select { @@ -260,9 +263,10 @@ func (s *Statsd) udpListen() error { return nil default: buf := make([]byte, s.UDPPacketSize) - n, _, err := listener.ReadFromUDP(buf) + n, _, err := s.listener.ReadFromUDP(buf) if err != nil { - log.Printf("ERROR: %s\n", err.Error()) + log.Printf("ERROR READ: %s\n", err.Error()) + continue } select { @@ -557,6 +561,7 @@ func (s *Statsd) Stop() { s.Lock() defer s.Unlock() log.Println("Stopping the statsd service") + s.listener.Close() close(s.done) close(s.in) }