Comparison

net/dns.lua @ 3327:b447682f2a8d

net.dns: Make timeout configurable (default 15s)
author Matthew Wild <mwild1@gmail.com>
date Mon, 05 Jul 2010 12:09:50 +0100
parent 3326:fb95015bc646
child 3328:5ac4fbbfb74d
comparison
equal deleted inserted replaced
3326:fb95015bc646 3327:b447682f2a8d
27 local ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack = 27 local ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack =
28 ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack; 28 ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack;
29 29
30 local get, set = ztact.get, ztact.set; 30 local get, set = ztact.get, ztact.set;
31 31
32 local dns_timeout = 15; 32 local default_timeout = 15;
33 33
34 -------------------------------------------------- module dns 34 -------------------------------------------------- module dns
35 module('dns') 35 module('dns')
36 local dns = _M; 36 local dns = _M;
37 37
116 116
117 117
118 local resolver = {}; 118 local resolver = {};
119 resolver.__index = resolver; 119 resolver.__index = resolver;
120 120
121 resolver.timeout = default_timeout;
121 122
122 local SRV_tostring; 123 local SRV_tostring;
123 124
124 125
125 local rr_metatable = {}; -- - - - - - - - - - - - - - - - - - - rr_metatable 126 local rr_metatable = {}; -- - - - - - - - - - - - - - - - - - - rr_metatable
682 end 683 end
683 684
684 local conn = self:getsocket(o.server) 685 local conn = self:getsocket(o.server)
685 conn:send (o.packet) 686 conn:send (o.packet)
686 687
687 if timer then 688 if timer and self.timeout then
688 local num_servers = #self.server; 689 local num_servers = #self.server;
689 local i = 1; 690 local i = 1;
690 timer.add_task(dns_timeout, function () 691 timer.add_task(self.timeout, function ()
691 if get(self.wanted, qclass, qtype, qname, co) then 692 if get(self.wanted, qclass, qtype, qname, co) then
692 if i < num_servers then 693 if i < num_servers then
693 i = i + 1; 694 i = i + 1;
694 self:servfail(conn); 695 self:servfail(conn);
695 o.server = self.best_server; 696 o.server = self.best_server;
696 conn = self:getsocket(o.server); 697 conn = self:getsocket(o.server);
697 conn:send(o.packet); 698 conn:send(o.packet);
698 return dns_timeout; 699 return self.timeout;
699 else 700 else
700 -- Tried everything, failed 701 -- Tried everything, failed
701 resolver:cancel(qclass, qtype, qname, co, true); 702 resolver:cancel(qclass, qtype, qname, co, true);
702 end 703 end
703 end 704 end
740 if self.best_server > #self.server then 741 if self.best_server > #self.server then
741 -- Exhausted all servers, try first again 742 -- Exhausted all servers, try first again
742 self.best_server = 1; 743 self.best_server = 1;
743 end 744 end
744 end 745 end
746 end
747
748 function resolver:settimeout(seconds)
749 self.timeout = seconds;
745 end 750 end
746 751
747 function resolver:receive(rset) -- - - - - - - - - - - - - - - - - receive 752 function resolver:receive(rset) -- - - - - - - - - - - - - - - - - receive
748 --print('receive'); print(self.socket); 753 --print('receive'); print(self.socket);
749 self.time = socket.gettime(); 754 self.time = socket.gettime();