Commit c2331d7d authored by Miek Gieben's avatar Miek Gieben Committed by Yong Tang

plugin/host: parse file without holding lock (#2270)

* plugin/host: parse file without holding lock

Parse first and then swap the maps *while* holding the lock.
Signed-off-by: default avatarMiek Gieben <miek@miek.nl>

* add back in the parse function, but now purely for testing
Signed-off-by: default avatarMiek Gieben <miek@miek.nl>
parent 921b02e2
...@@ -2,6 +2,7 @@ package hosts ...@@ -2,6 +2,7 @@ package hosts
import ( import (
"context" "context"
"io"
"strings" "strings"
"testing" "testing"
...@@ -11,6 +12,8 @@ import ( ...@@ -11,6 +12,8 @@ import (
"github.com/miekg/dns" "github.com/miekg/dns"
) )
func (h *Hostsfile) parseReader(r io.Reader) { h.hmap = h.parse(r, h.inline) }
func TestLookupA(t *testing.T) { func TestLookupA(t *testing.T) {
h := Hosts{Next: test.ErrorHandler(), Hostsfile: &Hostsfile{Origins: []string{"."}}} h := Hosts{Next: test.ErrorHandler(), Hostsfile: &Hostsfile{Origins: []string{"."}}}
h.parseReader(strings.NewReader(hostsExample)) h.parseReader(strings.NewReader(hostsExample))
......
...@@ -106,13 +106,17 @@ func (h *Hostsfile) readHosts() { ...@@ -106,13 +106,17 @@ func (h *Hostsfile) readHosts() {
return return
} }
newMap := h.parse(file, h.inline)
log.Debugf("Parsed hosts file into %d entries", newMap.Len())
h.Lock() h.Lock()
defer h.Unlock()
h.parseReader(file)
h.hmap = newMap
// Update the data cache. // Update the data cache.
h.mtime = stat.ModTime() h.mtime = stat.ModTime()
h.size = stat.Size() h.size = stat.Size()
h.Unlock()
} }
func (h *Hostsfile) initInline(inline []string) { func (h *Hostsfile) initInline(inline []string) {
...@@ -125,12 +129,6 @@ func (h *Hostsfile) initInline(inline []string) { ...@@ -125,12 +129,6 @@ func (h *Hostsfile) initInline(inline []string) {
*h.hmap = *h.inline *h.hmap = *h.inline
} }
func (h *Hostsfile) parseReader(r io.Reader) {
h.hmap = h.parse(r, h.inline)
log.Debugf("Parsed hosts file into %d entries", h.hmap.Len())
}
// Parse reads the hostsfile and populates the byName and byAddr maps. // Parse reads the hostsfile and populates the byName and byAddr maps.
func (h *Hostsfile) parse(r io.Reader, override *hostsMap) *hostsMap { func (h *Hostsfile) parse(r io.Reader, override *hostsMap) *hostsMap {
hmap := newHostsMap() hmap := newHostsMap()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment