Skip to content

Commit

Permalink
lib/dns: add method to populate internal caches by Zone
Browse files Browse the repository at this point in the history
The InternalPopulateZone populate the internal caches from Zone's
messages.
  • Loading branch information
shuLhan committed Aug 6, 2023
1 parent a20aaf1 commit 0c967f1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 16 additions & 0 deletions lib/dns/caches.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type Caches struct {
// by is domain name.
external map[string]*answers

// zone contains internal zones, with its origin as the key.
zone map[string]*Zone

// lru contains list of external answers, ordered by access time in
// ascending order (the least recently used, LRU, record will be on
// the top).
Expand Down Expand Up @@ -79,6 +82,7 @@ func (c *Caches) init(pruneDelay, pruneThreshold time.Duration, debug int) {

c.internal = make(map[string]*answers)
c.external = make(map[string]*answers)
c.zone = make(map[string]*Zone)
c.lru = list.New()
c.debug = debug

Expand Down Expand Up @@ -308,6 +312,18 @@ func (c *Caches) InternalPopulateRecords(listRR []*ResourceRecord, from string)
return nil
}

// InternalPopulateZone populate the internal caches from Zone.
func (c *Caches) InternalPopulateZone(zone *Zone) {
if zone == nil {
return
}
if len(zone.Origin) == 0 {
return
}
c.zone[zone.Origin] = zone
c.InternalPopulate(zone.Messages(), zone.Path)
}

// InternalRemoveNames remove internal caches by domain names.
func (c *Caches) InternalRemoveNames(names []string) {
var (
Expand Down
4 changes: 2 additions & 2 deletions lib/dns/zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewZone(file, origin string) (zone *Zone) {

// LoadZoneDir load DNS record from zone formatted files in
// directory "dir".
// On success, it will return map of zone Origin and its content as list
// On success, it will return map of file name and Zone content as list
// of Message.
// On fail, it will return possible partially parse zone file and an error.
func LoadZoneDir(dir string) (zoneFiles map[string]*Zone, err error) {
Expand Down Expand Up @@ -99,7 +99,7 @@ func LoadZoneDir(dir string) (zoneFiles map[string]*Zone, err error) {
return zoneFiles, fmt.Errorf("LoadZoneDir %q: %w", dir, err)
}

zoneFiles[zone.Origin] = zone
zoneFiles[name] = zone
}

err = d.Close()
Expand Down

0 comments on commit 0c967f1

Please sign in to comment.