1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Server: Retry NTP request up to three times when it fails

This commit is contained in:
Laurent Cozic 2021-11-28 16:58:39 +00:00
parent 920847245f
commit 7eb1d89d66

View File

@ -1,4 +1,5 @@
import shim from './shim';
import time from './time';
const ntpClient_ = require('./vendor/ntp-client');
const server = {
@ -25,12 +26,24 @@ export async function getNetworkTime(): Promise<Date> {
}
export async function getDeviceTimeDrift(): Promise<number> {
const maxTries = 3;
let tryCount = 0;
let ntpTime: Date = null;
try {
ntpTime = await getNetworkTime();
} catch (error) {
error.message = `Cannot retrieve the network time: ${error.message}`;
throw error;
while (true) {
tryCount++;
try {
ntpTime = await getNetworkTime();
break;
} catch (error) {
if (tryCount >= maxTries) {
error.message = `Cannot retrieve the network time: ${error.message}`;
throw error;
} else {
await time.msleep(tryCount * 1000);
}
}
}
return ntpTime.getTime() - Date.now();