Browse Source

backend: use postgresql LISTEN/NOTIFY for new message checking

Andrew Dolgov 11 months ago
parent
commit
7fcd093581

+ 2 - 6
backend.php

@@ -291,12 +291,8 @@
 		@$uniqid = $_REQUEST["uniqid"];
 
 		if (!$init) {
-			$sleep_start = time();
-			while (time() - $sleep_start < UPDATE_DELAY_MAX &&
-					!num_new_lines($last_id, $bufsize)) {
-
-				sleep(1);
-			}
+			$dbh->exec("LISTEN ttirc_messages");
+			$res = $dbh->pgsqlGetNotify(PDO::FETCH_ASSOC, UPDATE_DELAY_MAX * 1000);
 		}
 
 		$lines = get_new_lines($last_id, $bufsize);

+ 1 - 1
daemon/src/org/fox/ttirc/Master.java

@@ -16,7 +16,7 @@ public class Master {
 	
 	public static enum DbType { MYSQL, PGSQL };
 	
-	private final int m_schemaVersion = 15;
+	private final int m_schemaVersion = 16;
 	
 	private String m_dbKeyParam;
 	

+ 1 - 1
include/sanity_check.php

@@ -1,7 +1,7 @@
 <?php
 	require_once "functions.php";
 
-	define('SCHEMA_VERSION', 15);
+	define('SCHEMA_VERSION', 16);
 	define('EXPECTED_CONFIG_VERSION', 2);
 
 	$err_msg = "";

+ 12 - 0
schema/ttirc_schema_pgsql.sql

@@ -15,6 +15,7 @@ drop table ttirc_sessions;
 drop table ttirc_version;
 drop table ttirc_system;
 drop function SUBSTRING_FOR_DATE(timestamp, int, int);
+DROP TRIGGER IF EXISTS ttirc_new_message_trigger ON ttirc_messages;
 
 begin;
 
@@ -22,6 +23,14 @@ create table ttirc_system(id serial not null primary key,
 	key varchar(120) not null,
 	value text);
 
+CREATE OR REPLACE FUNCTION ttirc_notify_new_message() RETURNS trigger as $$
+  DECLARE
+  BEGIN
+    PERFORM pg_notify('ttirc_messages', 'NEW_MESSAGE');
+    RETURN new;
+  END;
+$$ language plpgsql;
+
 insert into ttirc_system (key, value) values ('MASTER_RUNNING', '');
 insert into ttirc_system (key, value) values ('MASTER_HEARTBEAT', '');
 
@@ -90,6 +99,9 @@ create table ttirc_messages(id serial not null primary key,
 	userhost varchar(120),
 	connection_id integer not null references ttirc_connections(id) ON DELETE CASCADE);
 
+CREATE TRIGGER ttirc_new_message_trigger AFTER INSERT ON ttirc_messages
+ 	EXECUTE PROCEDURE ttirc_notify_new_message();
+
 create index ttirc_messages_incoming on ttirc_messages(incoming);
 
 create table ttirc_prefs_types (id integer not null primary key,

+ 11 - 0
schema/versions/pgsql/16.sql

@@ -0,0 +1,11 @@
+begin;
+
+CREATE OR REPLACE FUNCTION ttirc_notify_new_message() RETURNS trigger as $$ DECLARE BEGIN  PERFORM pg_notify('ttirc_messages', 'NEW_MESSAGE'); RETURN new; END; $$ language plpgsql;
+
+CREATE TRIGGER ttirc_new_message_trigger AFTER INSERT ON ttirc_messages EXECUTE PROCEDURE ttirc_notify_new_message();
+
+update ttirc_version set schema_version = 16;
+
+commit;
+
+