-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import sbp from '@sbp/sbp' | ||
|
||
let wallBase = Date.now() | ||
let monotonicBase = performance.now() | ||
// `undefined` means the sync process has been stopped, `null` that the current | ||
// request has finished | ||
let resyncTimeout | ||
let watchdog | ||
|
||
const syncServerTime = async function () { | ||
// Get our current monotonic time | ||
const newMonotonicBase = performance.now() | ||
// Now, ask the server for the time | ||
const time = await fetch(`${this.config.connectionURL}/time`, { signal: this.abortController.signal }) | ||
const requestTimeElapsed = performance.now() | ||
if (requestTimeElapsed - newMonotonicBase > 1000) { | ||
throw new Error('Error fetching server time: request took too long') | ||
} | ||
// If the request didn't succeed, report it | ||
if (!time.ok) throw new Error('Error fetching server time') | ||
const serverTime = (new Date(await time.text())).valueOf() | ||
// If the value could not be parsed, report that as well | ||
if (Number.isNaN(serverTime)) throw new Error('Unable to parse server time') | ||
wallBase = serverTime | ||
monotonicBase = newMonotonicBase | ||
} | ||
|
||
export default (sbp('sbp/selectors/register', { | ||
'chelonia/private/startClockSync': function () { | ||
// Default re-sync every 5 minutes | ||
const resync = (delay: number = 300000) => { | ||
// If there's another time sync process in progress, don't do anything | ||
if (resyncTimeout !== null) return | ||
const timeout = setTimeout(() => { | ||
// Get the server time | ||
syncServerTime.call(this).then(() => { | ||
// Mark the process as finished | ||
if (resyncTimeout === timeout) resyncTimeout = null | ||
// And then restart the listener | ||
resync() | ||
}).catch(e => { | ||
// If there was an error, log it and possibly attempt again | ||
if (resyncTimeout === timeout) { | ||
// In this case, it was the current task that failed | ||
resyncTimeout = null | ||
console.error('Error re-syncing server time; will re-attempt in 5s', e) | ||
// Call resync again, with a shorter delay | ||
setTimeout(() => resync(0), 5000) | ||
} else { | ||
// If there is already another attempt, just log it | ||
console.error('Error re-syncing server time; another attempt is in progress', e) | ||
} | ||
}) | ||
}, delay) | ||
resyncTimeout = timeout | ||
} | ||
|
||
let wallLast = Date.now() | ||
let monotonicLast = performance.now() | ||
|
||
// Watchdog to ensure our time doesn't drift. Periodically check for | ||
// differences between the elapsed wall time and the elapsed monotonic | ||
// time | ||
watchdog = setInterval(() => { | ||
const wallNow = Date.now() | ||
const monotonicNow = performance.now() | ||
const difference = Math.abs(Math.abs((wallNow - wallLast)) - Math.abs((monotonicNow - monotonicLast))) | ||
// Tolerate up to a 10ms difference | ||
if (difference > 10) { | ||
clearTimeout(resyncTimeout) | ||
resyncTimeout = null | ||
resync(0) | ||
} | ||
wallLast = wallNow | ||
monotonicLast = monotonicNow | ||
}, 10000) | ||
|
||
// Start the sync process | ||
resyncTimeout = null | ||
resync(0) | ||
}, | ||
'chelonia/private/stopClockSync': () => { | ||
if (resyncTimeout !== undefined) { | ||
clearInterval(watchdog) | ||
clearTimeout(resyncTimeout) | ||
watchdog = undefined | ||
resyncTimeout = undefined | ||
} | ||
}, | ||
'chelonia/time': function () { | ||
const monotonicNow = performance.now() | ||
const wallNow = wallBase - monotonicBase + monotonicNow | ||
return (wallNow / 1e3 | 0) | ||
} | ||
}): string[]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters