proxy server: update stats on startup

This commit is contained in:
Adrien Béraud committed 2020-01-19 14:32:57 -05:00
1 parent 7b8ebbc54d
commit ec8beec427
2 files changed
+15 -18

No files matched your search

+1
View File
@@ -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;
+14 -18
View File
@@ -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));
}