Skip to content

Commit

Permalink
src,dns: refactor cares_wrap to avoid global state
Browse files Browse the repository at this point in the history
PR-URL: #14518
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
  • Loading branch information
addaleax committed Aug 1, 2017
1 parent 7b96944 commit 106a23b
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 221 deletions.
25 changes: 14 additions & 11 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ const uv = process.binding('uv');
const internalNet = require('internal/net');
const { customPromisifyArgs } = require('internal/util');

const GetAddrInfoReqWrap = cares.GetAddrInfoReqWrap;
const GetNameInfoReqWrap = cares.GetNameInfoReqWrap;
const QueryReqWrap = cares.QueryReqWrap;
const {
GetAddrInfoReqWrap,
GetNameInfoReqWrap,
QueryReqWrap,
ChannelWrap,
isIP
} = cares;

const defaultChannel = new ChannelWrap();

const isIP = cares.isIP;
const isLegalPort = internalNet.isLegalPort;


Expand Down Expand Up @@ -240,8 +245,6 @@ function onresolve(err, result, ttls) {


function resolver(bindingName) {
var binding = cares[bindingName];

return function query(name, /* options, */ callback) {
var options;
if (arguments.length > 2) {
Expand All @@ -261,7 +264,7 @@ function resolver(bindingName) {
req.hostname = name;
req.oncomplete = onresolve;
req.ttl = !!(options && options.ttl);
var err = binding(req, name);
var err = defaultChannel[bindingName](req, name);
if (err) throw errnoException(err, bindingName);
return req;
};
Expand Down Expand Up @@ -303,7 +306,7 @@ function resolve(hostname, type_, callback_) {


function getServers() {
const ret = cares.getServers();
const ret = defaultChannel.getServers();
return ret.map((val) => {
if (!val[1] || val[1] === IANA_DNS_PORT) return val[0];

Expand All @@ -316,7 +319,7 @@ function getServers() {
function setServers(servers) {
// cache the original servers because in the event of an error setting the
// servers cares won't have any servers available for resolution
const orig = cares.getServers();
const orig = defaultChannel.getServers();
const newSet = [];
const IPv6RE = /\[(.*)\]/;
const addrSplitRE = /(^.+?)(?::(\d+))?$/;
Expand Down Expand Up @@ -348,11 +351,11 @@ function setServers(servers) {
throw new Error(`IP address is not properly formatted: ${serv}`);
});

const errorNumber = cares.setServers(newSet);
const errorNumber = defaultChannel.setServers(newSet);

if (errorNumber !== 0) {
// reset the servers to the old servers, because ares probably unset them
cares.setServers(orig.join(','));
defaultChannel.setServers(orig.join(','));

var err = cares.strerror(errorNumber);
throw new Error(`c-ares failed to set servers: "${err}" [${servers}]`);
Expand Down
1 change: 1 addition & 0 deletions src/async-wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace node {

#define NODE_ASYNC_NON_CRYPTO_PROVIDER_TYPES(V) \
V(NONE) \
V(DNSCHANNEL) \
V(FSEVENTWRAP) \
V(FSREQWRAP) \
V(GETADDRINFOREQWRAP) \
Expand Down
Loading

0 comments on commit 106a23b

Please sign in to comment.