coredns/plugin/file/reload.go
Miek Gieben eba020e6a1
plugin/file: simplify locking (#3024)
* plugin/file: simplify locking

Simplify the locking, remove the reloadMu and just piggyback on the
other lock for accessing content, which assumes things can be move
underneath.

Copy the Apex and Zone to new vars to make sure the pointer isn't
updated from under us.

The releadMu isn't need at all, the time.Ticker firing while we're
reading means we will just miss that tick and get it on the next go.

Add rrutil subpackage and put some more generic functions in there, that
are now used from file and the tree package. This removes some
duplication.

Rename additionalProcessing that didn't actually do that to
externalLookup, because that's what being done at some point.

Signed-off-by: Miek Gieben <miek@miek.nl>

* Update plugin/file/lookup.go

Co-Authored-By: Michael Grosser <development@stp-ip.net>
2019-07-23 18:32:44 +00:00

61 lines
1.3 KiB
Go

package file
import (
"os"
"time"
)
// Reload reloads a zone when it is changed on disk. If z.NoReload is true, no reloading will be done.
func (z *Zone) Reload() error {
if z.ReloadInterval == 0 {
return nil
}
tick := time.NewTicker(z.ReloadInterval)
go func() {
for {
select {
case <-tick.C:
zFile := z.File()
reader, err := os.Open(zFile)
if err != nil {
log.Errorf("Failed to open zone %q in %q: %v", z.origin, zFile, err)
continue
}
serial := z.SOASerialIfDefined()
zone, err := Parse(reader, z.origin, zFile, serial)
if err != nil {
if _, ok := err.(*serialErr); !ok {
log.Errorf("Parsing zone %q: %v", z.origin, err)
}
continue
}
// copy elements we need
z.Lock()
z.Apex = zone.Apex
z.Tree = zone.Tree
z.Unlock()
log.Infof("Successfully reloaded zone %q in %q with serial %d", z.origin, zFile, z.Apex.SOA.Serial)
z.Notify()
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.Apex.SOA != nil {
return int64(z.Apex.SOA.Serial)
}
return -1
}