Files
coredns/plugin/file/reload.go
jxj 72160e6e20 plugin/file: track zone mtime for reload_by_mtime (#8556)
* plugin/file: track zone mtime for reload_by_mtime

Signed-off-by: git-jxj <65210887+git-jxj@users.noreply.github.com>

* plugin/file: capture initial mtime before parsing

Signed-off-by: git-jxj <65210887+git-jxj@users.noreply.github.com>

* plugin/file: keep unloaded zones retryable after open error

Signed-off-by: git-jxj <65210887+git-jxj@users.noreply.github.com>

---------

Signed-off-by: git-jxj <65210887+git-jxj@users.noreply.github.com>
2026-09-28 00:22:39 -07:00

89 lines
1.9 KiB
Go

package file
import (
"os"
"path/filepath"
"time"
"github.com/coredns/coredns/plugin/transfer"
)
// Reload reloads a zone when it is changed on disk. If z.ReloadInterval is zero, no reloading will be done.
func (z *Zone) Reload(t *transfer.Transfer) error {
if z.ReloadInterval == 0 {
return nil
}
tick := time.NewTicker(z.ReloadInterval)
go func() {
for {
select {
case <-tick.C:
zFile := z.File()
serial := z.SOASerialIfDefined()
var fileMtime time.Time
if z.ReloadByMtime {
fi, err := os.Stat(zFile)
if err != nil {
log.Errorf("Failed to stat zone %q in %q: %v", z.origin, zFile, err)
continue
}
z.RLock()
lastMtime := z.file_mtime
z.RUnlock()
if !fi.ModTime().After(lastMtime) {
continue
}
fileMtime = fi.ModTime()
serial = 0 // force reload of the zone
}
reader, err := os.Open(filepath.Clean(zFile))
if err != nil {
log.Errorf("Failed to open zone %q in %q: %v", z.origin, zFile, err)
continue
}
zone, err := Parse(reader, z.origin, zFile, serial)
reader.Close()
if err != nil {
if _, ok := err.(*serialErr); !ok {
log.Errorf("Parsing zone %q: %v", z.origin, err)
}
continue
}
z.setData(zone.Apex, zone.Tree)
if z.ReloadByMtime {
z.Lock()
z.file_mtime = fileMtime
z.Unlock()
}
log.Infof("Successfully reloaded zone %q in %q with %d SOA serial", z.origin, zFile, zone.SOA.Serial)
if t != nil {
if err := t.Notify(z.origin); err != nil {
log.Warningf("Failed sending notifies: %s", err)
}
}
case <-z.reloadShutdown:
tick.Stop()
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.RLock()
defer z.RUnlock()
if z.SOA != nil {
return int64(z.SOA.Serial)
}
return -1
}