Skip to content

Commit e9c1180

Browse files
authored
feat: dns cache logger (#388)
##### Checklist - [ x] `npm test` passes - [ x] tests and/or benchmarks are included ##### Affected core subsystem(s) Feature: Add dns cache customLogger <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Improved DNS cache lifecycle for more reliable startup and shutdown. * **Improvements** * Dedicated DNS cache logger for clearer diagnostics. * More consistent and informative DNS lookup/resolution logging, including TTL awareness. * Safer shutdown handling to avoid errors when DNS resolver is absent. * **Configuration** * DNS cache settings reorganized under a nested config section; test app logging level default reduced. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent c8c089b commit e9c1180

File tree

4 files changed

+52
-37
lines changed

4 files changed

+52
-37
lines changed

plugin/dns-cache/app.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ export default class DnsCacheAppHook {
1515
'[tegg-dns-cache-plugin] DNS cache is disabled, please setup dnsCache config.',
1616
);
1717
}
18+
}
19+
20+
async configDidLoad() {
1821
const config = this.app.config.dnsCache || {};
1922

2023
// Create DNS resolver instance
2124
const useDNSResolver = config.mode !== 'lookup';
25+
const dnsCacheLogger = this.app.coreLogger;
2226
this.dnsResolver = new DnsResolver(
2327
{
2428
useResolver: useDNSResolver,
@@ -27,14 +31,13 @@ export default class DnsCacheAppHook {
2731
dnsCacheLookupInterval: config.lookupInterval || 10000,
2832
addressRotation: config.addressRotation !== false,
2933
},
30-
{ logger: this.app.logger },
34+
{ logger: dnsCacheLogger },
3135
);
36+
3237
const lookupFunction = this.dnsResolver.getLookupFunction();
3338
this.app.config.httpclient = this.app.config.httpclient || {};
3439
this.app.config.httpclient.lookup = lookupFunction;
35-
}
3640

37-
configDidLoad() {
3841
// Add dnsResolver to app
3942
this.app.dnsResolver = this.dnsResolver;
4043
}
@@ -45,6 +48,8 @@ export default class DnsCacheAppHook {
4548

4649
beforeClose() {
4750
// Cleanup DNS cache resources
48-
this.dnsResolver.resetCache();
51+
if (this.app.dnsResolver) {
52+
this.app.dnsResolver.resetCache();
53+
}
4954
}
5055
}
Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
1-
/**
2-
* DNS Cache Configuration
3-
*
4-
* The DNS cache plugin provides DNS caching and resolution capabilities to improve performance
5-
* by caching DNS lookups and supports both dns.lookup and dns.resolve modes with address rotation.
6-
*
7-
* @property {'lookup' | 'resolve'} mode - Use dns.lookup or dns.resolve, default is 'resolve'.
8-
* - lookup: Use dns.lookup mode (old behavior), respects system DNS configuration and /etc/hosts, but does not support ttl.
9-
* - resolve: Use dns.resolve mode (new feature), queries DNS servers directly, ttl supported.
10-
* Note: When using resolve mode, /etc/hosts is not respected. You may need to implement custom logic
11-
* if you want to include /etc/hosts resolution.
12-
* @property {Number} maxCacheLength - Maximum number of DNS cache entries, default is 1000.
13-
* Uses LRU (Least Recently Used) algorithm to evict old entries when cache is full.
14-
* @property {Number} lookupInterval - Only works when mode is 'lookup'. DNS cache lookup interval in milliseconds, default is 10000 (10 seconds).
15-
* @property {Boolean} addressRotation - Enable round-robin address rotation when multiple IP addresses
16-
* are returned for a hostname, default is true. Helps distribute load across multiple servers.
17-
* @property {Array<String>} dnsServers - Custom DNS nameservers for dns.resolve mode, e.g. ['8.8.8.8', '1.1.1.1'].
18-
* Only effective when mode is 'resolve'. If not set, uses system default DNS servers.
19-
*/
1+
export default () => {
2+
const config = {
3+
/**
4+
* DNS Cache Configuration
5+
*
6+
* The DNS cache plugin provides DNS caching and resolution capabilities to improve performance
7+
* by caching DNS lookups and supports both dns.lookup and dns.resolve modes with address rotation.
8+
*
9+
* @property {'lookup' | 'resolve'} mode - Use dns.lookup or dns.resolve, default is 'resolve'.
10+
* - lookup: Use dns.lookup mode (old behavior), respects system DNS configuration and /etc/hosts, but does not support ttl.
11+
* - resolve: Use dns.resolve mode (new feature), queries DNS servers directly, ttl supported.
12+
* Note: When using resolve mode, /etc/hosts is not respected. You may need to implement custom logic
13+
* if you want to include /etc/hosts resolution.
14+
* @property {Number} maxCacheLength - Maximum number of DNS cache entries, default is 1000.
15+
* Uses LRU (Least Recently Used) algorithm to evict old entries when cache is full.
16+
* @property {Number} lookupInterval - Only works when mode is 'lookup'. DNS cache lookup interval in milliseconds, default is 10000 (10 seconds).
17+
* @property {Boolean} addressRotation - Enable round-robin address rotation when multiple IP addresses
18+
* are returned for a hostname, default is true. Helps distribute load across multiple servers.
19+
* @property {Array<String>} dnsServers - Custom DNS nameservers for dns.resolve mode, e.g. ['8.8.8.8', '1.1.1.1'].
20+
* Only effective when mode is 'resolve'. If not set, uses system default DNS servers.
21+
*/
22+
dnsCache: {
23+
mode: 'resolve' as 'lookup' | 'resolve',
24+
maxCacheLength: 1000,
25+
lookupInterval: 10000,
26+
addressRotation: true,
27+
dnsServers: undefined as string[] | undefined,
28+
},
29+
};
2030

21-
export const dnsCache = {
22-
mode: 'resolve' as 'lookup' | 'resolve',
23-
maxCacheLength: 1000,
24-
lookupInterval: 10000,
25-
addressRotation: true,
26-
dnsServers: undefined as string[] | undefined,
31+
return config;
2732
};

plugin/dns-cache/lib/DnsResolver.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class DnsResolver {
3636
private resolver?: dns.Resolver;
3737
private _resolve4?: typeof dns.resolve4.__promisify__;
3838
private _lookup?: typeof dns.lookup.__promisify__;
39-
logger: EggLogger;
39+
private logger: EggLogger;
4040

4141
/**
4242
* Create a DNS cache resolver instance
@@ -69,7 +69,7 @@ export class DnsResolver {
6969

7070
this.resetCache = this.resetCache.bind(this);
7171

72-
this.logger.debug(
72+
this._debugLog(
7373
`[dns-cache] DNS Resolver initialized in ${
7474
this.useResolver ? 'resolve' : 'lookup'
7575
} mode, maxCacheSize: ${this._maxCacheSize}, addressRotation: ${
@@ -328,7 +328,7 @@ export class DnsResolver {
328328
currentIndex: 0,
329329
};
330330
this._dnsCache.set(hostname, cacheEntry);
331-
this._debugLog(
331+
this.logger.info(
332332
`[dns-cache] dns.lookup succeeded for ${hostname}, resolved ${
333333
records.length
334334
} address(es): ${records.map(r => r.ip).join(', ')}, TTL: ${
@@ -352,6 +352,11 @@ export class DnsResolver {
352352
const address = typeof addr === 'string' ? addr : addr.address;
353353
const ttlSeconds =
354354
addr && Number.isInteger(addr.ttl) && addr.ttl >= 0 ? addr.ttl : 0;
355+
if (ttlSeconds === 0) {
356+
this.logger.warn(
357+
`[dns-cache] Warning: TTL is 0 for ${hostname} address ${address}`,
358+
);
359+
}
355360
return {
356361
ip: address,
357362
family: 4,
@@ -371,7 +376,7 @@ export class DnsResolver {
371376
currentIndex: 0,
372377
};
373378
this._dnsCache.set(hostname, cacheEntry);
374-
this._debugLog(
379+
this.logger.info(
375380
`[dns-cache] dns.resolve4 succeeded for ${hostname}, resolved ${
376381
records.length
377382
} address(es): ${records
@@ -393,7 +398,7 @@ export class DnsResolver {
393398
*/
394399
private _errorDNS(err: any, mode: 'lookup' | 'resolve', hostname: string) {
395400
this.logger.error(
396-
`error occurred when resolving ${hostname} with dns.${mode}: ${
401+
`[dns-cache] error occurred when resolving ${hostname} with dns.${mode}: ${
397402
err && err.message ? err.message : err
398403
}`,
399404
);

plugin/dns-cache/test/fixtures/apps/dns_cache_lookup/config/config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict';
22

3-
module.exports = function () {
3+
module.exports = function (appInfo) {
44
const config = {
55
keys: 'test key',
66
logger: {
77
level: 'DEBUG',
8-
consoleLevel: 'DEBUG',
8+
consoleLevel: 'NONE',
99
},
1010
httpclient: {
1111
httpAgent: {
@@ -18,7 +18,7 @@ module.exports = function () {
1818
lookupInterval: 3000,
1919
addressRotation: true,
2020
resolveLocalhost: false,
21-
}
21+
},
2222
}
2323
return config;
2424
};

0 commit comments

Comments
 (0)