aboutsummaryrefslogtreecommitdiff
path: root/plugin/file/reload.go
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2017-09-14 09:36:06 +0100
committerGravatar GitHub <noreply@github.com> 2017-09-14 09:36:06 +0100
commitd8714e64e400ef873c2adc4d929a07d7890727b9 (patch)
treec9fa4c157e6af12eb1517654f8d23ca5d5619513 /plugin/file/reload.go
parentb984aa45595dc95253b91191afe7d3ee29e71b48 (diff)
downloadcoredns-d8714e64e400ef873c2adc4d929a07d7890727b9.tar.gz
coredns-d8714e64e400ef873c2adc4d929a07d7890727b9.tar.zst
coredns-d8714e64e400ef873c2adc4d929a07d7890727b9.zip
Remove the word middleware (#1067)
* Rename middleware to plugin first pass; mostly used 'sed', few spots where I manually changed text. This still builds a coredns binary. * fmt error * Rename AddMiddleware to AddPlugin * Readd AddMiddleware to remain backwards compat
Diffstat (limited to 'plugin/file/reload.go')
-rw-r--r--plugin/file/reload.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/plugin/file/reload.go b/plugin/file/reload.go
new file mode 100644
index 000000000..18e949a94
--- /dev/null
+++ b/plugin/file/reload.go
@@ -0,0 +1,72 @@
+package file
+
+import (
+ "log"
+ "os"
+ "path"
+
+ "github.com/fsnotify/fsnotify"
+)
+
+// Reload reloads a zone when it is changed on disk. If z.NoRoload is true, no reloading will be done.
+func (z *Zone) Reload() error {
+ if z.NoReload {
+ return nil
+ }
+ watcher, err := fsnotify.NewWatcher()
+ if err != nil {
+ return err
+ }
+ err = watcher.Add(path.Dir(z.file))
+ if err != nil {
+ return err
+ }
+
+ go func() {
+ // TODO(miek): needs to be killed on reload.
+ for {
+ select {
+ case event := <-watcher.Events:
+ if path.Clean(event.Name) == z.file {
+
+ reader, err := os.Open(z.file)
+ if err != nil {
+ log.Printf("[ERROR] Failed to open `%s' for `%s': %v", z.file, z.origin, err)
+ continue
+ }
+
+ serial := z.SOASerialIfDefined()
+ zone, err := Parse(reader, z.origin, z.file, serial)
+ if err != nil {
+ log.Printf("[WARNING] Parsing zone `%s': %v", z.origin, err)
+ continue
+ }
+
+ // copy elements we need
+ z.reloadMu.Lock()
+ z.Apex = zone.Apex
+ z.Tree = zone.Tree
+ z.reloadMu.Unlock()
+
+ log.Printf("[INFO] Successfully reloaded zone `%s'", z.origin)
+ z.Notify()
+ }
+ case <-z.ReloadShutdown:
+ watcher.Close()
+ return
+ }
+ }
+ }()
+ return nil
+}
+
+// SOASerialIfDefined returns the SOA's serial if the zone has a SOA record in the Apex, or
+// -1 otherwise.
+func (z *Zone) SOASerialIfDefined() int64 {
+ z.reloadMu.Lock()
+ defer z.reloadMu.Unlock()
+ if z.Apex.SOA != nil {
+ return int64(z.Apex.SOA.Serial)
+ }
+ return -1
+}