From 3839d05a994c8e30033119a71f8aec084cfc2c88 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Tue, 21 Jul 2020 07:50:36 -0400 Subject: [PATCH] Auto-update most obsolete asset first Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/1165 --- src/js/assets.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/js/assets.js b/src/js/assets.js index aac3126e1..a6be36169 100644 --- a/src/js/assets.js +++ b/src/js/assets.js @@ -884,7 +884,7 @@ const updateNext = async function() { ]); const now = Date.now(); - let assetKeyToUpdate; + const toUpdate = []; for ( const assetKey in assetDict ) { const assetEntry = assetDict[assetKey]; if ( assetEntry.hasRemoteURL !== true ) { continue; } @@ -902,23 +902,30 @@ const updateNext = async function() { type: assetEntry.content }) === true ) { - assetKeyToUpdate = assetKey; - break; + toUpdate.push(assetKey); + continue; } // This will remove a cached asset when it's no longer in use. if ( cacheEntry && cacheEntry.readTime < assetCacheRegistryStartTime ) { assetCacheRemove(assetKey); } } - if ( assetKeyToUpdate === undefined ) { + if ( toUpdate.length === 0 ) { return updateDone(); } - updaterFetched.add(assetKeyToUpdate); + // https://github.com/uBlockOrigin/uBlock-issues/issues/1165 + // Update most obsolete asset first. + toUpdate.sort((a, b) => { + const ta = cacheDict[a] !== undefined ? cacheDict[a].writeTime : 0; + const tb = cacheDict[b] !== undefined ? cacheDict[b].writeTime : 0; + return ta - tb; + }); + updaterFetched.add(toUpdate[0]); // In auto-update context, be gentle on remote servers. remoteServerFriendly = updaterAuto; - const result = await getRemote(assetKeyToUpdate); + const result = await getRemote(toUpdate[0]); remoteServerFriendly = false;