diff --git a/include/opendht/dht_proxy_server.h b/include/opendht/dht_proxy_server.h index c8594e21a8d494c354132a0bbc6d8ce9467fe045..4108411c57b4b9b49b3d2049bcbc5f690545fc47 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 564b2140b964e983e495d7c52c166e7884b3f8a4..74c571f6489967e70045c652f16a706b5e54a7e1 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)); }