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

Add delay before calling reconnect when server is back up #46399

Merged
merged 4 commits into from
Jul 31, 2024
Merged
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
45 changes: 35 additions & 10 deletions src/libs/NetworkConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,26 @@ type ResponseJSON = {
let callbackID = 0;
const reconnectionCallbacks: Record<string, () => void> = {};

let isServerUp = true;
let wasServerDown = false;

/**
* Loop over all reconnection callbacks and fire each one
*/
const triggerReconnectionCallbacks = throttle(
(reason) => {
Log.info(`[NetworkConnection] Firing reconnection callbacks because ${reason}`);
Object.values(reconnectionCallbacks).forEach((callback) => {
callback();
});
let delay = 0;

if (wasServerDown && isServerUp) {
delay = Math.floor(Math.random() * 61000);
wasServerDown = false;
}
setTimeout(() => {
Log.info(`[NetworkConnection] Firing reconnection callbacks because ${reason}`);
Object.values(reconnectionCallbacks).forEach((callback) => {
callback();
});
}, delay);
},
5000,
{trailing: false},
Expand Down Expand Up @@ -70,12 +81,12 @@ Onyx.connect({
} else {
// If we are no longer forcing offline fetch the NetInfo to set isOffline appropriately
NetInfo.fetch().then((state) => {
const isInternetReachable = (state.isInternetReachable ?? false) === false;
setOfflineStatus(isInternetReachable, 'NetInfo checked if the internet is reachable');
const isInternetUnreachable = (state.isInternetReachable ?? false) === false;
setOfflineStatus(isInternetUnreachable || !isServerUp, 'NetInfo checked if the internet is reachable');
Log.info(
`[NetworkStatus] The force-offline mode was turned off. Getting the device network status from NetInfo. Network state: ${JSON.stringify(
state,
)}. Setting the offline status to: ${isInternetReachable}.`,
)}. Setting the offline status to: ${isInternetUnreachable}.`,
);
});
}
Expand Down Expand Up @@ -116,8 +127,22 @@ function subscribeToNetInfo(): () => void {
}
return response
.json()
.then((json: ResponseJSON) => Promise.resolve(json.jsonCode === 200))
.catch(() => Promise.resolve(false));
.then((json: ResponseJSON) => {
if (json.jsonCode !== 200 && isServerUp) {
Log.info('[NetworkConnection] Received non-200 response from reachability test. Setting isServerUp status to false.');
isServerUp = false;
wasServerDown = true;
} else if (json.jsonCode === 200 && !isServerUp) {
Log.info('[NetworkConnection] Received 200 response from reachability test. Setting isServerUp status to true.');
isServerUp = true;
}
return Promise.resolve(json.jsonCode === 200);
})
.catch(() => {
isServerUp = false;
wasServerDown = true;
return Promise.resolve(false);
});
},

// If a check is taking longer than this time we're considered offline
Expand All @@ -133,7 +158,7 @@ function subscribeToNetInfo(): () => void {
Log.info('[NetworkConnection] Not setting offline status because shouldForceOffline = true');
return;
}
setOfflineStatus(state.isInternetReachable === false, 'NetInfo received a state change event');
setOfflineStatus(state.isInternetReachable === false || !isServerUp, 'NetInfo received a state change event');
Log.info(`[NetworkStatus] NetInfo.addEventListener event coming, setting "offlineStatus" to ${!!state.isInternetReachable} with network state: ${JSON.stringify(state)}`);
let networkStatus;
if (!isBoolean(state.isInternetReachable)) {
Expand Down
Loading