Code review for new DNS cache code

Prevent discarding DNS cache entries looked up during a passive
read. Related feedback:
https://github.com/uBlockOrigin/uBlock-issues/discussions/3376#discussioncomment-10711948

Add advanced setting `dnsCacheTTL` to control the TLL (in
seconds) of DNS cache entries. Default to 600 (10 minutes).
This commit is contained in:
Raymond Hill 2024-09-22 10:02:45 -04:00
parent 1c97ca10fc
commit e7c783cefa
No known key found for this signature in database
GPG key ID: 25E1490B761470C2
3 changed files with 11 additions and 6 deletions

View file

@ -52,9 +52,9 @@ vAPI.Net = class extends vAPI.Net {
this.pendingRequests = [];
this.dnsList = []; // ring buffer
this.dnsWritePtr = 0; // next write pointer in ring buffer
this.dnsMaxCount = 256; // max size of ring buffer
this.dnsMaxCount = 512; // max size of ring buffer
this.dnsDict = new Map(); // hn to index in ring buffer
this.dnsEntryTTL = 60000; // delay after which an entry is obsolete
this.dnsCacheTTL = 600; // TTL in seconds
this.canUncloakCnames = true;
this.cnameUncloakEnabled = true;
this.cnameIgnoreList = null;
@ -90,6 +90,9 @@ vAPI.Net = class extends vAPI.Net {
if ( 'cnameReplayFullURL' in options ) {
this.cnameReplayFullURL = options.cnameReplayFullURL === true;
}
if ( 'dnsCacheTTL' in options ) {
this.dnsCacheTTL = options.dnsCacheTTL;
}
if ( 'dnsResolveEnabled' in options ) {
this.dnsResolveEnabled = options.dnsResolveEnabled === true;
}
@ -143,7 +146,7 @@ vAPI.Net = class extends vAPI.Net {
canonicalNameFromHostname(hn) {
if ( hn === '' ) { return; }
const dnsEntry = this.dnsFromCache(hn);
const dnsEntry = this.dnsFromCache(hn, true);
if ( isResolvedObject(dnsEntry) === false ) { return; }
return dnsEntry.cname;
}
@ -212,7 +215,7 @@ vAPI.Net = class extends vAPI.Net {
}
dnsToCache(hn, record, details) {
const dnsEntry = { hn, until: Date.now() + this.dnsEntryTTL };
const dnsEntry = { hn, until: Date.now() + this.dnsCacheTTL * 1000 };
if ( record ) {
const cname = this.cnameFromRecord(hn, record, details);
if ( cname ) { dnsEntry.cname = cname; }
@ -223,13 +226,13 @@ vAPI.Net = class extends vAPI.Net {
return dnsEntry;
}
dnsFromCache(hn) {
dnsFromCache(hn, passive = false) {
const i = this.dnsDict.get(hn);
if ( i === undefined ) { return; }
if ( isPromise(i) ) { return i; }
const dnsEntry = this.dnsList[i];
if ( dnsEntry !== null && dnsEntry.hn === hn ) {
if ( dnsEntry.until >= Date.now() ) {
if ( passive || dnsEntry.until >= Date.now() ) {
return dnsEntry;
}
}

View file

@ -66,6 +66,7 @@ const hiddenSettingsDefault = {
debugScriptletInjector: false,
differentialUpdate: true,
disableWebAssembly: false,
dnsCacheTTL: 600,
dnsResolveEnabled: true,
extensionUpdateForceReload: false,
filterAuthorMode: false,

View file

@ -319,6 +319,7 @@ onBroadcast(msg => {
cnameIgnoreRootDocument: µbhs.cnameIgnoreRootDocument,
cnameMaxTTL: µbhs.cnameMaxTTL,
cnameReplayFullURL: µbhs.cnameReplayFullURL,
dnsCacheTTL: µbhs.dnsCacheTTL,
dnsResolveEnabled: µbhs.dnsResolveEnabled,
});
});