net.dns: Avoid duplicate cache entries
authorFlorian Zeitz <florob@babelmonkeys.de>
Sun, 05 Oct 2014 14:28:40 +0200
changeset 6466 460584257cc9
parent 6465 fcff8fa495d4
child 6467 737c81bd898e
child 6469 6e67c73f730c
net.dns: Avoid duplicate cache entries
net/dns.lua
--- a/net/dns.lua	Mon Sep 29 11:18:04 2014 +0200
+++ b/net/dns.lua	Sun Oct 05 14:28:40 2014 +0200
@@ -134,17 +134,19 @@
 
 local function prune(rrs, time, soft)    -- - - - - - - - - - - - - - -  prune
 	time = time or socket.gettime();
-	for i,rr in pairs(rrs) do
+	for i,rr in ipairs(rrs) do
 		if rr.tod then
 			-- rr.tod = rr.tod - 50    -- accelerated decripitude
 			rr.ttl = math.floor(rr.tod - time);
 			if rr.ttl <= 0 then
+				rrs[rr[rr.type:lower()]] = nil;
 				table.remove(rrs, i);
 				return prune(rrs, time, soft); -- Re-iterate
 			end
 		elseif soft == 'soft' then    -- What is this?  I forget!
 			assert(rr.ttl == 0);
-			rrs[i] = nil;
+			rrs[rr[rr.type:lower()]] = nil;
+			table.remove(rrs, i);
 		end
 	end
 end
@@ -187,7 +189,7 @@
 local rrs_metatable = {};    -- - - - - - - - - - - - - - - - - -  rrs_metatable
 function rrs_metatable.__tostring(rrs)
 	local t = {};
-	for i,rr in pairs(rrs) do
+	for i,rr in ipairs(rrs) do
 		append(t, tostring(rr)..'\n');
 	end
 	return table.concat(t);
@@ -674,7 +676,10 @@
 	self.cache = self.cache or setmetatable({}, cache_metatable);
 	local rrs = get(self.cache, qclass, type, qname) or
 		set(self.cache, qclass, type, qname, setmetatable({}, rrs_metatable));
-	append(rrs, rr);
+	if not rrs[rr[qtype:lower()]] then
+		rrs[rr[qtype:lower()]] = true;
+		append(rrs, rr);
+	end
 
 	if type == 'MX' then self.unsorted[rrs] = true; end
 end