You've already forked joplin
							
							
				mirror of
				https://github.com/laurent22/joplin.git
				synced 2025-10-31 00:07:48 +02:00 
			
		
		
		
	Server: Retry NTP request up to three times when it fails
This commit is contained in:
		| @@ -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(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user