Comparison

net/adns.lua @ 10614:431511e190bc

net.adns: Add :lookup_promise() method
author Matthew Wild <mwild1@gmail.com>
date Fri, 24 Jan 2020 13:50:02 +0000
parent 10114:2f7628804db6
child 10966:97de279ca01a
comparison
equal deleted inserted replaced
10613:74d66b1be989 10614:431511e190bc
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 local server = require "net.server"; 9 local server = require "net.server";
10 local new_resolver = require "net.dns".resolver; 10 local new_resolver = require "net.dns".resolver;
11 local promise = require "util.promise";
11 12
12 local log = require "util.logger".init("adns"); 13 local log = require "util.logger".init("adns");
13 14
14 local coroutine, pcall = coroutine, pcall; 15 local coroutine, pcall = coroutine, pcall;
15 local setmetatable = setmetatable; 16 local setmetatable = setmetatable;
89 log("error", "Error in DNS response handler: %s", err); 90 log("error", "Error in DNS response handler: %s", err);
90 end 91 end
91 end)(resolver:peek(qname, qtype, qclass)); 92 end)(resolver:peek(qname, qtype, qclass));
92 end 93 end
93 94
95 function async_resolver_methods:lookup_promise(qname, qtype, qclass)
96 return promise.new(function (resolve, reject)
97 local function handler(answer)
98 if not answer then
99 return reject();
100 end
101 resolve(answer);
102 end
103 self:lookup(handler, qname, qtype, qclass);
104 end);
105 end
106
94 function query_methods:cancel(call_handler, reason) -- luacheck: ignore 212/reason 107 function query_methods:cancel(call_handler, reason) -- luacheck: ignore 212/reason
95 log("warn", "Cancelling DNS lookup for %s", self[4]); 108 log("warn", "Cancelling DNS lookup for %s", self[4]);
96 self[1].cancel(self[2], self[3], self[4], self[5], call_handler); 109 self[1].cancel(self[2], self[3], self[4], self[5], call_handler);
97 end 110 end
98 111