From c10c4251b0b918b9df1667ebf9e2405175e0947d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Fri, 13 Apr 2018 15:34:23 -0400
Subject: [PATCH] proxy server: print stats to stdout once in a while

---
 include/opendht/dht_proxy_server.h | 2 ++
 src/dht_proxy_server.cpp           | 8 ++++++++
 2 files changed, 10 insertions(+)

diff --git a/include/opendht/dht_proxy_server.h b/include/opendht/dht_proxy_server.h
index a83b07ed..d0af6a87 100644
--- a/include/opendht/dht_proxy_server.h
+++ b/include/opendht/dht_proxy_server.h
@@ -230,6 +230,8 @@ private:
     Scheduler scheduler_;
     std::thread schedulerThread_;
 
+    Sp<Scheduler::Job> printStatsJob_;
+
     // Handle client quit for listen.
     // NOTE: can be simplified when we will supports restbed 5.0
     std::thread listenThread_;
diff --git a/src/dht_proxy_server.cpp b/src/dht_proxy_server.cpp
index e46d6e75..cd6623d9 100644
--- a/src/dht_proxy_server.cpp
+++ b/src/dht_proxy_server.cpp
@@ -45,6 +45,7 @@ struct DhtProxyServer::SearchPuts {
     std::map<dht::Value::Id, PermanentPut> puts;
 };
 
+constexpr const std::chrono::minutes PRINT_STATS_PERIOD {2};
 
 DhtProxyServer::DhtProxyServer(std::shared_ptr<DhtRunner> dht, in_port_t port , const std::string& pushServer)
 : dht_(dht) , pushServer_(pushServer)
@@ -122,6 +123,12 @@ DhtProxyServer::DhtProxyServer(std::shared_ptr<DhtRunner> dht, in_port_t port ,
         while (not service_->is_up() and not stopListeners) {
             std::this_thread::sleep_for(std::chrono::seconds(1));
         }
+        printStatsJob_ = scheduler_.add(scheduler_.time() + PRINT_STATS_PERIOD, [this]{
+            if (service_->is_up() and not stopListeners) {
+                std::cout << getStats().toString() << std::endl;
+                scheduler_.edit(printStatsJob_, scheduler_.time() + PRINT_STATS_PERIOD);
+            }
+        });
         while (service_->is_up()  and not stopListeners) {
             std::unique_lock<std::mutex> lock(schedulerLock_);
             auto next = scheduler_.run();
@@ -143,6 +150,7 @@ DhtProxyServer::~DhtProxyServer()
 void
 DhtProxyServer::stop()
 {
+    printStatsJob_->cancel();
     service_->stop();
     {
         std::lock_guard<std::mutex> lock(lockListener_);
-- 
GitLab