Browse Source

Move last modified time out of zone

Only load zones when they parse correctly, but record the modify
time whenever we try to parse them. This fixes the issue of
constantly trying to reparse broken zones.

Signed-off-by: Alex Bligh <[email protected]>
Alex Bligh 10 years ago
parent
commit
f8c1b6d9b7
4 changed files with 12 additions and 5 deletions
  1. 0 2
      zone.go
  2. 4 1
      zone_test.go
  3. 6 2
      zones.go
  4. 2 0
      zones_test.go

+ 0 - 2
zone.go

@@ -2,7 +2,6 @@ package main
 
 
 import (
 import (
 	"strings"
 	"strings"
-	"time"
 
 
 	"github.com/abh/geodns/Godeps/_workspace/src/github.com/miekg/dns"
 	"github.com/abh/geodns/Godeps/_workspace/src/github.com/miekg/dns"
 	"github.com/abh/geodns/Godeps/_workspace/src/github.com/rcrowley/go-metrics"
 	"github.com/abh/geodns/Godeps/_workspace/src/github.com/rcrowley/go-metrics"
@@ -58,7 +57,6 @@ type Zone struct {
 	LabelCount int
 	LabelCount int
 	Options    ZoneOptions
 	Options    ZoneOptions
 	Logging    *ZoneLogging
 	Logging    *ZoneLogging
-	LastRead   time.Time
 	Metrics    ZoneMetrics
 	Metrics    ZoneMetrics
 }
 }
 
 

+ 4 - 1
zone_test.go

@@ -6,7 +6,10 @@ import (
 )
 )
 
 
 func (s *ConfigSuite) TestExampleComZone(c *C) {
 func (s *ConfigSuite) TestExampleComZone(c *C) {
-	ex := s.zones["test.example.com"]
+	ex, ok := s.zones["test.example.com"]
+
+	c.Check(ok, Equals, true)
+	c.Check(ex, NotNil)
 
 
 	// test.example.com was loaded
 	// test.example.com was loaded
 	c.Assert(ex.Labels, NotNil)
 	c.Assert(ex.Labels, NotNil)

+ 6 - 2
zones.go

@@ -21,6 +21,8 @@ import (
 // Zones maps domain names to zone data
 // Zones maps domain names to zone data
 type Zones map[string]*Zone
 type Zones map[string]*Zone
 
 
+var lastRead = map[string]time.Time{}
+
 func zonesReader(dirName string, zones Zones) {
 func zonesReader(dirName string, zones Zones) {
 	for {
 	for {
 		zonesReadDir(dirName, zones)
 		zonesReadDir(dirName, zones)
@@ -56,13 +58,15 @@ func zonesReadDir(dirName string, zones Zones) error {
 
 
 		seenZones[zoneName] = true
 		seenZones[zoneName] = true
 
 
-		if zone, ok := zones[zoneName]; !ok || file.ModTime().After(zone.LastRead) {
+		if _, ok := lastRead[zoneName]; !ok || file.ModTime().After(lastRead[zoneName]) {
 			if ok {
 			if ok {
 				logPrintf("Reloading %s\n", fileName)
 				logPrintf("Reloading %s\n", fileName)
 			} else {
 			} else {
 				logPrintf("Reading new file %s\n", fileName)
 				logPrintf("Reading new file %s\n", fileName)
 			}
 			}
 
 
+			lastRead[zoneName] = file.ModTime()
+
 			//log.Println("FILE:", i, file, zoneName)
 			//log.Println("FILE:", i, file, zoneName)
 			config, err := readZoneFile(zoneName, path.Join(dirName, fileName))
 			config, err := readZoneFile(zoneName, path.Join(dirName, fileName))
 			if config == nil || err != nil {
 			if config == nil || err != nil {
@@ -70,7 +74,6 @@ func zonesReadDir(dirName string, zones Zones) error {
 				log.Println(parseErr.Error())
 				log.Println(parseErr.Error())
 				continue
 				continue
 			}
 			}
-			config.LastRead = file.ModTime()
 
 
 			addHandler(zones, zoneName, config)
 			addHandler(zones, zoneName, config)
 		}
 		}
@@ -84,6 +87,7 @@ func zonesReadDir(dirName string, zones Zones) error {
 			continue
 			continue
 		}
 		}
 		log.Println("Removing zone", zone.Origin)
 		log.Println("Removing zone", zone.Origin)
+		delete(lastRead, zoneName)
 		zone.Close()
 		zone.Close()
 		dns.HandleRemove(zoneName)
 		dns.HandleRemove(zoneName)
 		delete(zones, zoneName)
 		delete(zones, zoneName)

+ 2 - 0
zones_test.go

@@ -5,6 +5,7 @@ import (
 	"io/ioutil"
 	"io/ioutil"
 	"os"
 	"os"
 	"testing"
 	"testing"
+	"time"
 
 
 	"github.com/abh/geodns/Godeps/_workspace/src/github.com/miekg/dns"
 	"github.com/abh/geodns/Godeps/_workspace/src/github.com/miekg/dns"
 	. "github.com/abh/geodns/Godeps/_workspace/src/gopkg.in/check.v1"
 	. "github.com/abh/geodns/Godeps/_workspace/src/gopkg.in/check.v1"
@@ -21,6 +22,7 @@ var _ = Suite(&ConfigSuite{})
 
 
 func (s *ConfigSuite) SetUpSuite(c *C) {
 func (s *ConfigSuite) SetUpSuite(c *C) {
 	s.zones = make(Zones)
 	s.zones = make(Zones)
+	lastRead = map[string]time.Time{}
 	zonesReadDir("dns", s.zones)
 	zonesReadDir("dns", s.zones)
 }
 }