Skip to content

Commit

Permalink
check sync every 5 mins, default to ok.
Browse files Browse the repository at this point in the history
  • Loading branch information
smk762 committed Aug 27, 2024
1 parent 76f6036 commit 6dc7212
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/core/atomicdex/services/sync/timesync.checker.service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,22 @@ namespace
{
using namespace std::string_literals;
nlohmann::json resp;
bool sync_ok = false;
bool sync_ok = true;
std::string resp_str = TO_STD_STR(resp_http.extract_string(true).get());
if (resp_http.status_code() != 200)
{
SPDLOG_ERROR("Cannot reach the endpoint [{}]: {}", g_timesync_endpoint);
SPDLOG_ERROR("Cannot reach the endpoint [{}]: {}", g_timesync_endpoint, resp_str);
}
else
{
resp = nlohmann::json::parse(resp_str);
int64_t epoch_ts = resp["unixtime"];
int64_t current_ts = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
int64_t ts_diff = epoch_ts - current_ts;
if (abs(ts_diff) < 60)
if (abs(ts_diff) > 60)
{
sync_ok = true;
SPDLOG_WARN("Time sync failed! Actual: {}, System: {}, Diff: {}", epoch_ts, current_ts, ts_diff);
sync_ok = false;
}
}
return sync_ok;
Expand All @@ -82,7 +83,7 @@ namespace atomic_dex
int64_t m_timesync_clock_ts = std::chrono::duration_cast<std::chrono::seconds>(m_timesync_clock.time_since_epoch()).count();
int64_t now_ts = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
int64_t ts_diff = now_ts - m_timesync_clock_ts;
if (abs(ts_diff) >= 60)
if (abs(ts_diff) > 300)
{
fetch_timesync_status();
m_timesync_clock = std::chrono::high_resolution_clock::now();
Expand All @@ -91,21 +92,28 @@ namespace atomic_dex

void timesync_checker_service::fetch_timesync_status()
{
SPDLOG_INFO("Checking system time is in sync...");
if (is_timesync_fetching)
{
SPDLOG_WARN("Already checking timesync, returning");
return;
}
is_timesync_fetching = true;
emit isTimesyncFetchingChanged();
async_fetch_timesync()
.then([this](web::http::http_response resp) {
this->m_timesync_status = get_timesync_info_rpc(resp);
emit timesyncInfoChanged();
bool is_timesync_ok = get_timesync_info_rpc(resp);
SPDLOG_INFO("System time is in sync: {}", is_timesync_ok);

if (is_timesync_ok != *m_timesync_status)
{
this->m_timesync_status = is_timesync_ok;
emit timesyncInfoChanged();
}
})
.then(&handle_exception_pplx_task);
is_timesync_fetching = false;
emit isTimesyncFetchingChanged();

}

bool timesync_checker_service::get_timesync_info() const
Expand All @@ -115,4 +123,5 @@ namespace atomic_dex

} // namespace atomic_dex



0 comments on commit 6dc7212

Please sign in to comment.