aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <0xdec05eba@gmail.com>2018-05-21 03:12:17 +0200
committerdec05eba <0xdec05eba@gmail.com>2018-05-21 03:12:21 +0200
commit72e5d3e6495fad1edbc3b8c4f7eb4941dbd873bd (patch)
treea0447cd290580f8881ef58ca86b8df2329197742 /src
parente1315807021fe4f21d145195b012a9d0cb3feb2e (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();
}