Add lock to entries to prevent concurrent modifications.

This commit is contained in:
Joachim Bauch 2024-01-30 16:29:22 +01:00
parent 2c4cdedcae
commit c4fce20678
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02

View file

@ -55,11 +55,15 @@ type dnsMonitorEntry struct {
hostname string
hostIP net.IP
mu sync.Mutex
ips []net.IP
entries map[*DnsMonitorEntry]bool
}
func (e *dnsMonitorEntry) setIPs(ips []net.IP, fromIP bool) {
e.mu.Lock()
defer e.mu.Unlock()
empty := len(e.ips) == 0
if empty {
// Simple case: initial lookup.
@ -113,6 +117,21 @@ func (e *dnsMonitorEntry) setIPs(ips []net.IP, fromIP bool) {
}
}
func (e *dnsMonitorEntry) addEntry(entry *DnsMonitorEntry) {
e.mu.Lock()
defer e.mu.Unlock()
e.entries[entry] = true
}
func (e *dnsMonitorEntry) removeEntry(entry *DnsMonitorEntry) bool {
e.mu.Lock()
defer e.mu.Unlock()
delete(e.entries, entry)
return len(e.entries) == 0
}
func (e *dnsMonitorEntry) runCallbacks(all []net.IP, add []net.IP, keep []net.IP, remove []net.IP) {
for entry := range e.entries {
entry.callback(entry, all, add, keep, remove)
@ -197,7 +216,7 @@ func (m *DnsMonitor) Add(target string, callback DnsMonitorCallback) (*DnsMonito
m.hostnames[hostname] = entry
}
e.entry = entry
entry.entries[e] = true
entry.addEntry(e)
m.cond.Signal()
return e, nil
}
@ -216,8 +235,7 @@ func (m *DnsMonitor) Remove(entry *DnsMonitorEntry) {
}
entry.entry = nil
delete(e.entries, entry)
if len(e.entries) == 0 {
if e.removeEntry(entry) {
delete(m.hostnames, e.hostname)
}
}