diff options
author | 2017-10-24 10:27:07 -0600 | |
---|---|---|
committer | 2017-10-24 10:27:07 -0600 | |
commit | 7ad99d0d3677459ac7196eb2020f1091d5ea1d69 (patch) | |
tree | e0e23f3782077d1f67cbc4c0d7c2547e8f0d00b1 /plugin/hosts | |
parent | cc490a8912dc5d1afe5f2c2ee53d7fc044496225 (diff) | |
download | coredns-7ad99d0d3677459ac7196eb2020f1091d5ea1d69.tar.gz coredns-7ad99d0d3677459ac7196eb2020f1091d5ea1d69.tar.zst coredns-7ad99d0d3677459ac7196eb2020f1091d5ea1d69.zip |
Fix locking for hosts plugin
Diffstat (limited to 'plugin/hosts')
-rw-r--r-- | plugin/hosts/hostsfile.go | 16 |
1 files 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 == "" { |