aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2019-04-12 20:00:39 +0700
committerShulhan <ms@kilabit.info>2019-04-12 21:19:19 +0700
commit21dc9bcd808a426f9a37e759d50ec63d7ac907f7 (patch)
tree1e463a07797fd5f9d2a230447a6f7ec6e3580ffe
parentadcb787dbd3d9176ad86fdd3e7b51f85fcc9e16a (diff)
downloadpakakeh.go-21dc9bcd808a426f9a37e759d50ec63d7ac907f7.tar.xz
dns: update accessed time when answer found on caches
Also, remove process of moving the answer to the back of LRU when doing upsert on caches, since this is already handled by get().
-rw-r--r--lib/dns/caches.go9
-rw-r--r--lib/dns/caches_test.go2
2 files changed, 4 insertions, 7 deletions
diff --git a/lib/dns/caches.go b/lib/dns/caches.go
index 8aa5440d..14af58d6 100644
--- a/lib/dns/caches.go
+++ b/lib/dns/caches.go
@@ -66,7 +66,7 @@ func newCaches(pruneDelay, pruneThreshold time.Duration) (ca *caches) {
// it will return list of answer and nil answer.
//
// If answer exist on cache, their accessed time will be updated to current
-// time.
+// time and moved to back of LRU to prevent being pruned later.
//
func (c *caches) get(qname string, qtype, qclass uint16) (ans *answers, an *answer) {
c.Lock()
@@ -78,9 +78,10 @@ func (c *caches) get(qname string, qtype, qclass uint16) (ans *answers, an *answ
an, _ = ans.get(qtype, qclass)
if an != nil {
// Move the answer to the back of LRU if its not
- // local.
+ // local and update its accessed time.
if an.receivedAt > 0 {
c.lru.MoveToBack(an.el)
+ an.accessedAt = time.Now().Unix()
}
}
}
@@ -165,10 +166,6 @@ func (c *caches) upsert(nu *answer) (inserted bool) {
// not local and its inserted to list.
nu.el = c.lru.PushBack(nu)
}
- } else {
- if nu.receivedAt > 0 {
- c.lru.MoveToBack(an.el)
- }
}
}
diff --git a/lib/dns/caches_test.go b/lib/dns/caches_test.go
index c228f5fd..d25d235e 100644
--- a/lib/dns/caches_test.go
+++ b/lib/dns/caches_test.go
@@ -259,7 +259,7 @@ func TestCachesUpsert(t *testing.T) {
desc: "With update on answer",
nu: an1Update,
expLen: 2,
- expList: []*answer{an2, an1},
+ expList: []*answer{an1, an2},
}, {
desc: "With update on answer (2)",
nu: an2Update,