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 <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
  • Loading branch information
addaleax committed Aug 1, 2017
1 parent cee8d6d commit 727b291
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 @@ -29,11 +29,16 @@ const internalNet = require('internal/net');
const { customPromisifyArgs } = require('internal/util');
const errors = require('internal/errors');

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 @@ -241,8 +246,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 @@ -263,7 +266,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 @@ -305,7 +308,7 @@ function resolve(hostname, rrtype, 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 @@ -318,7 +321,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 @@ -350,11 +353,11 @@ function setServers(servers) {
throw new errors.Error('ERR_INVALID_IP_ADDRESS', 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 errors.Error('ERR_DNS_SET_SERVERS_FAILED', 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 @@ -35,6 +35,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 727b291

Please sign in to comment.