From 5410f99f64ff27de87741bb1fe2986f61f630120 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nicolas.pope@utu.fi>
Date: Wed, 11 May 2022 08:02:49 +0100
Subject: [PATCH] Reduce buffer size and remove logs

---
 src/peer.cpp     | 32 +++++---------------------------
 src/peer.hpp     |  2 +-
 src/universe.cpp |  7 +------
 3 files changed, 7 insertions(+), 34 deletions(-)

diff --git a/src/peer.cpp b/src/peer.cpp
index b5f004d..b248217 100644
--- a/src/peer.cpp
+++ b/src/peer.cpp
@@ -174,7 +174,7 @@ bool Peer::reconnect() {
 
 	URI uri(uri_);
 
-	LOG(INFO) << "Reconnecting to " << uri_.to_string() << " ...";
+	LOG(1) << "Reconnecting to " << uri_.to_string() << " ...";
 
 	try {
 		_connect();
@@ -254,31 +254,17 @@ NodeType Peer::getType() const {
 }
 
 void Peer::data() {
-	//UNIQUE_LOCK(recv_mtx_,lk);
-
 	if (!sock_->is_valid()) { return; }
 
 	int rc = 0;
 
-	{
+	// Only need to lock and reserve buffer if there isn't enough
+	if (recv_buf_.buffer_capacity() < kMaxMessage) {
 		UNIQUE_LOCK(recv_mtx_,lk);
 		recv_buf_.reserve_buffer(kMaxMessage);
 	}
 
-	if (recv_buf_.buffer_capacity() < (kMaxMessage / 10)) {
-		net_->_notifyError(this, ftl::protocol::Error::kBufferSize, "Buffer is at capacity"); 
-		return;
-	}
-
 	int cap = static_cast<int>(recv_buf_.buffer_capacity());
-	// Buffer acquired, recv can be called outside the lock.
-
-	// TODO: Check if this is actually correct. If two threads call recv()
-	//       outside the lock and the second thread to call recv() re-acquires 
-	//       the lock first, buffer_consumed() will be called first with second
-	//       thread's number of bytes (rc).
-	//auto ctr = dbg_recv_begin_ctr_++;
-	//lk.unlock();
 
 	try {
 		rc = sock_->recv(recv_buf_.buffer(), recv_buf_.buffer_capacity());
@@ -308,16 +294,9 @@ void Peer::data() {
 		return;
 	}
 
-	// Re-acquire lock before processing buffer further
-	//lk.lock();
-
-	// buffer_consumed() will not be updated with correct value, race condition
-	// described above has occurred
-	//CHECK(ctr == dbg_recv_end_ctr_++) << "race in Peer::data()";
-
+	// May possibly need locking
 	recv_buf_.buffer_consumed(rc);
-	
-	//UNIQUE_LOCK(recv_mtx_, lk);
+
 	recv_checked_.clear();
 	if (!already_processing_.test_and_set()) {
 		//lk.unlock();
@@ -543,7 +522,6 @@ bool Peer::waitConnection(int s) {
 	});
 
 	cv.wait_for(lk, seconds(s), [this]() { return status_ == NodeStatus::kConnected;});
-	LOG(ERROR) << "CONN STAT = " << int(status_);
 	return status_ == NodeStatus::kConnected;
 }
 
diff --git a/src/peer.hpp b/src/peer.hpp
index 38082ad..88b6543 100644
--- a/src/peer.hpp
+++ b/src/peer.hpp
@@ -195,7 +195,7 @@ class Peer {
 	int connectionCount() const { return connection_count_; }
 	
 	public:
-	static const int kMaxMessage = 10*1024*1024;  // 10Mb currently
+	static const int kMaxMessage = 2*1024*1024;  // 10Mb currently
 	
 protected:
 	void data();			// Process one message from socket
diff --git a/src/universe.cpp b/src/universe.cpp
index d7207fb..f31551c 100644
--- a/src/universe.cpp
+++ b/src/universe.cpp
@@ -374,8 +374,6 @@ void Universe::_removePeer(PeerPtr &p) {
 			}
 		}
 
-		LOG(INFO) << "Remove peer: " << int(p->status());
-
 		if (p->status() == NodeStatus::kReconnecting) {
 			reconnects_.push_back({reconnect_attempts_, 1.0f, p});
 		} else {
@@ -427,7 +425,6 @@ std::list<PeerPtr> Universe::getPeers() const {
 }
 
 void Universe::_periodic() {
-	LOG(INFO) << "PERIODIC " << reconnects_.size();
 	auto i = reconnects_.begin();
 	while (i != reconnects_.end()) {
 
@@ -458,9 +455,7 @@ void Universe::_periodic() {
 		peer->status_ = NodeStatus::kConnecting;
 		i = reconnects_.erase(i);
 		//ftl::pool.push([peer](int id) {
-			if (!peer->reconnect()) {
-				LOG(INFO) << "Reconnect failed";
-			}
+			peer->reconnect();
 		//});
 
 		/*if ((*i).peer->reconnect()) {
-- 
GitLab