Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NoQA] Add max retry request #28365

Merged
merged 3 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ const CONST = {
MAX_RETRY_WAIT_TIME_MS: 10 * 1000,
PROCESS_REQUEST_DELAY_MS: 1000,
MAX_PENDING_TIME_MS: 10 * 1000,
MAX_REQUEST_RETRIES: 10,
},
DEFAULT_TIME_ZONE: {automatic: true, selected: 'America/Los_Angeles'},
DEFAULT_ACCOUNT_DATA: {errors: null, success: '', isLoading: false},
Expand Down
9 changes: 8 additions & 1 deletion src/libs/Network/SequentialQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,14 @@ function process() {
RequestThrottle.clear();
return process();
}
return RequestThrottle.sleep().then(process);
return RequestThrottle.sleep()
.then(process)
.catch(() => {
Onyx.update(requestToProcess.failureData);
PersistedRequests.remove(requestToProcess);
RequestThrottle.clear();
return process();
});
});
return currentRequest;
}
Expand Down
10 changes: 9 additions & 1 deletion src/libs/RequestThrottle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import CONST from '../CONST';
import {generateRandomInt} from './NumberUtils';

let requestWaitTime = 0;
let requestRetryCount = 0;

function clear() {
requestWaitTime = 0;
requestRetryCount = 0;
}

function getRequestWaitTime() {
Expand All @@ -21,7 +23,13 @@ function getLastRequestWaitTime() {
}

function sleep(): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, getRequestWaitTime()));
requestRetryCount++;
return new Promise((resolve, reject) => {
if (requestRetryCount <= CONST.NETWORK.MAX_REQUEST_RETRIES) {
return setTimeout(resolve, getRequestWaitTime());
}
return reject();
});
}

export {clear, getRequestWaitTime, sleep, getLastRequestWaitTime};