Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dns: implement light dns caching #49560

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const cares = internalBinding('cares_wrap');
const { isIP } = require('internal/net');
const { customPromisifyArgs } = require('internal/util');
const errors = require('internal/errors');
const { setTimeout, unref } = require('timers');
const {
bindDefaultResolver,
setDefaultResolver,
Expand Down Expand Up @@ -133,16 +134,48 @@ function onlookupall(err, addresses) {
}
}

function cleanDnsCache() {
for (const key in dnsCache) {
delete dnsCache[key];
}
}

function scheduleCleanup() {
kshitjj marked this conversation as resolved.
Show resolved Hide resolved
if (cleanupTimeout) {
return;
}

cleanupTimeout = setTimeout(() => {
cleanDnsCache();
cleanupTimeout = null;
}, 1000).ref();
}

function addDnsEntry(cacheKey, address, family) {
dnsCache[cacheKey] = {
address,
family,
timestamp: DateNow(),
};

scheduleCleanup();

return dnsCache[cacheKey];
}

// Easy DNS A/AAAA look up
// lookup(hostname, [options,] callback)
const validFamilies = [0, 4, 6];
const dnsCache = { __proto__: null };
const { DateNow } = primordials;
let cleanupTimeout = null;
function lookup(hostname, options, callback) {
let hints = 0;
let family = 0;
let all = false;
let verbatim = getDefaultVerbatim();


// Parse arguments
if (hostname) {
validateString(hostname, 'hostname');
Expand Down Expand Up @@ -212,8 +245,22 @@ function lookup(hostname, options, callback) {
return {};
}

const cacheKey = `${hostname}_${family || 'default'}`;
const cachedResult = dnsCache[cacheKey];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A read-through cache approach would be a bit simpler here. essentially something like

cache.get(cacheKey, callback, () => {
  // If the cache lookup is a miss, this function is called to do the lookup and cache the result of the lookup.
});


if (cachedResult && DateNow() - cachedResult.timestamp < 1000) {
callback(null, cachedResult.address, cachedResult.family);
return {};
}


const req = new GetAddrInfoReqWrap();
req.callback = callback;
req.callback = (error, address, family) => {
if (!error) {
addDnsEntry(cacheKey, address, family);
}
callback(error, address, family);
};
req.family = family;
req.hostname = hostname;
req.oncomplete = all ? onlookupall : onlookup;
Expand Down