From 7ad99d0d3677459ac7196eb2020f1091d5ea1d69 Mon Sep 17 00:00:00 2001 From: Pat Moroney Date: Tue, 24 Oct 2017 10:27:07 -0600 Subject: [PATCH] Fix locking for hosts plugin --- plugin/hosts/hostsfile.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/plugin/hosts/hostsfile.go b/plugin/hosts/hostsfile.go index 0862711fe..d6a5969ab 100644 --- a/plugin/hosts/hostsfile.go +++ b/plugin/hosts/hostsfile.go @@ -36,7 +36,7 @@ func absDomainName(b string) string { // Hostsfile contains known host entries. type Hostsfile struct { - sync.Mutex + sync.RWMutex // list of zones we are authoritive for Origins []string @@ -139,6 +139,8 @@ func (h *Hostsfile) Parse(file io.Reader) { is[addr.String()] = append(is[addr.String()], name) } } + h.Lock() + defer h.Unlock() h.byNameV4 = hsv4 h.byNameV6 = hsv6 h.byAddr = is @@ -159,8 +161,8 @@ func ipVersion(s string) int { // LookupStaticHostV4 looks up the IPv4 addresses for the given host from the hosts file. func (h *Hostsfile) LookupStaticHostV4(host string) []net.IP { - h.Lock() - defer h.Unlock() + h.RLock() + defer h.RUnlock() h.ReadHosts() if len(h.byNameV4) != 0 { if ips, ok := h.byNameV4[absDomainName(host)]; ok { @@ -174,8 +176,8 @@ func (h *Hostsfile) LookupStaticHostV4(host string) []net.IP { // LookupStaticHostV6 looks up the IPv6 addresses for the given host from the hosts file. func (h *Hostsfile) LookupStaticHostV6(host string) []net.IP { - h.Lock() - defer h.Unlock() + h.RLock() + defer h.RUnlock() h.ReadHosts() if len(h.byNameV6) != 0 { if ips, ok := h.byNameV6[absDomainName(host)]; ok { @@ -189,8 +191,8 @@ func (h *Hostsfile) LookupStaticHostV6(host string) []net.IP { // LookupStaticAddr looks up the hosts for the given address from the hosts file. func (h *Hostsfile) LookupStaticAddr(addr string) []string { - h.Lock() - defer h.Unlock() + h.RLock() + defer h.RUnlock() h.ReadHosts() addr = parseLiteralIP(addr).String() if addr == "" {