Browse Source

evtsource: return immediately if stream is restarted with events pending

Andrew Dolgov 4 months ago
parent
commit
71c0f07864
1 changed files with 29 additions and 26 deletions
  1. 29 26
      backend.php

+ 29 - 26
backend.php

@@ -287,42 +287,45 @@
 		$started = time();
 		while (time() - $started <= UPDATE_DELAY_MAX) {
 
-			try {
-				// we need to use a separate redis client to wait on subscription for some reason
-				$sredis = new Credis_Client(REDIS_SERVER);
+			// if stream was restarted with new events pending, we shouldn't wait
+			if (!has_new_lines($last_id)) {
+				try {
+					// we need to use a separate redis client to wait on subscription for some reason
+					$sredis = new Credis_Client(REDIS_SERVER);
 
-				$sredis->setReadTimeout(PING_INTERVAL);
-				$sredis->subscribe("ttirc_user," . $_SESSION["uid"], function($sredis, $channel, $message) use ($redis, &$last_id, $uniqid, $bufsize) {
+					$sredis->setReadTimeout(PING_INTERVAL);
+					$sredis->subscribe("ttirc_user," . $_SESSION["uid"], function($sredis, $channel, $message) use ($redis, &$last_id, $uniqid, $bufsize) {
 
-					print "event: update\n";
-					print "retry: 1000\n";
+						print "event: update\n";
+						print "retry: 1000\n";
 
-					$lines = get_new_lines($last_id, $bufsize);
-					$conn = get_conn_info();
-					$chandata = get_chan_data();
-					$params = get_misc_params($uniqid);
+						$lines = get_new_lines($last_id, $bufsize);
+						$conn = get_conn_info();
+						$chandata = get_chan_data();
+						$params = get_misc_params($uniqid);
 
-					foreach ($lines as $line)
-						if ($line['id'] > $last_id)
-							$last_id = $line['id'];
+						foreach ($lines as $line)
+							if ($line['id'] > $last_id)
+								$last_id = $line['id'];
 
-					if ($uniqid)
-						mark_duplicate_objects($uniqid, $conn, $chandata, $params);
+						if ($uniqid)
+							mark_duplicate_objects($uniqid, $conn, $chandata, $params);
 
+						print "id: $last_id\n";
+						print "data:" . json_encode(array($conn, $lines, $chandata, $params));
+						print "\n\n";
+
+						flush();
+					});
+				} catch (CredisException $e) {
+					print "event: ping\n";
+					print "retry: 1000\n";
 					print "id: $last_id\n";
-					print "data:" . json_encode(array($conn, $lines, $chandata, $params));
+					print "data:" . json_encode(["ts" => time()]);
 					print "\n\n";
 
 					flush();
-				});
-			} catch (CredisException $e) {
-				print "event: ping\n";
-				print "retry: 1000\n";
-				print "id: $last_id\n";
-				print "data:" . json_encode(["ts" => time()]);
-				print "\n\n";
-
-				flush();
+				}
 			}
 
 			// what if there were no listeners ready when updates happened