"Stop" should wait until stopped in DNS monitor.

This commit is contained in:
Joachim Bauch 2024-02-22 14:05:23 +01:00
parent 2e8b0dfe25
commit 7e613f831b
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02

View file

@ -145,6 +145,7 @@ type DnsMonitor struct {
stopCtx context.Context
stopFunc func()
stopped chan struct{}
mu sync.RWMutex
cond *sync.Cond
@ -167,6 +168,7 @@ func NewDnsMonitor(interval time.Duration) (*DnsMonitor, error) {
stopCtx: stopCtx,
stopFunc: stopFunc,
stopped: make(chan struct{}),
hostnames: make(map[string]*dnsMonitorEntry),
}
@ -183,6 +185,7 @@ func (m *DnsMonitor) Start() error {
func (m *DnsMonitor) Stop() {
m.stopFunc()
m.cond.Signal()
<-m.stopped
}
func (m *DnsMonitor) Add(target string, callback DnsMonitorCallback) (*DnsMonitorEntry, error) {
@ -296,6 +299,7 @@ func (m *DnsMonitor) waitForEntries() (waited bool) {
func (m *DnsMonitor) run() {
ticker := time.NewTicker(m.interval)
defer ticker.Stop()
defer close(m.stopped)
for {
if m.waitForEntries() {