dht_proxy_server: avoid mutex when getting /

The dht_proxy_server should be always up, so getNodesStats will
be pretty always up-to-date. We can add it into a job and avoid
to wait for this method to finish (and this method use an
internal lock) to have a quick answer about the status of the proxy.

Note: the update is forced during the initialization of the proxy
This commit is contained in:
Sébastien Blin committed 2018-05-30 14:25:46 -04:00
1 parent b7e483078a
commit 709a2c7b7c
2 files changed
+32 -5

No files matched your search

+25 -2
View File
@@ -30,6 +30,8 @@
#include <functional>
#include <limits>
#include <iostream>
using namespace std::placeholders;
namespace dht {
@@ -128,6 +130,16 @@ DhtProxyServer::DhtProxyServer(std::shared_ptr<DhtRunner> dht, in_port_t port ,
std::cout << getStats().toString() << std::endl;
scheduler_.edit(printStatsJob_, scheduler_.time() + PRINT_STATS_PERIOD);
}
if (not stopListeners) {
// Refresh stats cache
auto newIpv4Stats = dht_->getNodesStats(AF_INET);
auto newIpv6Stats = dht_->getNodesStats(AF_INET6);
{
std::lock_guard<std::mutex> lck(statsMutex_);
ipv4Stats_ = std::move(newIpv4Stats);
ipv6Stats_ = std::move(newIpv6Stats);
}
}
});
while (service_->is_up() and not stopListeners) {
std::unique_lock<std::mutex> lock(schedulerLock_);
@@ -205,8 +217,19 @@ DhtProxyServer::getNodeInfo(const std::shared_ptr<restbed::Session>& session) co
if (id)
result["id"] = id.toString();
result["node_id"] = dht_->getNodeId().toString();
result["ipv4"] = dht_->getNodesStats(AF_INET).toJson();
result["ipv6"] = dht_->getNodesStats(AF_INET6).toJson();
{
std::lock_guard<std::mutex> lck(statsMutex_);
if (ipv4Stats_.good_nodes == 0 && ipv6Stats_.good_nodes == 0) {
// NOTE: we want to avoid the disconnected state as much as possible
// So, if the node is disconnected, we should force the update of the cache
// and reconnect as soon as possible
// This should not happen much
ipv4Stats_ = dht_->getNodesStats(AF_INET);
ipv6Stats_ = dht_->getNodesStats(AF_INET);
}
result["ipv4"] = ipv4Stats_.toJson();
result["ipv6"] = ipv6Stats_.toJson();
}
result["public_ip"] = s->get_origin(); // [ipv6:ipv4]:port or ipv4:port
Json::StreamWriterBuilder wbuilder;
wbuilder["commentStyle"] = "None";