Comparison

net/adns.lua @ 973:b091a1a7273b

net.adns: Catch errors in DNS response callbacks
author Matthew Wild <mwild1@gmail.com>
date Fri, 10 Apr 2009 10:30:50 +0100
parent 886:96de7f0a41cc
child 980:b52d442161f2
comparison
equal deleted inserted replaced
971:dd736391bd41 973:b091a1a7273b
1 local server = require "net.server"; 1 local server = require "net.server";
2 local dns = require "net.dns"; 2 local dns = require "net.dns";
3 3
4 local log = require "util.logger".init("adns"); 4 local log = require "util.logger".init("adns");
5 5
6 local coroutine, tostring = coroutine, tostring; 6 local coroutine, tostring, pcall = coroutine, tostring, pcall;
7 7
8 module "adns" 8 module "adns"
9 9
10 function lookup(handler, qname, qtype, qclass) 10 function lookup(handler, qname, qtype, qclass)
11 return dns.peek(qname, qtype, qclass) or 11 return dns.peek(qname, qtype, qclass) or
12 coroutine.wrap(function () 12 coroutine.wrap(function ()
13 log("debug", "Records for "..qname.." not in cache, sending query (%s)...", tostring(coroutine.running())); 13 log("debug", "Records for "..qname.." not in cache, sending query (%s)...", tostring(coroutine.running()));
14 dns.query(qname, qtype, qclass); 14 dns.query(qname, qtype, qclass);
15 coroutine.yield(nil); -- Wait for reply 15 coroutine.yield(nil); -- Wait for reply
16 log("debug", "Reply for "..qname.." (%s)", tostring(coroutine.running())); 16 log("debug", "Reply for "..qname.." (%s)", tostring(coroutine.running()));
17 handler(dns.peek(qname, qtype, qclass)); 17 local ok, err = pcall(handler, dns.peek(qname, qtype, qclass));
18 if not ok then
19 log("debug", "Error in DNS response handler: %s", tostring(err));
20 end
18 end)(); 21 end)();
19 end 22 end
20 23
21 function new_async_socket(sock) 24 function new_async_socket(sock)
22 local newconn = {}; 25 local newconn = {};