Merge branch 'fetch-with-retries' into development

This commit is contained in:
ilyaigpetrov 2021-05-28 15:28:17 +00:00
commit 05903e32b3
2 changed files with 65 additions and 16 deletions

View File

@ -60,7 +60,38 @@
}; };
const tryPromiseSeveralTimesAsync = (createPromise, timeoutsInSec) =>
createPromise().then(
(res) => Promise.resolve(res),
(err) => {
console.log('Promise failed, are there any retries?');
const outSec = timeoutsInSec.shift();
if (outSec === undefined) {
console.log('No retries left.');
return Promise.reject(err);
}
console.log('Retrying in', outSec, 'sec');
/*
const alarmName = 'try-promise=several-times-async';
const res = new Promise((resolve) => {
chrome.alarms.onAlarm.addListener((alarmInfo) => {
if (alarmInfo.name === alarmName) {
console.log('Time to retry.');
resolve(tryPromiseSeveralTimesAsync(createPromise, timeoutsInSec));
}
});
});
chrome.alarms.create(alarmName, { delayInMinutes: outSec/60 });
return res;
*/
return new Promise((resolve) =>
window.setTimeout(() => resolve(tryPromiseSeveralTimesAsync(createPromise, timeoutsInSec)), outSec*1000),
);
},
);
const doWithoutProxyAsync = (createPromise) => new Promise((resolve, reject) => { const doWithoutProxyAsync = (createPromise) => new Promise((resolve, reject) => {
console.log('Doing without proxy...');
chrome.proxy.settings.get({}, chromified((getErr, settings) => { chrome.proxy.settings.get({}, chromified((getErr, settings) => {
if (getErr) { if (getErr) {
reject(getErr); reject(getErr);
@ -147,7 +178,7 @@
}; };
const setPacScriptFromProviderAsync = function setPacScriptFromProviderAsync( const setPacScriptFromProviderAsync = function setPacScriptFromProviderAsync(
provider, lastModifiedStr = mandatory(), cb = throwIfError, provider, lastModifiedStr, ifUnattended = mandatory(), cb = throwIfError,
) { ) {
const pacUrl = provider.pacUrls[0]; const pacUrl = provider.pacUrls[0];
@ -182,11 +213,10 @@
} }
console.log('Doing without proxy...'); doWithoutProxyAsync(
const pacDataPromise = doWithoutProxyAsync(
// Employ all urls, the latter are fallbacks for the former. // Employ all urls, the latter are fallbacks for the former.
() => () => {
provider.pacUrls.reduce( const tryAllUrlsAsync = () => provider.pacUrls.reduce(
(promise, url) => promise.catch( (promise, url) => promise.catch(
() => new Promise( () => new Promise(
(resolve, reject) => httpLib.get( (resolve, reject) => httpLib.get(
@ -197,13 +227,18 @@
), ),
), ),
Promise.reject(), Promise.reject(),
);
return (ifUnattended
? tryPromiseSeveralTimesAsync(tryAllUrlsAsync, [20, 40, 60])
: tryAllUrlsAsync()
).catch( ).catch(
(err) => Promise.reject(clarify( (err) => Promise.reject(clarify(
err, err,
chrome.i18n.getMessage('FailedToDownloadPacScriptFromAddresses') + ': [ ' chrome.i18n.getMessage('FailedToDownloadPacScriptFromAddresses') + ': [ '
+ provider.pacUrls.join(' , ') + ' ].', + provider.pacUrls.join(' , ') + ' ].',
)), )),
), );
},
).then( ).then(
(pacData) => { (pacData) => {
setPacAsync( setPacAsync(
@ -390,12 +425,13 @@
}, },
syncWithPacProviderAsync( syncWithPacProviderAsync(opts = {}, cb = throwIfError) {
key = this.currentPacProvierKey, cb = throwIfError) { const optsDefaults = Object.freeze({ key: this.getCurrentPacProviderKey(), ifUnattended: false });
if( typeof(key) === 'function' ) { if( typeof(opts) === 'function' ) {
cb = key; cb = opts;
key = this.getCurrentPacProviderKey(); opts = {};
} }
let { key, ifUnattended } = { ...optsDefaults, ...opts };
cb = asyncLogGroup('Syncing with PAC provider ' + key + '...', cb); cb = asyncLogGroup('Syncing with PAC provider ' + key + '...', cb);
if (key === null) { if (key === null) {
@ -409,6 +445,7 @@
(resolve, reject) => setPacScriptFromProviderAsync( (resolve, reject) => setPacScriptFromProviderAsync(
pacProvider, pacProvider,
this.getLastModifiedForKey(key), this.getLastModifiedForKey(key),
ifUnattended,
(err, res, ...warns) => { (err, res, ...warns) => {
if (!err) { if (!err) {
@ -425,12 +462,22 @@
) )
); );
const ipsErrorPromise = new Promise( const updateIpsAsync = () => new Promise(
(resolve, reject) => updatePacProxyIps( (resolve, reject) => updatePacProxyIps(
resolve, (err, res, ...warns) => {
if (err) {
reject([err, ...warns]);
return;
}
resolve(err, res, ...warns);
},
), ),
); );
const ipsErrorPromise = !ifUnattended
? updateIpsAsync()
: tryPromiseSeveralTimesAsync(updateIpsAsync, [20, 40, 60]);
Promise.all([pacSetPromise, ipsErrorPromise]).then( Promise.all([pacSetPromise, ipsErrorPromise]).then(
([[pacErr, pacRes, ...pacWarns], ipsErr]) => { ([[pacErr, pacRes, ...pacWarns], ipsErr]) => {
@ -492,7 +539,7 @@
throw new Error('Key must be defined.'); throw new Error('Key must be defined.');
} }
if (this.currentProviderKey !== key) { if (this.currentProviderKey !== key) {
return this.syncWithPacProviderAsync(key, cb); return this.syncWithPacProviderAsync({ key }, cb);
} }
console.log(key + ' already installed.'); console.log(key + ' already installed.');
cb(); cb();
@ -579,7 +626,7 @@
'Periodic PAC update triggered:', 'Periodic PAC update triggered:',
new Date().toLocaleString('ru-RU'), new Date().toLocaleString('ru-RU'),
); );
antiCensorRu.syncWithPacProviderAsync(() => { /* Swallow. */ }); antiCensorRu.syncWithPacProviderAsync({ ifUnattended: true }, () => { /* Swallow. */ });
} }
}) })

View File

@ -255,7 +255,9 @@
_updateAllAsync(cb = mandatory()) { _updateAllAsync(cb = mandatory()) {
const hostArr = Object.keys(privates._strToHostObj); const hostArr = Object.keys(privates._strToHostObj)
.filter((hostStr) => hostStr !== 'localhost');
console.log('Update all:', hostArr); console.log('Update all:', hostArr);
const promises = hostArr.map( const promises = hostArr.map(