From ec8beec427b320ed3e5cee7cb716b5fb5e69c398 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com> Date: Sun, 19 Jan 2020 13:36:44 -0500 Subject: [PATCH] proxy server: update stats on startup --- include/opendht/dht_proxy_server.h | 1 + src/dht_proxy_server.cpp | 32 +++++++++++++----------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/include/opendht/dht_proxy_server.h b/include/opendht/dht_proxy_server.h index c8594e21..4108411c 100644 --- a/include/opendht/dht_proxy_server.h +++ b/include/opendht/dht_proxy_server.h @@ -307,6 +307,7 @@ private: #endif //OPENDHT_PUSH_NOTIFICATIONS void handlePrintStats(const asio::error_code &ec); + void updateStats(); using clock = std::chrono::steady_clock; using time_point = clock::time_point; diff --git a/src/dht_proxy_server.cpp b/src/dht_proxy_server.cpp index 564b2140..74c571f6 100644 --- a/src/dht_proxy_server.cpp +++ b/src/dht_proxy_server.cpp @@ -272,6 +272,7 @@ DhtProxyServer::DhtProxyServer( }); } dht->forwardAllMessages(true); + updateStats(); printStatsTimer_->async_wait(std::bind(&DhtProxyServer::handlePrintStats, this, std::placeholders::_1)); } @@ -363,29 +364,24 @@ DhtProxyServer::updateStats(std::shared_ptr<NodeInfo> info) const return sstats; } +void +DhtProxyServer::updateStats() { + dht_->getNodeInfo([this](std::shared_ptr<NodeInfo> newInfo){ + stats_ = updateStats(newInfo); + nodeInfo_ = newInfo; + if (logger_) { + auto str = Json::writeString(jsonBuilder_, newInfo->toJson()); + logger_->d("[proxy:server] [stats] %s", str.c_str()); + } + }); +} + void DhtProxyServer::handlePrintStats(const asio::error_code &ec) { if (ec == asio::error::operation_aborted) return; - else if (ec){ - if (logger_) - logger_->e("[proxy:server] [stats] error printing: %s", ec.message().c_str()); - } - if (io_context().stopped()) - return; - - if (auto dht = dht_) { - // Refresh stats cache - dht->getNodeInfo([this](std::shared_ptr<NodeInfo> newInfo){ - stats_ = updateStats(newInfo); - nodeInfo_ = newInfo; - if (logger_) { - auto str = Json::writeString(jsonBuilder_, newInfo->toJson()); - logger_->d("[proxy:server] [stats] %s", str.c_str()); - } - }); - } + updateStats(); printStatsTimer_->expires_at(printStatsTimer_->expiry() + PRINT_STATS_PERIOD); printStatsTimer_->async_wait(std::bind(&DhtProxyServer::handlePrintStats, this, std::placeholders::_1)); } -- GitLab