You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-08-13 22:12:50 +02:00
Desktop: Only repeat failed requests up to 3 times during sync
This commit is contained in:
@@ -17,17 +17,27 @@ function requestCanBeRepeated(error) {
|
||||
async function tryAndRepeat(fn, count) {
|
||||
let retryCount = 0;
|
||||
|
||||
// Don't use internal fetch retry mechanim since we
|
||||
// are already retrying here.
|
||||
const shimFetchMaxRetryPrevious = shim.fetchMaxRetrySet(0);
|
||||
const defer = () => {
|
||||
shim.fetchMaxRetrySet(shimFetchMaxRetryPrevious);
|
||||
}
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
const result = await fn();
|
||||
defer();
|
||||
return result;
|
||||
} catch (error) {
|
||||
if (retryCount >= count) throw error;
|
||||
if (!requestCanBeRepeated(error)) throw error;
|
||||
if (retryCount >= count || !requestCanBeRepeated(error)) {
|
||||
defer();
|
||||
throw error;
|
||||
}
|
||||
retryCount++;
|
||||
await time.sleep(1 + retryCount * 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class FileApi {
|
||||
|
@@ -104,12 +104,20 @@ shim.fetchRequestCanBeRetried = function(error) {
|
||||
return false;
|
||||
};
|
||||
|
||||
shim.fetchMaxRetry_ = 5;
|
||||
|
||||
shim.fetchMaxRetrySet = v => {
|
||||
const previous = shim.fetchMaxRetry_;
|
||||
shim.fetchMaxRetry_ = v;
|
||||
return previous;
|
||||
}
|
||||
|
||||
shim.fetchWithRetry = async function(fetchFn, options = null) {
|
||||
const { time } = require('lib/time-utils.js');
|
||||
|
||||
if (!options) options = {};
|
||||
if (!options.timeout) options.timeout = 1000 * 120; // ms
|
||||
if (!('maxRetry' in options)) options.maxRetry = 5;
|
||||
if (!('maxRetry' in options)) options.maxRetry = shim.fetchMaxRetry_;
|
||||
|
||||
let retryCount = 0;
|
||||
while (true) {
|
||||
|
Reference in New Issue
Block a user