mirror of
https://github.com/laurent22/joplin.git
synced 2025-01-11 18:24:43 +02:00
Server: Retry NTP request up to three times when it fails
This commit is contained in:
parent
920847245f
commit
7eb1d89d66
@ -1,4 +1,5 @@
|
|||||||
import shim from './shim';
|
import shim from './shim';
|
||||||
|
import time from './time';
|
||||||
const ntpClient_ = require('./vendor/ntp-client');
|
const ntpClient_ = require('./vendor/ntp-client');
|
||||||
|
|
||||||
const server = {
|
const server = {
|
||||||
@ -25,12 +26,24 @@ export async function getNetworkTime(): Promise<Date> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getDeviceTimeDrift(): Promise<number> {
|
export async function getDeviceTimeDrift(): Promise<number> {
|
||||||
|
const maxTries = 3;
|
||||||
|
let tryCount = 0;
|
||||||
|
|
||||||
let ntpTime: Date = null;
|
let ntpTime: Date = null;
|
||||||
try {
|
|
||||||
ntpTime = await getNetworkTime();
|
while (true) {
|
||||||
} catch (error) {
|
tryCount++;
|
||||||
error.message = `Cannot retrieve the network time: ${error.message}`;
|
try {
|
||||||
throw error;
|
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();
|
return ntpTime.getTime() - Date.now();
|
||||||
|
Loading…
Reference in New Issue
Block a user