Browse Source

relay: use redis pipelines

Andrew Dolgov 9 months ago
parent
commit
a7cf5528b0
1 changed files with 22 additions and 17 deletions
  1. 22 17
      include/functions.php

+ 22 - 17
include/functions.php

@@ -37,9 +37,7 @@
 
 	define('URL_REGEXP', "/(([a-z]+):\/\/[(,_)'\[email protected]:\!%_\+.~#?&\/=]+)/i");
 
-	function message_id() {
-		global $redis;
-
+	function message_id($redis) {
 		return $redis->incr("ttirc_last_message_id");
 	}
 
@@ -629,10 +627,12 @@
 
 			$res = $sth->execute([$channel, $except_connection_id]);
 
-			while ($row = $sth->fetch()) {
-				$message_id = message_id();
+			$message_id = message_id($redis);
 
-				$redis->rpush("ttirc_messages,".$row['id'], json_encode([
+			$pipe = $redis->pipeline();
+
+			while ($row = $sth->fetch()) {
+				$pipe->rpush("ttirc_messages,".$row['id'], json_encode([
 					"id" => $message_id,
 					"sender" => $sender,
 					"message" => $message,
@@ -642,11 +642,13 @@
 					"channel" => $channel
 				]));
 
-				$redis->set("ttirc_last_message,".$row['owner_uid'], $message_id);
+				$pipe->set("ttirc_last_message,".$row['owner_uid'], $message_id);
 
-				$redis->publish("ttirc_connection," . $row["id"], "UPDATE");
-				$redis->publish("ttirc_user," . $row["owner_uid"], "UPDATE");
+				$pipe->publish("ttirc_connection," . $row["id"], "UPDATE");
+				$pipe->publish("ttirc_user," . $row["owner_uid"], "UPDATE");
 			}
+
+			$pipe->exec();
 		}
 	}
 
@@ -694,7 +696,7 @@
 
 			if ($channel == "---") {
 				$owner_uid = connection_owner($connection_id);
-				$message_id = message_id();
+				$message_id = message_id($redis);
 
 				$redis->rpush("ttirc_messages,$connection_id", json_encode([
 					"id" => $message_id,
@@ -718,11 +720,12 @@
 
 				$sth->execute([$channel]);
 
-				while ($row = $sth->fetch()) {
+				$message_id = message_id($redis);
 
-					$message_id = message_id();
+				$pipe = $redis->pipeline();
 
-					$redis->rpush("ttirc_messages,".$row['id'], json_encode([
+				while ($row = $sth->fetch()) {
+					$pipe->rpush("ttirc_messages,".$row['id'], json_encode([
 						"id" => $message_id,
 						"sender" => $sender,
 						"message" => $message,
@@ -733,11 +736,13 @@
 						"incoming" => true,
 					]));
 
-					$redis->set("ttirc_last_message,".$row['owner_uid'], $message_id);
+					$pipe->set("ttirc_last_message,".$row['owner_uid'], $message_id);
 
-					$redis->publish("ttirc_connection," . $row["id"], "UPDATE");
-					$redis->publish("ttirc_user," . $row["owner_uid"], "UPDATE");
+					$pipe->publish("ttirc_connection," . $row["id"], "UPDATE");
+					$pipe->publish("ttirc_user," . $row["owner_uid"], "UPDATE");
 				}
+
+				$pipe->exec();
 			}
 
 		} else {
@@ -1099,7 +1104,7 @@
 
 		global $redis;
 
-		$message_id = message_id();
+		$message_id = message_id($redis);
 
 		$redis->rpush("ttirc_messages,$connection_id", json_encode([
 			"id" => $message_id,