-
Notifications
You must be signed in to change notification settings - Fork 35
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
feat: implement local dns cache #132
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const LRUCache = require('lru-cache') | ||
const dns = require('dns') | ||
|
||
const defaultOptions = exports.defaultOptions = { | ||
family: undefined, | ||
hints: dns.ADDRCONFIG, | ||
all: false, | ||
verbatim: true, | ||
} | ||
|
||
const lookupCache = exports.lookupCache = new LRUCache({ max: 50 }) | ||
|
||
// this is a factory so that each request can have its own opts (i.e. ttl) | ||
// while still sharing the cache across all requests | ||
exports.getLookup = (dnsOptions) => { | ||
return (hostname, options, callback) => { | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = null | ||
} else if (typeof options === 'number') { | ||
options = { family: options } | ||
} | ||
|
||
options = { ...defaultOptions, ...options } | ||
|
||
const key = JSON.stringify({ | ||
hostname, | ||
family: options.family, | ||
hints: options.hints, | ||
all: options.all, | ||
verbatim: options.verbatim, | ||
}) | ||
|
||
if (lookupCache.has(key)) { | ||
const [address, family] = lookupCache.get(key) | ||
process.nextTick(callback, null, address, family) | ||
return | ||
} | ||
|
||
dnsOptions.lookup(hostname, options, (err, address, family) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
|
||
lookupCache.set(key, [address, family], { ttl: dnsOptions.ttl }) | ||
return callback(null, address, family) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
const t = require('tap') | ||
|
||
const dns = require('../lib/dns.js') | ||
const DEFAULT_OPTS = { ttl: 5 * 60 * 1000 } | ||
|
||
t.afterEach(() => dns.lookupCache.clear()) | ||
|
||
t.test('supports no options passed', async (t) => { | ||
let lookupCalled = 0 | ||
const fakeLookup = (hostname, options, callback) => { | ||
lookupCalled += 1 | ||
t.match(options, dns.defaultOptions, 'applied default options') | ||
process.nextTick(callback, null, '127.0.0.1', 4) | ||
} | ||
const lookup = dns.getLookup({ ...DEFAULT_OPTS, lookup: fakeLookup }) | ||
|
||
return new Promise((resolve) => { | ||
lookup('localhost', (err, address, family) => { | ||
t.equal(err, null, 'no error') | ||
t.equal(address, '127.0.0.1', 'got address') | ||
t.equal(family, 4, 'got family') | ||
t.equal(lookupCalled, 1, 'lookup was called once') | ||
resolve() | ||
}) | ||
}) | ||
}) | ||
|
||
t.test('supports family passed directly as options', async (t) => { | ||
let lookupCalled = 0 | ||
const fakeLookup = (hostname, options, callback) => { | ||
lookupCalled += 1 | ||
t.match(options, { ...dns.defaultOptions, family: 4 }, 'kept family setting') | ||
process.nextTick(callback, null, '127.0.0.1', 4) | ||
} | ||
const lookup = dns.getLookup({ ...DEFAULT_OPTS, lookup: fakeLookup }) | ||
|
||
return new Promise((resolve) => { | ||
lookup('localhost', 4, (err, address, family) => { | ||
t.equal(err, null, 'no error') | ||
t.equal(address, '127.0.0.1', 'got address') | ||
t.equal(family, 4, 'got family') | ||
t.equal(lookupCalled, 1, 'lookup was called once') | ||
resolve() | ||
}) | ||
}) | ||
}) | ||
|
||
t.test('reads from cache', async (t) => { | ||
let lookupCalled = 0 | ||
const fakeLookup = (hostname, options, callback) => { | ||
lookupCalled += 1 | ||
t.match(options, dns.defaultOptions, 'applied default options') | ||
process.nextTick(callback, null, '127.0.0.1', 4) | ||
} | ||
const lookup = dns.getLookup({ ...DEFAULT_OPTS, lookup: fakeLookup }) | ||
|
||
return new Promise((resolve) => { | ||
lookup('localhost', (err, address, family) => { | ||
t.equal(err, null, 'no error') | ||
t.equal(address, '127.0.0.1', 'got address') | ||
t.equal(family, 4, 'got family') | ||
t.equal(lookupCalled, 1, 'lookup was called once') | ||
resolve() | ||
}) | ||
}).then(() => new Promise((resolve) => { | ||
lookup('localhost', (err, address, family) => { | ||
t.equal(err, null, 'no error') | ||
t.equal(address, '127.0.0.1', 'got address') | ||
t.equal(family, 4, 'got family') | ||
t.equal(lookupCalled, 1, 'lookup was still only called once') | ||
resolve() | ||
}) | ||
})) | ||
}) | ||
|
||
t.test('does not cache errors', async (t) => { | ||
let lookupCalled = 0 | ||
const fakeLookup = (hostname, options, callback) => { | ||
lookupCalled += 1 | ||
if (lookupCalled === 1) { | ||
process.nextTick(callback, new Error('failed')) | ||
return | ||
} | ||
|
||
t.match(options, dns.defaultOptions, 'applied default options') | ||
process.nextTick(callback, null, '127.0.0.1', 4) | ||
} | ||
const lookup = dns.getLookup({ ...DEFAULT_OPTS, lookup: fakeLookup }) | ||
|
||
return new Promise((resolve) => { | ||
lookup('localhost', (err, address, family) => { | ||
t.match(err, { message: 'failed' }, 'got the error') | ||
t.equal(lookupCalled, 1, 'lookup was called once') | ||
resolve() | ||
}) | ||
}).then(() => new Promise((resolve) => { | ||
lookup('localhost', (err, address, family) => { | ||
t.equal(err, null, 'no error') | ||
t.equal(address, '127.0.0.1', 'got address') | ||
t.equal(family, 4, 'got family') | ||
t.equal(lookupCalled, 2, 'lookup was now called twice') | ||
resolve() | ||
}) | ||
})).then(() => new Promise((resolve) => { | ||
lookup('localhost', (err, address, family) => { | ||
t.equal(err, null, 'no error') | ||
t.equal(address, '127.0.0.1', 'got address') | ||
t.equal(family, 4, 'got family') | ||
t.equal(lookupCalled, 2, 'lookup was still only called twice') | ||
resolve() | ||
}) | ||
})) | ||
}) | ||
|
||
t.test('varies when options change', async (t) => { | ||
let lookupCalled = 0 | ||
const fakeLookup = (hostname, options, callback) => { | ||
lookupCalled += 1 | ||
if (lookupCalled === 1) { | ||
t.match(options, dns.defaultOptions, 'applied default options') | ||
process.nextTick(callback, null, '127.0.0.1', 4) | ||
} else { | ||
t.match(options, { ...dns.defaultOptions, family: 6 }, 'kept family from second lookup') | ||
process.nextTick(callback, null, '::1', 6) | ||
} | ||
} | ||
const lookup = dns.getLookup({ ...DEFAULT_OPTS, lookup: fakeLookup }) | ||
|
||
return new Promise((resolve) => { | ||
lookup('localhost', (err, address, family) => { | ||
t.equal(err, null, 'no error') | ||
t.equal(address, '127.0.0.1', 'got address') | ||
t.equal(family, 4, 'got family') | ||
t.equal(lookupCalled, 1, 'lookup was called once') | ||
resolve() | ||
}) | ||
}).then(() => new Promise((resolve) => { | ||
lookup('localhost', { family: 6 }, (err, address, family) => { | ||
t.equal(err, null, 'no error') | ||
t.equal(address, '::1', 'got address') | ||
t.equal(family, 6, 'got family') | ||
t.equal(lookupCalled, 2, 'lookup was called twice') | ||
resolve() | ||
}) | ||
})) | ||
}) | ||
|
||
t.test('lookup can return all results', async (t) => { | ||
let lookupCalled = 0 | ||
const fakeLookup = (hostname, options, callback) => { | ||
lookupCalled += 1 | ||
t.match(options, { ...dns.defaultOptions, all: true }, 'applied default options') | ||
process.nextTick(callback, null, [{ | ||
address: '127.0.0.1', family: 4, | ||
}, { | ||
address: '::1', family: 6, | ||
}]) | ||
} | ||
const lookup = dns.getLookup({ ...DEFAULT_OPTS, lookup: fakeLookup }) | ||
|
||
return new Promise((resolve) => { | ||
lookup('localhost', { all: true }, (err, addresses) => { | ||
t.equal(err, null, 'no error') | ||
t.match(addresses, [{ | ||
address: '127.0.0.1', family: 4, | ||
}, { | ||
address: '::1', family: 6, | ||
}], 'got all addresses') | ||
t.equal(lookupCalled, 1, 'lookup was called once') | ||
resolve() | ||
}) | ||
}).then(() => new Promise((resolve) => { | ||
lookup('localhost', { all: true }, (err, addresses) => { | ||
t.equal(err, null, 'no error') | ||
t.match(addresses, [{ | ||
address: '127.0.0.1', family: 4, | ||
}, { | ||
address: '::1', family: 6, | ||
}], 'got all addresses') | ||
t.equal(lookupCalled, 1, 'lookup was called once') | ||
resolve() | ||
}) | ||
})) | ||
}) | ||
|
||
t.test('respects ttl option', async (t) => { | ||
let lookupCalled = 0 | ||
const fakeLookup = (hostname, options, callback) => { | ||
lookupCalled += 1 | ||
t.match(options, dns.defaultOptions, 'applied default options') | ||
process.nextTick(callback, null, '127.0.0.1', 4) | ||
} | ||
const lookup = dns.getLookup({ ttl: 10, lookup: fakeLookup }) | ||
|
||
return new Promise((resolve) => { | ||
lookup('localhost', (err, address, family) => { | ||
t.equal(err, null, 'no error') | ||
t.equal(address, '127.0.0.1', 'got address') | ||
t.equal(family, 4, 'got family') | ||
t.equal(lookupCalled, 1, 'lookup was called once') | ||
resolve() | ||
}) | ||
}).then(() => new Promise((resolve) => { | ||
lookup('localhost', (err, address, family) => { | ||
t.equal(err, null, 'no error') | ||
t.equal(address, '127.0.0.1', 'got address') | ||
t.equal(family, 4, 'got family') | ||
t.equal(lookupCalled, 1, 'lookup was still only called once') | ||
// delay before the next request to allow the ttl to invalidate | ||
setTimeout(resolve, 15) | ||
}) | ||
})).then(() => new Promise((resolve) => { | ||
lookup('localhost', (err, address, family) => { | ||
t.equal(err, null, 'no error') | ||
t.equal(address, '127.0.0.1', 'got address') | ||
t.equal(family, 4, 'got family') | ||
t.equal(lookupCalled, 2, 'lookup was now called twice') | ||
resolve() | ||
}) | ||
})) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have any input on what this number should be, but I'm curious on what factors would lead us to change this. Do we have a target package-lock.json that would install without evicting any DNS cache entries?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is per-host, so unless you have more than 50 hosts you're making requests to in a single package-lock this should be just fine. also the lru-cache behaviors mean even if you did unless there's an even distribution you'll end up hitting the cache for the majority of requests anyway.