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 6 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
30 changes: 29 additions & 1 deletion lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,16 @@ function onlookupall(err, addresses) {
// Easy DNS A/AAAA look up
// lookup(hostname, [options,] callback)
const validFamilies = [0, 4, 6];
const dnsCache = { __proto__: null };
function lookup(hostname, options, callback) {
let hints = 0;
let family = 0;
let all = false;
let verbatim = getDefaultVerbatim();

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

@Linkgoron Linkgoron Sep 11, 2023

Choose a reason for hiding this comment

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

IMO this (and the code added before the if(!hostname) should move to before const req= new GetAddrInfoReqWrap(). The hostname variable should be used only after it passes validation.

Copy link
Member

Choose a reason for hiding this comment

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

The cache key should probably include if options.all is true or not

Copy link
Author

Choose a reason for hiding this comment

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

I thought if there's a dns key for that domain that means it passed the validation earlier so there was no need for validating the hostname again. I could be wrong though

Also having the all in dns key makes a lot of sense I will add that.

const cachedResult = dnsCache[cacheKey];

// Parse arguments
if (hostname) {
validateString(hostname, 'hostname');
Expand Down Expand Up @@ -191,6 +195,11 @@ function lookup(hostname, options, callback) {
}
}

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

if (!hostname) {
emitInvalidHostnameWarning(hostname);
if (all) {
Expand All @@ -213,7 +222,16 @@ function lookup(hostname, options, callback) {
}

const req = new GetAddrInfoReqWrap();
req.callback = callback;
req.callback = (error, address, family) => {
if (!error) {
dnsCache[cacheKey] = {
address,
family,
timestamp: Date.now(),
};
}
callback(error, address, family);
};
req.family = family;
req.hostname = hostname;
req.oncomplete = all ? onlookupall : onlookup;
Expand All @@ -237,6 +255,16 @@ function lookup(hostname, options, callback) {
return req;
}

// DNS cache checks every 1 second
setInterval(() => {
kshitjj marked this conversation as resolved.
Show resolved Hide resolved
const currentTime = Date.now();
for (const key in dnsCache) {
if (currentTime - dnsCache[key].timestamp >= 1000) {
delete dnsCache[key];
}
}
}, 1000);

ObjectDefineProperty(lookup, customPromisifyArgs,
{ __proto__: null, value: ['address', 'family'], enumerable: false });

Expand Down