aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-04-29 14:17:30 +0200
committerdec05eba <dec05eba@protonmail.com>2020-08-18 23:25:46 +0200
commitdd0c526c726af7e20de9c82c6c68056f093fc7d1 (patch)
tree3bc1e93638f439d41ad9c66bd5b4f887cb0b030a /src
parenta43c7ba9fa28b04dab3ba1f77a9428b6cc26003d (diff)
Make ntp sync more robust with retries
Diffstat (limited to 'src')
-rw-r--r--src/Database.cpp26
1 files 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");
});
}
}