core: fail fast if sync connection fails

this adds reslience to the sync logic in cases where network
isn't responding or is very slow to respond. Previously there were
cases where the sync would get stuck in Connecting state.
This will no longer happen.
This commit is contained in:
Abdullah Atta
2022-09-19 16:20:12 +05:00
parent 9668b304da
commit ca0d746966

View File

@@ -405,13 +405,30 @@ class Sync {
async checkConnection() {
try {
if (this.connection.state !== signalr.HubConnectionState.Connected) {
if (this.connection.state !== signalr.HubConnectionState.Disconnected)
if (this.connection.state !== signalr.HubConnectionState.Disconnected) {
await this.connection.stop();
}
await this.connection.start();
await promiseTimeout(15000, this.connection.start());
}
} catch (e) {
this.connection.stop();
this.logger.warn(e.message);
throw new Error(
"Could not connect to the Sync server. Please try again."
);
}
}
}
function promiseTimeout(ms, promise) {
// Create a promise that rejects in <ms> milliseconds
let timeout = new Promise((resolve, reject) => {
let id = setTimeout(() => {
clearTimeout(id);
reject(new Error("Sync timed out in " + ms + "ms."));
}, ms);
});
// Returns a race between our timeout and the passed in promise
return Promise.race([promise, timeout]);
}