From 9519d5fc682f83b2a4d78fe8f66b169a8c9314eb Mon Sep 17 00:00:00 2001 From: dec05eba <0xdec05eba@gmail.com> Date: Sun, 29 Apr 2018 14:17:30 +0200 Subject: Make ntp sync more robust with retries --- src/Database.cpp | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/Database.cpp b/src/Database.cpp index 492cc7d..cf72c6a 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -113,15 +113,29 @@ namespace odhtdb { if(ntpThread) delete ntpThread; + + const int ntpFetchTimestampRetries = 5; ntpThread = new thread([]() { ntp::NtpClient ntpClient("pool.ntp.org"); while(databaseCount > 0) { - ntp::NtpTimestamp ntpTimestamp = ntpClient.getTimestamp(); - timeOffset = time(nullptr) - ntpTimestamp.seconds; - timestampSynced = true; + for(int i = 0; i < ntpFetchTimestampRetries; ++i) + { + try + { + ntp::NtpTimestamp ntpTimestamp = ntpClient.getTimestamp(); + timeOffset = time(nullptr) - ntpTimestamp.seconds; + timestampSynced = true; + break; + } + catch(ntp::NtpClientException &e) + { + Log::warn("Failed to sync clock with ntp server, reason: %s. Try #%d", e.what(), i); + this_thread::sleep_for(500ms); + } + } // TODO: Also use timestamp fraction (milliseconds) this_thread::sleep_for(60s); } @@ -200,7 +214,7 @@ namespace odhtdb auto requestedData = databaseStorage.getStorage(*nodeToSeed.getRequestHash()); if(!requestedData) { - Log::warn("No data found for hash %s, unable to serve peer", nodeToSeed.getRequestHash()->toString().c_str()); + Log::debug("No data found for hash %s, unable to serve peer", nodeToSeed.getRequestHash()->toString().c_str()); return true; } @@ -212,7 +226,7 @@ namespace odhtdb node.put(requestResponseInfoHash, Value((u8*)requestedData->data.data, requestedData->data.size), [](bool ok) { if(!ok) - Log::warn("Failed to put response for old data for 'create' data"); + Log::error("Failed to put response for old data for 'create' data"); }); } @@ -221,7 +235,7 @@ namespace odhtdb node.put(requestResponseInfoHash, Value((u8*)requestedObject->data.data, requestedObject->data.size), [](bool ok) { if(!ok) - Log::warn("Failed to put response for old data for 'add' data"); + Log::error("Failed to put response for old data for 'add' data"); }); } } -- cgit v1.2.3