middleware/file: don't reload zone when SOA isn't changed (#707)

* middleware/file: don't reload zone when SOA isn't changed

Give Parse an extra argument which is the SOA's serial, if > 0 we check
against the just parsed SOA and then just return.

Most notable use is in reload.go which is both used in the file and auto
middleware.

Fixes #415

* PR comments
This commit is contained in:
Miek Gieben
2017-06-08 18:43:11 +01:00
committed by GitHub
parent 1c45e262f5
commit 990460ee7c
20 changed files with 122 additions and 80 deletions

View File

@@ -3,6 +3,7 @@ package file
import (
"errors"
"fmt"
"io"
"log"
@@ -109,14 +110,26 @@ func (f File) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (i
func (f File) Name() string { return "file" }
// Parse parses the zone in filename and returns a new Zone or an error.
func Parse(f io.Reader, origin, fileName string) (*Zone, error) {
// If serial >= 0 it will reload the zone, if the SOA hasn't changed
// it returns an error indicating nothing was read.
func Parse(f io.Reader, origin, fileName string, serial int64) (*Zone, error) {
tokens := dns.ParseZone(f, dns.Fqdn(origin), fileName)
z := NewZone(origin, fileName)
seenSOA := false
for x := range tokens {
if x.Error != nil {
log.Printf("[ERROR] Failed to parse `%s': %v", origin, x.Error)
return nil, x.Error
}
if !seenSOA && serial >= 0 {
if s, ok := x.RR.(*dns.SOA); ok {
if s.Serial == uint32(serial) { // same zone
return nil, fmt.Errorf("no change in serial: %d", serial)
}
}
seenSOA = true
}
if err := z.Insert(x.RR); err != nil {
return nil, err
}