diff options
author | 2017-10-31 01:40:47 -0600 | |
---|---|---|
committer | 2017-10-31 07:40:47 +0000 | |
commit | 1d4ac4adbbfa7f2f4d3549e3e77056f2503d8f8e (patch) | |
tree | c6bbaa2606ee41d090467b1519cc0d095a0163f3 /plugin/hosts/setup.go | |
parent | 87c9f00c83212734d7ab0172ac34aee5bd271b07 (diff) | |
download | coredns-1d4ac4adbbfa7f2f4d3549e3e77056f2503d8f8e.tar.gz coredns-1d4ac4adbbfa7f2f4d3549e3e77056f2503d8f8e.tar.zst coredns-1d4ac4adbbfa7f2f4d3549e3e77056f2503d8f8e.zip |
add goroutine to check hosts file for updates (#1180)
* add goroutine to check hosts file for updates
* rename parseFile to parseReader, remove extra error check
Diffstat (limited to 'plugin/hosts/setup.go')
-rw-r--r-- | plugin/hosts/setup.go | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/plugin/hosts/setup.go b/plugin/hosts/setup.go index 48beb0dcb..1b281b9a3 100644 --- a/plugin/hosts/setup.go +++ b/plugin/hosts/setup.go @@ -5,6 +5,7 @@ import ( "os" "path" "strings" + "time" "github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/plugin" @@ -25,6 +26,30 @@ func setup(c *caddy.Controller) error { return plugin.Error("hosts", err) } + parseChan := make(chan bool) + + c.OnStartup(func() error { + h.readHosts() + + go func() { + ticker := time.NewTicker(5 * time.Second) + for { + select { + case <-parseChan: + return + case <-ticker.C: + h.readHosts() + } + } + }() + return nil + }) + + c.OnShutdown(func() error { + close(parseChan) + return nil + }) + dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler { h.Next = next return h @@ -35,12 +60,15 @@ func setup(c *caddy.Controller) error { func hostsParse(c *caddy.Controller) (Hosts, error) { var h = Hosts{ - Hostsfile: &Hostsfile{path: "/etc/hosts"}, + Hostsfile: &Hostsfile{ + path: "/etc/hosts", + hmap: newHostsMap(), + }, } - defer h.ReadHosts() config := dnsserver.GetConfig(c) + inline := []string{} for c.Next() { args := c.RemainingArgs() if len(args) >= 1 { @@ -86,12 +114,15 @@ func hostsParse(c *caddy.Controller) (Hosts, error) { default: if !h.Fallthrough { line := strings.Join(append([]string{c.Val()}, c.RemainingArgs()...), " ") - h.inline = append(h.inline, line) + inline = append(inline, line) continue } return h, c.Errf("unknown property '%s'", c.Val()) } } } + + h.initInline(inline) + return h, nil } |