-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(http): use own dns cache (#17574)
- Loading branch information
Showing
5 changed files
with
162 additions
and
8 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { logger } from '../../logger'; | ||
import { clearDnsCache, dnsLookup, printDnsStats } from './dns'; | ||
|
||
describe('util/http/dns', () => { | ||
describe('dnsLookup', () => { | ||
it('works', async () => { | ||
clearDnsCache(); | ||
const ip = await new Promise((resolve) => | ||
dnsLookup('api.github.com', 4, (_e, r, _f) => { | ||
resolve(r); | ||
}) | ||
); | ||
expect(ip).toBeString(); | ||
// uses cache | ||
expect( | ||
await new Promise((resolve) => | ||
dnsLookup('api.github.com', (_e, r, _f) => { | ||
resolve(r); | ||
}) | ||
) | ||
).toBe(ip); | ||
expect( | ||
await new Promise((resolve) => | ||
dnsLookup('api.github.com', {}, (_e, r, _f) => { | ||
resolve(r); | ||
}) | ||
) | ||
).toBe(ip); | ||
}); | ||
|
||
it('throws', async () => { | ||
clearDnsCache(); | ||
const ip = new Promise((resolve, reject) => | ||
dnsLookup('api.github.comcccccccc', 4, (_e, r, _f) => { | ||
if (_e) { | ||
reject(_e); | ||
} else { | ||
resolve(r); | ||
} | ||
}) | ||
); | ||
await expect(ip).rejects.toThrow(); | ||
}); | ||
|
||
it('prints stats', () => { | ||
printDnsStats(); | ||
expect(logger.debug).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,6 +1,107 @@ | ||
import CacheableLookup from 'cacheable-lookup'; | ||
import { LookupAllOptions, LookupOneOptions, lookup as _dnsLookup } from 'dns'; | ||
import type { EntryObject, IPFamily, LookupOptions } from 'cacheable-lookup'; | ||
import QuickLRU from 'quick-lru'; | ||
import { logger } from '../../logger'; | ||
|
||
export const dnsCache = new CacheableLookup({ | ||
cache: new QuickLRU({ maxSize: 1000 }), | ||
}); | ||
const cache = new QuickLRU<string, any>({ maxSize: 1000 }); | ||
|
||
function lookup( | ||
...[host, options, callback]: | ||
| [ | ||
hostname: string, | ||
family: IPFamily, | ||
callback: ( | ||
error: NodeJS.ErrnoException, | ||
address: string, | ||
family: IPFamily | ||
) => void | ||
] | ||
| [ | ||
hostname: string, | ||
callback: ( | ||
error: NodeJS.ErrnoException, | ||
address: string, | ||
family: IPFamily | ||
) => void | ||
] | ||
| [ | ||
hostname: string, | ||
options: LookupOptions & { all: true }, | ||
callback: ( | ||
error: NodeJS.ErrnoException, | ||
result: ReadonlyArray<EntryObject> | ||
) => void | ||
] | ||
| [ | ||
hostname: string, | ||
options: LookupOptions, | ||
callback: ( | ||
error: NodeJS.ErrnoException, | ||
address: string, | ||
family: IPFamily | ||
) => void | ||
] | ||
): void { | ||
let opts: LookupOneOptions | LookupAllOptions; | ||
// TODO: strict null incompatible types (#7154) | ||
let cb: any; | ||
|
||
if (typeof options === 'function') { | ||
opts = {}; | ||
cb = options; | ||
} else if (typeof options === 'number') { | ||
opts = { family: options }; | ||
cb = callback; | ||
} else { | ||
opts = options; | ||
cb = callback; | ||
} | ||
|
||
// istanbul ignore if: not used | ||
if (opts.all) { | ||
const key = `${host}_all`; | ||
if (cache.has(key)) { | ||
logger.trace({ host }, 'dns lookup cache hit'); | ||
cb(null, cache.get(key)); | ||
return; | ||
} | ||
|
||
_dnsLookup(host, opts, (err, res) => { | ||
if (err) { | ||
logger.error({ host, err }, 'dns lookup error'); | ||
cb(err, null, null); | ||
return; | ||
} | ||
logger.trace({ host, opts, res }, 'dns lookup'); | ||
cache.set(key, res); | ||
cb(null, res, null); | ||
}); | ||
} else { | ||
if (cache.has(host)) { | ||
logger.trace({ host }, 'dns lookup cache hit'); | ||
cb(null, ...cache.get(host)); | ||
return; | ||
} | ||
|
||
_dnsLookup(host, opts, (err, ...res) => { | ||
if (err) { | ||
logger.error({ host, err }, 'dns lookup error'); | ||
cb(err); | ||
return; | ||
} | ||
logger.trace({ host, opts, res }, 'dns lookup'); | ||
cache.set(host, res); | ||
cb(null, ...res); | ||
}); | ||
} | ||
} | ||
|
||
export { lookup as dnsLookup }; | ||
|
||
export function printDnsStats(): void { | ||
logger.debug({ hosts: Array.from(cache.keys()) }, 'dns cache'); | ||
} | ||
|
||
export function clearDnsCache(): void { | ||
cache.clear(); | ||
} |
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