diff --git a/include/opendht/dhtrunner.h b/include/opendht/dhtrunner.h index 2687de11..2fa737cd 100644 --- a/include/opendht/dhtrunner.h +++ b/include/opendht/dhtrunner.h @@ -349,6 +349,7 @@ public: NodeStats getNodesStats(sa_family_t af) const; unsigned getNodesStats(sa_family_t af, unsigned *good_return, unsigned *dubious_return, unsigned *cached_return, unsigned *incoming_return) const; NodeInfo getNodeInfo() const; + void getNodeInfo(std::function)>); std::vector getNodeMessageStats(bool in = false) const; std::string getStorageLog() const; diff --git a/src/dht_proxy_server.cpp b/src/dht_proxy_server.cpp index f0ff8a0e..da41154f 100644 --- a/src/dht_proxy_server.cpp +++ b/src/dht_proxy_server.cpp @@ -378,13 +378,14 @@ DhtProxyServer::handlePrintStats(const asio::error_code &ec) if (auto dht = dht_) { // Refresh stats cache - auto newInfo = std::make_shared(dht->getNodeInfo()); - stats_ = updateStats(newInfo); - nodeInfo_ = newInfo; - auto json = newInfo->toJson(); - auto str = Json::writeString(jsonBuilder_, json); - if (logger_) - logger_->d("[proxy:server] [stats] %s", str.c_str()); + dht->getNodeInfo([this](std::shared_ptr newInfo){ + stats_ = updateStats(newInfo); + nodeInfo_ = newInfo; + if (logger_) { + auto str = Json::writeString(jsonBuilder_, newInfo->toJson()); + logger_->d("[proxy:server] [stats] %s", str.c_str()); + } + }); } printStatsTimer_->expires_at(printStatsTimer_->expiry() + PRINT_STATS_PERIOD); printStatsTimer_->async_wait(std::bind(&DhtProxyServer::handlePrintStats, this, std::placeholders::_1)); diff --git a/src/dhtrunner.cpp b/src/dhtrunner.cpp index 633e2897..ac55d5e6 100644 --- a/src/dhtrunner.cpp +++ b/src/dhtrunner.cpp @@ -486,6 +486,25 @@ DhtRunner::getNodeInfo() const { return info; } +void +DhtRunner::getNodeInfo(std::function)> cb) +{ + std::lock_guard lck(storage_mtx); + ongoing_ops++; + pending_ops_prio.emplace([cb = std::move(cb), this](SecureDht& dht){ + auto sinfo = std::make_shared(); + auto& info = *sinfo; + info.id = dht.getId(); + info.node_id = dht.getNodeId(); + info.ipv4 = dht.getNodesStats(AF_INET); + info.ipv6 = dht.getNodesStats(AF_INET6); + info.ongoing_ops = ongoing_ops; + cb(sinfo); + opEnded(); + }); + cv.notify_all(); +} + std::vector DhtRunner::getNodeMessageStats(bool in) const {