From b3b3d8a30afa74c806e9bbe094545c7267aad370 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Sat, 24 Aug 2019 09:28:57 +0300
Subject: [PATCH] Reduce locking

---
 components/net/cpp/src/universe.cpp | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/components/net/cpp/src/universe.cpp b/components/net/cpp/src/universe.cpp
index 034c41f1c..29cf1254f 100644
--- a/components/net/cpp/src/universe.cpp
+++ b/components/net/cpp/src/universe.cpp
@@ -39,6 +39,8 @@ Universe::Universe() :
 		reconnect_attempts_(50),
 		thread_(Universe::__start, this) {
 	_installBindings();
+
+	LOG(WARNING) << "Deprecated Universe constructor";
 }
 
 Universe::Universe(nlohmann::json &config) :
@@ -54,17 +56,18 @@ Universe::Universe(nlohmann::json &config) :
 
 	_installBindings();
 
-	LOG(INFO) << "SEND BUFFER SIZE = " << send_size_;
-
+	// Add an idle timer job to garbage collect peer objects
+	// Note: Important to be a timer job to ensure no other timer jobs are
+	// using the object.
 	ftl::timer::add(ftl::timer::kTimerIdle10, [this](int64_t ts) {
-		UNIQUE_LOCK(net_mutex_,lk);
-		if (ftl::pool.n_idle() == ftl::pool.size()) {
-			if (garbage_.size() > 0) LOG(INFO) << "Garbage collection";
-			while (garbage_.size() > 0) {
-				// FIXME: There is possibly still something with a peer pointer
-				// that is causing this throw an exception sometimes?
-				delete garbage_.front();
-				garbage_.pop_front();
+		if (garbage_.size() > 0) {
+			UNIQUE_LOCK(net_mutex_,lk);
+			if (ftl::pool.n_idle() == ftl::pool.size()) {
+				if (garbage_.size() > 0) LOG(INFO) << "Garbage collection";
+				while (garbage_.size() > 0) {
+					delete garbage_.front();
+					garbage_.pop_front();
+				}
 			}
 		}
 		return true;
-- 
GitLab