Skip to content

Commit

Permalink
feat(ntpClient): Double wait time to next attempt on each sync error
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypfer committed Jan 29, 2024
1 parent e0ae732 commit 6b15920
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions backend/lib/NTPClient.js
Expand Up @@ -16,6 +16,7 @@ class NTPClient {
this.config = options.config;

this.nextPollTimeout = undefined;
this.retryWaitTime = BASE_RETRY_WAIT_TIME;

this.config.onUpdate((key) => {
if (key === "ntpClient") {
Expand All @@ -38,6 +39,7 @@ class NTPClient {

reconfigure() {
clearTimeout(this.nextPollTimeout);
this.retryWaitTime = BASE_RETRY_WAIT_TIME;
const ntpConfig = this.config.get("ntpClient");

if (ntpConfig.enabled === true) {
Expand Down Expand Up @@ -80,8 +82,9 @@ class NTPClient {
this.state = new States.ValetudoNTPClientSyncedState({
offset: currentNTPTime.getTime() - preSyncTime.getTime()
});
this.retryWaitTime = BASE_RETRY_WAIT_TIME;

Logger.debug("Next NTP sync in " + ntpConfig.interval + " ms");
Logger.debug(`Next NTP sync in ${ntpConfig.interval}ms`);

this.nextPollTimeout = setTimeout(() => {
this.pollTime().catch(() => {
Expand Down Expand Up @@ -125,13 +128,14 @@ class NTPClient {

this.state = new States.ValetudoNTPClientErrorState(error);

Logger.debug("Next NTP sync in " + FAILURE_RETRY_INTERVAL + " ms");

Logger.debug(`Next NTP sync attempt in ${this.retryWaitTime}ms`);
this.nextPollTimeout = setTimeout(() => {
this.pollTime().catch(() => {
/* intentional */
});
}, FAILURE_RETRY_INTERVAL);
}, this.retryWaitTime);

this.retryWaitTime = Math.min(this.retryWaitTime * 2, 8 * 60 * 60 * 1000);
}
}

Expand Down Expand Up @@ -178,6 +182,6 @@ class NTPClient {
}
}

const FAILURE_RETRY_INTERVAL = 60*1000; //1 Minute
const BASE_RETRY_WAIT_TIME = 60*1000; //1 Minute

module.exports = NTPClient;

0 comments on commit 6b15920

Please sign in to comment.