Aggressively update assets when at least one is very obsolete

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/1165
This commit is contained in:
Raymond Hill 2023-03-26 20:32:51 -04:00
parent a8a46c4593
commit f7f776fb8a
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 30 additions and 8 deletions

View File

@ -1463,10 +1463,11 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
/******************************************************************************/
µb.scheduleAssetUpdater = (( ) => {
{
let timer, next = 0;
return function(updateDelay) {
µb.scheduleAssetUpdater = async function(updateDelay) {
if ( timer ) {
clearTimeout(timer);
timer = undefined;
@ -1475,24 +1476,45 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
next = 0;
return;
}
const assetDict = await io.metadata();
const now = Date.now();
let needEmergencyUpdate = false;
for ( const asset of Object.values(assetDict) ) {
if ( asset.hasRemoteURL !== true ) { continue; }
if ( asset.content === 'filters' && asset.off === true ) { continue; }
if ( asset.obsolete !== true ) { continue; }
const lastUpdateInDays = (now - asset.writeTime) / 86400000;
const daysSinceVeryObsolete = lastUpdateInDays - 2 * asset.updateAfter;
if ( daysSinceVeryObsolete < 0 ) { continue; }
needEmergencyUpdate = true;
break;
}
// Use the new schedule if and only if it is earlier than the previous
// one.
if ( next !== 0 ) {
updateDelay = Math.min(updateDelay, Math.max(next - now, 0));
}
if ( needEmergencyUpdate ) {
updateDelay = Math.min(updateDelay, 15000);
}
next = now + updateDelay;
const fetchDelay = needEmergencyUpdate
? 2000
: this.hiddenSettings.autoUpdateAssetFetchPeriod * 1000 || 60000;
timer = vAPI.setTimeout(( ) => {
timer = undefined;
next = 0;
io.updateStart({
delay: this.hiddenSettings.autoUpdateAssetFetchPeriod * 1000 ||
120000,
auto: true,
});
io.updateStart({ delay: fetchDelay, auto: true });
}, updateDelay);
};
})();
}
/******************************************************************************/