Comparison

net/dns.lua @ 6463:460584257cc9

net.dns: Avoid duplicate cache entries
author Florian Zeitz <florob@babelmonkeys.de>
date Sun, 05 Oct 2014 14:28:40 +0200
parent 6310:d232bb1bbe1e
child 6464:737c81bd898e
child 6508:4a28b8320d7f
comparison
equal deleted inserted replaced
6462:fcff8fa495d4 6463:460584257cc9
132 end 132 end
133 133
134 134
135 local function prune(rrs, time, soft) -- - - - - - - - - - - - - - - prune 135 local function prune(rrs, time, soft) -- - - - - - - - - - - - - - - prune
136 time = time or socket.gettime(); 136 time = time or socket.gettime();
137 for i,rr in pairs(rrs) do 137 for i,rr in ipairs(rrs) do
138 if rr.tod then 138 if rr.tod then
139 -- rr.tod = rr.tod - 50 -- accelerated decripitude 139 -- rr.tod = rr.tod - 50 -- accelerated decripitude
140 rr.ttl = math.floor(rr.tod - time); 140 rr.ttl = math.floor(rr.tod - time);
141 if rr.ttl <= 0 then 141 if rr.ttl <= 0 then
142 rrs[rr[rr.type:lower()]] = nil;
142 table.remove(rrs, i); 143 table.remove(rrs, i);
143 return prune(rrs, time, soft); -- Re-iterate 144 return prune(rrs, time, soft); -- Re-iterate
144 end 145 end
145 elseif soft == 'soft' then -- What is this? I forget! 146 elseif soft == 'soft' then -- What is this? I forget!
146 assert(rr.ttl == 0); 147 assert(rr.ttl == 0);
147 rrs[i] = nil; 148 rrs[rr[rr.type:lower()]] = nil;
149 table.remove(rrs, i);
148 end 150 end
149 end 151 end
150 end 152 end
151 153
152 154
185 187
186 188
187 local rrs_metatable = {}; -- - - - - - - - - - - - - - - - - - rrs_metatable 189 local rrs_metatable = {}; -- - - - - - - - - - - - - - - - - - rrs_metatable
188 function rrs_metatable.__tostring(rrs) 190 function rrs_metatable.__tostring(rrs)
189 local t = {}; 191 local t = {};
190 for i,rr in pairs(rrs) do 192 for i,rr in ipairs(rrs) do
191 append(t, tostring(rr)..'\n'); 193 append(t, tostring(rr)..'\n');
192 end 194 end
193 return table.concat(t); 195 return table.concat(t);
194 end 196 end
195 197
672 end 674 end
673 675
674 self.cache = self.cache or setmetatable({}, cache_metatable); 676 self.cache = self.cache or setmetatable({}, cache_metatable);
675 local rrs = get(self.cache, qclass, type, qname) or 677 local rrs = get(self.cache, qclass, type, qname) or
676 set(self.cache, qclass, type, qname, setmetatable({}, rrs_metatable)); 678 set(self.cache, qclass, type, qname, setmetatable({}, rrs_metatable));
677 append(rrs, rr); 679 if not rrs[rr[qtype:lower()]] then
680 rrs[rr[qtype:lower()]] = true;
681 append(rrs, rr);
682 end
678 683
679 if type == 'MX' then self.unsorted[rrs] = true; end 684 if type == 'MX' then self.unsorted[rrs] = true; end
680 end 685 end
681 686
682 687