From 708ff9e8af731e2799767ed8bfca7df3b74fc26a Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期五, 16 四月 2021 19:20:21 +0800
Subject: [PATCH] sendq use less shm, copy data.

---
 src/sendq.cpp |   40 +++++++++++++++++++++++++++-------------
 1 files changed, 27 insertions(+), 13 deletions(-)

diff --git a/src/sendq.cpp b/src/sendq.cpp
index ad293c3..4be24f1 100644
--- a/src/sendq.cpp
+++ b/src/sendq.cpp
@@ -19,11 +19,6 @@
 #include "shm_queue.h"
 #include <chrono>
 
-//TODO change to save head, body, instead of MsgI.
-// as MsgI which is in shm, but head, body are in current process.
-// Then if node crashes, shm will not be affected by msgs in sendq.
-// but pulishing ref-counted msg need some work.
-
 int SendQ::DoSend1Remote(bhome_shm::ShmMsgQueue &mq, const Remote &remote, Array &arr)
 {
 	auto FirstNotExpired = [](Array &l) {
@@ -35,22 +30,41 @@
 	for (auto it = arr.begin(); it != pos; ++it) {
 		auto &info = it->data();
 		if (info.on_expire_) {
-			info.on_expire_(info.msg_);
+			info.on_expire_(info.data_);
 		}
-		info.msg_.Release(mq.shm());
+		if (info.data_.index() == 0) {
+			boost::variant2::get<0>(info.data_).Release(mq.shm());
+		}
 	}
 
-	int n = mq.TrySendAll(*(MQId *) remote.data(), MsgIter(pos), MsgIter(arr.end()));
-	for (int i = 0; i < n; ++i) {
-		auto &msg = pos->data().msg_;
-		if (msg.IsCounted()) {
-			msg.Release(mq.shm());
+	auto SendData = [&](Data &d) {
+		bool r = false;
+		if (d.index() == 0) {
+			auto &msg = boost::variant2::get<0>(pos->data().data_);
+			r = mq.TrySend(*(MQId *) remote.data(), msg);
+			if (r && msg.IsCounted()) {
+				msg.Release(mq.shm());
+			}
+		} else {
+			auto &content = boost::variant2::get<1>(pos->data().data_);
+			MsgI msg;
+			if (msg.Make(mq.shm(), content)) {
+				r = mq.TrySend(*(MQId *) remote.data(), msg);
+				if (!r || msg.IsCounted()) {
+					msg.Release(mq.shm());
+				}
+			}
 		}
+		return r;
+	};
+
+	while (pos != arr.end() && SendData(pos->data().data_)) {
 		++pos;
 	}
 
+	int nprocessed = std::distance(arr.begin(), pos);
 	arr.erase(arr.begin(), pos);
-	return n;
+	return nprocessed;
 }
 
 int SendQ::DoSend1Remote(bhome_shm::ShmMsgQueue &mq, const Remote &remote, ArrayList &al)

--
Gitblit v1.8.0