From f893df7e642b1ee3dd29adddd028c123f8a0f73b Mon Sep 17 00:00:00 2001 From: jvoisin Date: Tue, 22 Dec 2020 13:39:50 +0100 Subject: [PATCH 1/2] Use a RWMutex instead of a Mutex for authosts --- internal/util/autohosts.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/internal/util/autohosts.go b/internal/util/autohosts.go index ec2f3579..a9b4f752 100644 --- a/internal/util/autohosts.go +++ b/internal/util/autohosts.go @@ -21,7 +21,7 @@ type onChangedT func() // AutoHosts - automatic DNS records type AutoHosts struct { // lock protects table and tableReverse. - lock sync.Mutex + lock sync.RWMutex // table is the host-to-IPs map. table map[string][]net.IP // tableReverse is the IP-to-hosts map. @@ -119,14 +119,14 @@ func (a *AutoHosts) Process(host string, qtype uint16) []net.IP { } var ipsCopy []net.IP - a.lock.Lock() + a.lock.RLock() if ips, ok := a.table[host]; ok { ipsCopy = make([]net.IP, len(ips)) copy(ipsCopy, ips) } - a.lock.Unlock() + a.lock.RUnlock() log.Debug("AutoHosts: answer: %s -> %v", host, ipsCopy) return ipsCopy @@ -145,8 +145,8 @@ func (a *AutoHosts) ProcessReverse(addr string, qtype uint16) (hosts []string) { ipStr := ipReal.String() - a.lock.Lock() - defer a.lock.Unlock() + a.lock.RLock() + defer a.lock.RUnlock() hosts = a.tableReverse[ipStr] @@ -161,8 +161,8 @@ func (a *AutoHosts) ProcessReverse(addr string, qtype uint16) (hosts []string) { // List returns an IP-to-hostnames table. It is safe for concurrent use. func (a *AutoHosts) List() (ipToHosts map[string][]string) { - a.lock.Lock() - defer a.lock.Unlock() + a.lock.RLock() + defer a.lock.RUnlock() ipToHosts = make(map[string][]string, len(a.tableReverse)) for k, v := range a.tableReverse { From 52575d0247d5411d26083d4c186d39d8098b916e Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Wed, 3 Mar 2021 16:14:23 +0300 Subject: [PATCH 2/2] util: imp autohosts --- internal/util/autohosts.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/internal/util/autohosts.go b/internal/util/autohosts.go index ad7825d5..e4cea58c 100644 --- a/internal/util/autohosts.go +++ b/internal/util/autohosts.go @@ -120,14 +120,13 @@ func (a *AutoHosts) Process(host string, qtype uint16) []net.IP { var ipsCopy []net.IP a.lock.RLock() + defer a.lock.RUnlock() if ips, ok := a.table[host]; ok { ipsCopy = make([]net.IP, len(ips)) copy(ipsCopy, ips) } - a.lock.RUnlock() - log.Debug("AutoHosts: answer: %s -> %v", host, ipsCopy) return ipsCopy } @@ -339,10 +338,13 @@ func (a *AutoHosts) updateHosts() { } } - a.lock.Lock() - a.table = table - a.tableReverse = tableRev - a.lock.Unlock() + func() { + a.lock.Lock() + defer a.lock.Unlock() + + a.table = table + a.tableReverse = tableRev + }() a.notify() }