szmarczak / cacheable-lookup

A cacheable dns.lookup(…) that respects TTL :tada:
MIT License
190 stars 29 forks source link

dns lookup is not cached the way I understand #67

Open menocomp opened 2 years ago

menocomp commented 2 years ago

Hello there,

I could be missing something here.

I am trying to reduce the calls that go to dns.lookup in order to reduce the C++ libuv threads allocation so I am using this library.

How I am testing that, by running this code:

const nodeDnsLookup = dns.lookup;
let dnsLookupCounter = 0;
dns.lookup = (...args) => {
  console.log(`I am calling the native node dns lookup ${JSON.stringify(args)}, number ${dnsLookupCounter}`);
  dnsLookupCounter++;
  return nodeDnsLookup(...args);
};

so, without installing this library I can see a lot of dns lookups.

Now after I installed this library with its global http(s) agent as follows:

import CacheableLookup from 'cacheable-lookup';
const cacheable = new CacheableLookup();

cacheable.install(http.globalAgent);
cacheable.install(https.globalAgent);

I was expecting the number of dns lookups would go down and cache hits to happen, but it did not! I see from the logs that the same hostname with the same family are still calling the native dns.lookup and not served from a cache.

szmarczak commented 1 year ago

Can you confirm if manually using https://nodejs.org/api/dns.html#dnsresolve4hostname-options-callback returns data?