Skip to content

Commit

Permalink
Close UDP listener in udp_listener plugin
Browse files Browse the repository at this point in the history
also adding waitgroups to udp_listener and statsd plugins to verify that
all goroutines have been cleaned up before Stop() exits.

closes #869
  • Loading branch information
sparrc committed Mar 17, 2016
1 parent 8e7284d commit b5a4316
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
10 changes: 7 additions & 3 deletions plugins/inputs/statsd/statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Statsd struct {
UDPPacketSize int `toml:"udp_packet_size"`

sync.Mutex
wg sync.WaitGroup

// Channel for all incoming statsd packets
in chan []byte
Expand Down Expand Up @@ -238,6 +239,7 @@ func (s *Statsd) Start(_ telegraf.Accumulator) error {
s.sets = make(map[string]cachedset)
s.timings = make(map[string]cachedtimings)

s.wg.Add(2)
// Start the UDP listener
go s.udpListen()
// Start the line parser
Expand All @@ -248,13 +250,13 @@ func (s *Statsd) Start(_ telegraf.Accumulator) error {

// udpListen starts listening for udp packets on the configured port.
func (s *Statsd) udpListen() error {
defer s.wg.Done()
var err error
address, _ := net.ResolveUDPAddr("udp", s.ServiceAddress)
s.listener, err = net.ListenUDP("udp", address)
if err != nil {
log.Fatalf("ERROR: ListenUDP - %s", err)
}
defer s.listener.Close()
log.Println("Statsd listener listening on: ", s.listener.LocalAddr().String())

for {
Expand All @@ -264,7 +266,7 @@ func (s *Statsd) udpListen() error {
default:
buf := make([]byte, s.UDPPacketSize)
n, _, err := s.listener.ReadFromUDP(buf)
if err != nil {
if err != nil && !strings.Contains(err.Error(), "closed network") {
log.Printf("ERROR READ: %s\n", err.Error())
continue
}
Expand All @@ -282,6 +284,7 @@ func (s *Statsd) udpListen() error {
// packet into statsd strings and then calls parseStatsdLine, which parses a
// single statsd metric into a struct.
func (s *Statsd) parser() error {
defer s.wg.Done()
for {
select {
case <-s.done:
Expand Down Expand Up @@ -561,8 +564,9 @@ func (s *Statsd) Stop() {
s.Lock()
defer s.Unlock()
log.Println("Stopping the statsd service")
s.listener.Close()
close(s.done)
s.listener.Close()
s.wg.Wait()
close(s.in)
}

Expand Down
21 changes: 16 additions & 5 deletions plugins/inputs/udp_listener/udp_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package udp_listener
import (
"log"
"net"
"strings"
"sync"

"github.com/influxdata/telegraf"
Expand All @@ -14,7 +15,9 @@ type UdpListener struct {
ServiceAddress string
UDPPacketSize int `toml:"udp_packet_size"`
AllowedPendingMessages int

sync.Mutex
wg sync.WaitGroup

in chan []byte
done chan struct{}
Expand All @@ -23,6 +26,8 @@ type UdpListener struct {

// Keep the accumulator in this struct
acc telegraf.Accumulator

listener *net.UDPConn
}

const UDP_PACKET_SIZE int = 1500
Expand Down Expand Up @@ -76,6 +81,7 @@ func (u *UdpListener) Start(acc telegraf.Accumulator) error {
u.in = make(chan []byte, u.AllowedPendingMessages)
u.done = make(chan struct{})

u.wg.Add(2)
go u.udpListen()
go u.udpParser()

Expand All @@ -87,28 +93,32 @@ func (u *UdpListener) Stop() {
u.Lock()
defer u.Unlock()
close(u.done)
u.listener.Close()
u.wg.Wait()
close(u.in)
log.Println("Stopped UDP listener service on ", u.ServiceAddress)
}

func (u *UdpListener) udpListen() error {
defer u.wg.Done()
var err error
address, _ := net.ResolveUDPAddr("udp", u.ServiceAddress)
listener, err := net.ListenUDP("udp", address)
u.listener, err = net.ListenUDP("udp", address)
if err != nil {
log.Fatalf("ERROR: ListenUDP - %s", err)
}
defer listener.Close()
log.Println("UDP server listening on: ", listener.LocalAddr().String())
log.Println("UDP server listening on: ", u.listener.LocalAddr().String())

for {
select {
case <-u.done:
return nil
default:
buf := make([]byte, u.UDPPacketSize)
n, _, err := listener.ReadFromUDP(buf)
if err != nil {
n, _, err := u.listener.ReadFromUDP(buf)
if err != nil && !strings.Contains(err.Error(), "closed network") {
log.Printf("ERROR: %s\n", err.Error())
continue
}

select {
Expand All @@ -121,6 +131,7 @@ func (u *UdpListener) udpListen() error {
}

func (u *UdpListener) udpParser() error {
defer u.wg.Done()
for {
select {
case <-u.done:
Expand Down
4 changes: 4 additions & 0 deletions plugins/inputs/udp_listener/udp_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestRunParser(t *testing.T) {
defer close(listener.done)

listener.parser, _ = parsers.NewInfluxParser()
listener.wg.Add(1)
go listener.udpParser()

in <- testmsg
Expand All @@ -58,6 +59,7 @@ func TestRunParserInvalidMsg(t *testing.T) {
defer close(listener.done)

listener.parser, _ = parsers.NewInfluxParser()
listener.wg.Add(1)
go listener.udpParser()

in <- testmsg
Expand All @@ -78,6 +80,7 @@ func TestRunParserGraphiteMsg(t *testing.T) {
defer close(listener.done)

listener.parser, _ = parsers.NewGraphiteParser("_", []string{}, nil)
listener.wg.Add(1)
go listener.udpParser()

in <- testmsg
Expand All @@ -98,6 +101,7 @@ func TestRunParserJSONMsg(t *testing.T) {
defer close(listener.done)

listener.parser, _ = parsers.NewJSONParser("udp_json_test", []string{}, nil)
listener.wg.Add(1)
go listener.udpParser()

in <- testmsg
Expand Down

0 comments on commit b5a4316

Please sign in to comment.