From 1933ca480c35490d83996a1d63a9aa585b0fd543 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 21 May 2018 03:12:17 +0200 Subject: Do not crash if ntp has been synced once and it fails next time (can happen when laptop lid is closed) --- src/Database.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'src/Database.cpp') 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(); } -- cgit v1.2.3