mirror of
https://github.com/coredns/coredns.git
synced 2025-11-08 13:06:24 -05:00
read lock around ReadHosts()
This commit is contained in:
@@ -68,11 +68,17 @@ type Hostsfile struct {
|
|||||||
func (h *Hostsfile) ReadHosts() {
|
func (h *Hostsfile) ReadHosts() {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
|
||||||
|
// Not deferring h.RUnlock() because we may need to remove the read lock and aquire a write lock
|
||||||
|
h.RLock()
|
||||||
if now.Before(h.expire) && len(h.byAddr) > 0 {
|
if now.Before(h.expire) && len(h.byAddr) > 0 {
|
||||||
|
h.RUnlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
stat, err := os.Stat(h.path)
|
stat, err := os.Stat(h.path)
|
||||||
if err == nil && h.mtime.Equal(stat.ModTime()) && h.size == stat.Size() {
|
if err == nil && h.mtime.Equal(stat.ModTime()) && h.size == stat.Size() {
|
||||||
|
h.RUnlock()
|
||||||
|
h.Lock()
|
||||||
|
defer h.Unlock()
|
||||||
h.expire = now.Add(cacheMaxAge)
|
h.expire = now.Add(cacheMaxAge)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -83,10 +89,14 @@ func (h *Hostsfile) ReadHosts() {
|
|||||||
if len(h.byAddr) == 0 && len(h.inline) > 0 {
|
if len(h.byAddr) == 0 && len(h.inline) > 0 {
|
||||||
h.Parse(nil)
|
h.Parse(nil)
|
||||||
}
|
}
|
||||||
|
h.RUnlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
|
h.RUnlock()
|
||||||
|
h.Lock()
|
||||||
|
defer h.Unlock()
|
||||||
h.Parse(file)
|
h.Parse(file)
|
||||||
|
|
||||||
// Update the data cache.
|
// Update the data cache.
|
||||||
@@ -139,8 +149,6 @@ func (h *Hostsfile) Parse(file io.Reader) {
|
|||||||
is[addr.String()] = append(is[addr.String()], name)
|
is[addr.String()] = append(is[addr.String()], name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
h.Lock()
|
|
||||||
defer h.Unlock()
|
|
||||||
h.byNameV4 = hsv4
|
h.byNameV4 = hsv4
|
||||||
h.byNameV6 = hsv6
|
h.byNameV6 = hsv6
|
||||||
h.byAddr = is
|
h.byAddr = is
|
||||||
|
|||||||
Reference in New Issue
Block a user