diff --git a/src/MessageBus/Handling/Handlers.php b/src/MessageBus/Handling/Handlers.php index b55ba5f..9aacd48 100644 --- a/src/MessageBus/Handling/Handlers.php +++ b/src/MessageBus/Handling/Handlers.php @@ -54,7 +54,6 @@ public function with(string $messageClass, callable $handler, string $qualifier } $copy = clone $this; - /** @phpstan-ignore assign.propertyType */ $copy->handlers[$messageClass][$qualifier] = $handler; return $copy; diff --git a/src/MessageBus/Pgmq/PgmqTransport.php b/src/MessageBus/Pgmq/PgmqTransport.php index 66bdca6..49a6fdd 100644 --- a/src/MessageBus/Pgmq/PgmqTransport.php +++ b/src/MessageBus/Pgmq/PgmqTransport.php @@ -60,7 +60,61 @@ public function createQueue(string $name): void Pgmq\createExtension($this->postgres); Pgmq\createQueue($this->postgres, $name); + // Consumers are woken up by pgmq_dispatch_v1() itself (see below), not by pgmq's insert trigger. The trigger + // updates the queue's row in pgmq.notify_insert_throttle on every insert, and that row lock is held until the + // sender commits: concurrent senders to one queue wait for each other, and two transactions sending to two + // queues in opposite order deadlock. Undo an earlier setup, without stalling a live queue: + // + // * the throttle row goes first: without it the trigger updates nothing and locks nothing. Deleting it waits + // only for senders holding that row, not for the whole table; + // * the trigger itself is dropped with a short lock_timeout: DROP TRIGGER takes an ACCESS EXCLUSIVE lock that + // waits for every open transaction on the queue and blocks its readers and senders meanwhile. If the queue is + // busy, it is skipped with a warning; the trigger is harmless without its row, and the next setup retries. + // + // Both steps only read the catalog when there is nothing to undo, so repeated setups run no DDL. + $this->postgres->query(\sprintf( + <<<'SQL' + DO $undo$ + DECLARE + v_queue text := %s; + v_lock_timeout text := current_setting('lock_timeout'); + BEGIN + PERFORM set_config('lock_timeout', '2s', true); + + BEGIN + DELETE FROM pgmq.notify_insert_throttle WHERE queue_name = v_queue; + EXCEPTION WHEN lock_not_available THEN + RAISE WARNING 'pgmq queue "%%" is busy: its insert-notify throttle row is left for the next setup', v_queue; + END; + + IF EXISTS ( + SELECT 1 + FROM pg_trigger tg + JOIN pg_class c ON c.oid = tg.tgrelid + JOIN pg_namespace n ON n.oid = c.relnamespace + WHERE n.nspname = 'pgmq' + AND c.relname = pgmq.format_table_name(v_queue, 'q') + AND tg.tgname = 'trigger_notify_queue_insert_listeners' + ) THEN + BEGIN + PERFORM pgmq.disable_notify_insert(v_queue); + EXCEPTION WHEN lock_not_available THEN + RAISE WARNING 'pgmq queue "%%" is busy: its insert-notify trigger is left for the next setup', v_queue; + END; + END IF; + + PERFORM set_config('lock_timeout', v_lock_timeout, true); + END + $undo$ + SQL, + $this->postgres->quoteLiteral($name), + )); + $this->postgres->query('create schema if not exists thesis_message_bus;'); + // Wakes consumers with pg_notify() on the channel pgmq's insert trigger used, so listeners need no change. + // No row is locked: a notification is delivered on commit (never for a rolled-back send), and identical + // notifications of one transaction are folded into one — a natural throttle per queue and transaction. + // Delayed messages are not announced: they are invisible until the delay passes, and the poll picks them up. $this->postgres->query( <<<'SQL' create or replace function thesis_message_bus.pgmq_dispatch_v1( @@ -75,12 +129,28 @@ public function createQueue(string $name): void as $$ declare index integer; + target text; begin for index in 1..coalesce(array_length(operations, 1), 0) loop if operations[index] = 'send' then return query select id from pgmq.send(addresses[index], payloads[index], headers[index], delays[index]) as id; + + if delays[index] <= 0 then + perform pg_notify('pgmq.' || pgmq.format_table_name(addresses[index], 'q') || '.INSERT', null); + end if; elsif operations[index] = 'publish' then return next pgmq.send_topic(addresses[index], payloads[index], headers[index], delays[index])::bigint; + + if delays[index] <= 0 then + -- The same queues pgmq.send_topic() has just sent to. + for target in + select distinct tb.queue_name + from pgmq.topic_bindings tb + where addresses[index] ~ tb.compiled_regex + loop + perform pg_notify('pgmq.' || pgmq.format_table_name(target, 'q') || '.INSERT', null); + end loop; + end if; else raise exception 'unknown message bus operation: %', operations[index]; end if; @@ -189,7 +259,9 @@ public function startConsumer(string $queue, ConsumerHandler $handler): PgmqCons $timeoutWatcher, new ChannelWatcher( $polls, - $this->postgres->listen($queue->enableNotifyInsert()), + // pgmq_dispatch_v1() notifies this channel; see createQueue() for why the insert trigger is not used. Lower case: + // the SQL side names the channel after the queue table, and pgmq lower-cases table names. + $this->postgres->listen(Pgmq\channelName(strtolower($queue->name))), $timeoutWatcher, ), ]);