aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-05-21 03:12:17 +0200
committerdec05eba <dec05eba@protonmail.com>2020-08-18 23:25:46 +0200
commit1933ca480c35490d83996a1d63a9aa585b0fd543 (patch)
tree49efb9956fa96fc792f9b7c01fc5a0c14fd50392 /src
parent13df5e20c9afcf518bf6c4fa064ba681bd29f8e8 (diff)
Do not crash if ntp has been synced once and it fails next time (can happen when laptop lid is closed)
Diffstat (limited to 'src')
-rw-r--r--src/Database.cpp22
1 files changed, 18 insertions, 4 deletions
diff --git a/src/Database.cpp b/src/Database.cpp
index 27caba0..155e8f5 100644
--- a/src/Database.cpp
+++ b/src/Database.cpp
@@ -144,8 +144,9 @@ namespace odhtdb
delete ntpThread;
const int ntpFetchTimestampRetries = 5;
+ const int ntpFetchFailRetryCooldownSec = 60;
- ntpThread = new thread([]()
+ ntpThread = new thread([ntpFetchFailRetryCooldownSec]()
{
ntp::NtpClient ntpClient("pool.ntp.org", 10);
while(databaseCount > 0)
@@ -166,17 +167,30 @@ namespace odhtdb
catch(ntp::NtpClientException &e)
{
Log::warn("Failed to sync clock with ntp server, reason: %s. Try #%d", e.what(), fetchRetryCounter);
- this_thread::sleep_for(500ms);
+ if(timestampSynced)
+ this_thread::sleep_for(3s);
}
++fetchRetryCounter;
}
if(fetchRetryCounter == ntpFetchTimestampRetries)
- throw ntp::NtpClientException("Failed to retrieve ntp timestamp after several retries");
+ {
+ if(!timestampSynced)
+ {
+ string errMsg = "Failed to retrieve ntp timestamp after ";
+ errMsg += to_string(ntpFetchTimestampRetries);
+ errMsg += " retries, giving up";
+ throw ntp::NtpClientException(errMsg);
+ }
+ else
+ {
+ Log::warn("Failed to retrieve ntp timestamp after %d retries, retrying in %d seconds", ntpFetchTimestampRetries, ntpFetchFailRetryCooldownSec);
+ this_thread::sleep_for(chrono::seconds(ntpFetchFailRetryCooldownSec));
+ }
+ }
this_thread::sleep_for(60s);
}
- timestampSynced = false;
});
ntpThread->detach();
}