This repository has been archived by the owner on May 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
feat(dns): add option to enable app-level DNS caching #727
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3f741e5
feat(dns): add option to enable app-level DNS caching
10xLaCroixDrinker 172b245
docs(configuration): fix typo
10xLaCroixDrinker 452f7ad
Merge branch 'main' of https://github.com/americanexpress/one-app int…
10xLaCroixDrinker 0353054
docs(config): define max TTL
10xLaCroixDrinker 33e9e30
Merge branch 'main' into feature/cacheable-lookup
bishnubista 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import http from 'http'; | ||
import https from 'https'; | ||
import CacheableLookup from 'cacheable-lookup'; | ||
import matchers from 'expect/build/matchers'; | ||
import setupDnsCache, { | ||
uninstallCacheableLookup, | ||
installCacheableLookup, | ||
} from '../../../src/server/utils/setupDnsCache'; | ||
|
||
// Create a fake agent so we can get the Symbols used by cacheable-lookup | ||
const fakeAgent = { createConnection: () => {} }; | ||
const cacheableInstance = new CacheableLookup(); | ||
cacheableInstance.install(fakeAgent); | ||
const cacheableLookupSymbols = Object.getOwnPropertySymbols(fakeAgent); | ||
// eslint-disable-next-line jest/no-standalone-expect -- validate we have the right symbols | ||
expect(cacheableLookupSymbols).toMatchInlineSnapshot(` | ||
Array [ | ||
Symbol(cacheableLookupCreateConnection), | ||
Symbol(cacheableLookupInstance), | ||
] | ||
`); | ||
const cacheableInstanceSymbol = cacheableLookupSymbols[1]; | ||
const getCacheableInstance = (protocol) => protocol.globalAgent[cacheableInstanceSymbol]; | ||
|
||
expect.extend({ | ||
toHaveCacheableLookupInstalled(input) { | ||
return matchers.toEqual.call( | ||
this, | ||
Object.getOwnPropertySymbols(input.globalAgent), | ||
expect.arrayContaining(cacheableLookupSymbols) | ||
); | ||
}, | ||
toHaveMaxTtl(input, expected) { | ||
return matchers.toBe.call( | ||
this, | ||
getCacheableInstance(input).maxTtl, | ||
expected | ||
); | ||
}, | ||
}); | ||
|
||
describe('setupDnsCache', () => { | ||
beforeEach(uninstallCacheableLookup); | ||
|
||
it('should uninstall cacheable lookup when DNS cache is not enabled and cacheable lookup is installed', () => { | ||
installCacheableLookup(); | ||
expect(http).toHaveCacheableLookupInstalled(); | ||
expect(https).toHaveCacheableLookupInstalled(); | ||
setupDnsCache(); | ||
expect(http).not.toHaveCacheableLookupInstalled(); | ||
expect(https).not.toHaveCacheableLookupInstalled(); | ||
}); | ||
|
||
it('should not attempt to uninstall cacheable lookup if it is not installed', () => { | ||
uninstallCacheableLookup(); | ||
expect(http).not.toHaveCacheableLookupInstalled(); | ||
expect(https).not.toHaveCacheableLookupInstalled(); | ||
setupDnsCache(); | ||
expect(http).not.toHaveCacheableLookupInstalled(); | ||
expect(https).not.toHaveCacheableLookupInstalled(); | ||
}); | ||
|
||
it('should install cacheable lookup when DNS cache is enabled and it is not installed', () => { | ||
uninstallCacheableLookup(); | ||
expect(http).not.toHaveCacheableLookupInstalled(); | ||
expect(https).not.toHaveCacheableLookupInstalled(); | ||
setupDnsCache({ enabled: true }); | ||
expect(http).toHaveCacheableLookupInstalled(); | ||
expect(https).toHaveCacheableLookupInstalled(); | ||
}); | ||
|
||
it('should uninstall and reinstall cacheable lookup when the max TTL changes', () => { | ||
installCacheableLookup(100); | ||
const httpInstanceBefore = getCacheableInstance(http); | ||
const httpsInstanceBefore = getCacheableInstance(https); | ||
expect(http).toHaveMaxTtl(100); | ||
expect(https).toHaveMaxTtl(100); | ||
setupDnsCache({ enabled: true, maxTtl: 10 }); | ||
expect(http).toHaveMaxTtl(10); | ||
expect(https).toHaveMaxTtl(10); | ||
expect(getCacheableInstance(http)).not.toBe(httpInstanceBefore); | ||
expect(getCacheableInstance(https)).not.toBe(httpsInstanceBefore); | ||
}); | ||
|
||
it('should not reinstall cacheable lookup when DNS cache settings do not change', () => { | ||
installCacheableLookup(100); | ||
const httpInstanceBefore = getCacheableInstance(http); | ||
const httpsInstanceBefore = getCacheableInstance(https); | ||
expect(http).toHaveMaxTtl(100); | ||
expect(https).toHaveMaxTtl(100); | ||
setupDnsCache({ enabled: true, maxTtl: 100 }); | ||
expect(http).toHaveMaxTtl(100); | ||
expect(https).toHaveMaxTtl(100); | ||
expect(getCacheableInstance(http)).toBe(httpInstanceBefore); | ||
expect(getCacheableInstance(https)).toBe(httpsInstanceBefore); | ||
}); | ||
}); |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,31 @@ | ||
import http from 'http'; | ||
import https from 'https'; | ||
import CacheableLookup from 'cacheable-lookup'; | ||
|
||
let cacheableInstance = null; | ||
|
||
export function uninstallCacheableLookup() { | ||
if (cacheableInstance !== null) { | ||
cacheableInstance.uninstall(http.globalAgent); | ||
cacheableInstance.uninstall(https.globalAgent); | ||
cacheableInstance = null; | ||
} | ||
} | ||
|
||
export function installCacheableLookup(maxTtl) { | ||
uninstallCacheableLookup(); | ||
cacheableInstance = new CacheableLookup({ maxTtl }); | ||
cacheableInstance.install(http.globalAgent); | ||
cacheableInstance.install(https.globalAgent); | ||
} | ||
|
||
export default function setupDnsCache({ enabled, maxTtl } = { enabled: false }) { | ||
if (enabled !== true) { | ||
uninstallCacheableLookup(); | ||
} else if ( | ||
enabled === true | ||
&& (cacheableInstance == null || cacheableInstance.maxTtl !== maxTtl) | ||
) { | ||
installCacheableLookup(maxTtl); | ||
} | ||
} |
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.
for other reviewers - https://github.com/szmarczak/cacheable-lookup/blob/master/source/index.js#L88