-
Notifications
You must be signed in to change notification settings - Fork 0
/
nscache.go
93 lines (77 loc) 路 1.66 KB
/
nscache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package recursor
import (
"bytes"
"encoding/gob"
"errors"
"time"
"github.com/ferama/pigdns/pkg/cache"
"github.com/ferama/pigdns/pkg/metrics"
"github.com/ferama/pigdns/pkg/racer"
)
type nsCacheItem struct {
Zone string
List []racer.NS
}
type nsCache struct {
cache cache.Cache
name string
}
func newNSCache(datadir string, name string, size int) *nsCache {
c := &nsCache{
cache: cache.NewFileCache(datadir, name, size),
name: name,
}
return c
}
func (c *nsCache) Set(as *authServers) error {
if as == nil {
return errors.New("no servers to set")
}
as.RLock()
defer as.RUnlock()
if len(as.List) == 0 {
return errors.New("no servers to set")
}
key := as.Zone
ttl := as.TTL
var packed bytes.Buffer
enc := gob.NewEncoder(&packed)
// this extra conversion step is needed
// becaouse the glob ancoder cannot encode the
// mutex
item := nsCacheItem{
Zone: as.Zone,
List: as.List,
}
err := enc.Encode(item)
if err != nil {
return err
}
i := &cache.Item{
Data: packed.Bytes(),
}
i.SetTTL(time.Duration(ttl) * time.Second)
metrics.Instance().ExchangeCacheMiss()
// log.Printf("[%s set] zone=%s, TTL: %d", c.name, key, ttl)
return c.cache.Set(key, i)
}
func (c *nsCache) Get(zone string) (*authServers, error) {
item, err := c.cache.Get(zone)
if err != nil {
return nil, err
}
// var packed bytes.Buffer
var as authServers
var nsItem nsCacheItem
reader := bytes.NewReader(item.Data)
dec := gob.NewDecoder(reader)
err = dec.Decode(&nsItem)
if err != nil {
return nil, err
}
as.List = nsItem.List
as.Zone = nsItem.Zone
// log.Printf("[%s get] zone=%s", c.name, as.Zone)
metrics.Instance().ExchangeCacheHit()
return &as, nil
}