diff options
author | dec05eba <0xdec05eba@gmail.com> | 2018-04-29 14:17:30 +0200 |
---|---|---|
committer | dec05eba <0xdec05eba@gmail.com> | 2018-04-29 14:17:35 +0200 |
commit | 9519d5fc682f83b2a4d78fe8f66b169a8c9314eb (patch) | |
tree | 4bfa856c6b0de8669d5c1e4652157e1b7035e7f9 /src | |
parent | 483c2e1825971b28ff1b6a28c5c37c42ff2ab063 (diff) |
Make ntp sync more robust with retries
Diffstat (limited to 'src')
-rw-r--r-- | src/Database.cpp | 26 |
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"); }); } } |