add waitgroup to udp server

This commit is contained in:
Jimmy Zelinskie 2015-10-28 16:32:17 -04:00
parent c0ba9b7d38
commit 39b1990798

View file

@ -9,6 +9,7 @@ package udp
import ( import (
"errors" "errors"
"net" "net"
"sync"
"time" "time"
"github.com/golang/glog" "github.com/golang/glog"
@ -22,11 +23,12 @@ import (
type Server struct { type Server struct {
config *config.Config config *config.Config
tracker *tracker.Tracker tracker *tracker.Tracker
sock *net.UDPConn
connIDGen *ConnectionIDGenerator
closing chan struct{} closing chan struct{}
booting chan struct{} booting chan struct{}
sock *net.UDPConn wg sync.WaitGroup
connIDGen *ConnectionIDGenerator
} }
func (s *Server) serve() error { func (s *Server) serve() error {
@ -73,7 +75,9 @@ func (s *Server) serve() error {
return err return err
} }
s.wg.Add(1)
go func() { go func() {
defer s.wg.Done()
start := time.Now() start := time.Now()
response, action, err := s.handlePacket(buffer[:n], addr) response, action, err := s.handlePacket(buffer[:n], addr)
defer pool.GiveSlice(buffer) defer pool.GiveSlice(buffer)
@ -124,6 +128,7 @@ func (s *Server) Serve() {
func (s *Server) Stop() { func (s *Server) Stop() {
close(s.closing) close(s.closing)
s.sock.SetReadDeadline(time.Now()) s.sock.SetReadDeadline(time.Now())
s.wg.Wait()
} }
// NewServer returns a new UDP server for a given configuration and tracker. // NewServer returns a new UDP server for a given configuration and tracker.