Merge pull request #2 from mjgarton/cleanupOnExit
Clean up probe goroutine when shutting down
This commit is contained in:
commit
a30f6551c5
16
server.go
16
server.go
@ -60,6 +60,7 @@ type Server struct {
|
|||||||
shutdown bool
|
shutdown bool
|
||||||
shutdownCh chan struct{}
|
shutdownCh chan struct{}
|
||||||
shutdownLock sync.Mutex
|
shutdownLock sync.Mutex
|
||||||
|
wg sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewServer is used to create a new mDNS server from a config
|
// NewServer is used to create a new mDNS server from a config
|
||||||
@ -119,6 +120,7 @@ func NewServer(config *Config) (*Server, error) {
|
|||||||
go s.recv(s.ipv6List)
|
go s.recv(s.ipv6List)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.wg.Add(1)
|
||||||
go s.probe()
|
go s.probe()
|
||||||
|
|
||||||
return s, nil
|
return s, nil
|
||||||
@ -144,6 +146,7 @@ func (s *Server) Shutdown() error {
|
|||||||
s.ipv6List.Close()
|
s.ipv6List.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.wg.Wait()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -310,6 +313,8 @@ func (s *Server) handleQuestion(q dns.Question) (multicastRecs, unicastRecs []dn
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) probe() {
|
func (s *Server) probe() {
|
||||||
|
defer s.wg.Done()
|
||||||
|
|
||||||
sd, ok := s.config.Zone.(*MDNSService)
|
sd, ok := s.config.Zone.(*MDNSService)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
@ -371,12 +376,19 @@ func (s *Server) probe() {
|
|||||||
// provided that the interval between unsolicited responses increases by
|
// provided that the interval between unsolicited responses increases by
|
||||||
// at least a factor of two with every response sent.
|
// at least a factor of two with every response sent.
|
||||||
timeout := 1 * time.Second
|
timeout := 1 * time.Second
|
||||||
|
timer := time.NewTimer(timeout)
|
||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < 3; i++ {
|
||||||
if err := s.multicastResponse(resp); err != nil {
|
if err := s.multicastResponse(resp); err != nil {
|
||||||
log.Println("[ERR] mdns: failed to send announcement:", err.Error())
|
log.Println("[ERR] mdns: failed to send announcement:", err.Error())
|
||||||
}
|
}
|
||||||
time.Sleep(timeout)
|
select {
|
||||||
timeout *= 2
|
case <-timer.C:
|
||||||
|
timeout *= 2
|
||||||
|
timer.Reset(timeout)
|
||||||
|
case <-s.shutdownCh:
|
||||||
|
timer.Stop()
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user