Add retries (not tested)

This commit is contained in:
ilyaigpetrov 2021-05-22 17:38:32 +00:00
parent da7701393e
commit 699bef6fea
2 changed files with 57 additions and 14 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) => {
console.log('Doing without proxy...');
chrome.proxy.settings.get({}, chromified((getErr, settings) => {
if (getErr) {
reject(getErr);
@ -147,7 +178,7 @@
};
const setPacScriptFromProviderAsync = function setPacScriptFromProviderAsync(
provider, lastModifiedStr = mandatory(), cb = throwIfError,
provider, lastModifiedStr, ifUnattended = mandatory(), cb = throwIfError,
) {
const pacUrl = provider.pacUrls[0];
@ -182,11 +213,10 @@
}
console.log('Doing without proxy...');
const pacDataPromise = doWithoutProxyAsync(
doWithoutProxyAsync(
// Employ all urls, the latter are fallbacks for the former.
() =>
provider.pacUrls.reduce(
() => {
const tryAllUrlsAsync = () => provider.pacUrls.reduce(
(promise, url) => promise.catch(
() => new Promise(
(resolve, reject) => httpLib.get(
@ -197,13 +227,18 @@
),
),
Promise.reject(),
);
return (ifUnattended
? tryPromiseSeveralTimesAsync(tryAllUrlsAsync, [10, 20, 30])
: tryAllUrlsAsync()
).catch(
(err) => Promise.reject(clarify(
err,
chrome.i18n.getMessage('FailedToDownloadPacScriptFromAddresses') + ': [ '
+ provider.pacUrls.join(' , ') + ' ].',
)),
),
);
},
).then(
(pacData) => {
setPacAsync(
@ -240,7 +275,7 @@
<br/> <a href="https://rebrand.ly/ac-pacs">Comparison of PAC-scripts (ru)</a>.
\`,
order: 0,
pacUrls: ['https://antizapret.prostovpn.org/proxy.pac'],
pacUrls: ['https://AAAAAAAAAantizapret.prostovpn.org/proxy.pac'],
},
Антицензорити: {
distinctKey: 'Anticensority',
@ -390,12 +425,13 @@
},
syncWithPacProviderAsync(
key = this.currentPacProvierKey, cb = throwIfError) {
if( typeof(key) === 'function' ) {
cb = key;
key = this.getCurrentPacProviderKey();
syncWithPacProviderAsync(opts = {}, cb = throwIfError) {
const optsDefaults = Object.freeze({ key: this.getCurrentPacProviderKey(), ifUnattended: false });
if( typeof(opts) === 'function' ) {
cb = opts;
opts = {};
}
let { key, ifUnattended } = { ...optsDefaults, ...opts };
cb = asyncLogGroup('Syncing with PAC provider ' + key + '...', cb);
if (key === null) {
@ -405,10 +441,13 @@
const pacProvider = this.getPacProvider(key);
ifUnattended = true; // TODO:
const pacSetPromise = new Promise(
(resolve, reject) => setPacScriptFromProviderAsync(
pacProvider,
this.getLastModifiedForKey(key),
ifUnattended,
(err, res, ...warns) => {
if (!err) {
@ -425,12 +464,16 @@
)
);
const ipsErrorPromise = new Promise(
const updateIpsAsync = () => new Promise(
(resolve, reject) => updatePacProxyIps(
resolve,
),
);
const ipsErrorPromise = !ifUnattended
? updateIpsAsync()
: tryPromiseSeveralTimesAsync(updateIpsAsync, [10, 20, 30]);
Promise.all([pacSetPromise, ipsErrorPromise]).then(
([[pacErr, pacRes, ...pacWarns], ipsErr]) => {

View File

@ -112,7 +112,7 @@
const promises = types.map(
(type) => new Promise((resolve) =>
httpLib.get(
`https://dns.google.com/resolve?type=${type}&name=${host}&random_padding=${generateRandomHexString(30,500)}`,
`https://AAAAAAAAAdns.google.com/resolve?type=${type}&name=${host}&random_padding=${generateRandomHexString(30,500)}`,
(err, res) => {
if (res) {