8000 [Messenger] Only send UNLISTEN query if we are actively listening · symfony/symfony@5171958 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5171958

Browse files
jwagenicolas-grekas
authored andcommitted
[Messenger] Only send UNLISTEN query if we are actively listening
1 parent 293e066 commit 5171958

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/PostgreSqlConnectionTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,13 @@ public function countNotifyCalls()
119119
$connection->get();
120120
$connection->get();
121121

122+
$this->assertTrue($connection->isListening());
123+
122124
$this->assertSame(2, $wrappedConnection->countNotifyCalls());
125+
126+
$connection->__destruct();
127+
128+
$this->assertFalse($connection->isListening());
123129
}
124130

125131
public function testGetExtraSetupSqlWrongTable()
@@ -132,4 +138,12 @@ public function testGetExtraSetupSqlWrongTable()
132138
// don't set the _symfony_messenger_table_name option
133139
$this->assertSame([], $connection->getExtraSetupSqlForTable($table));
134140
}
141+
142+
public function testIsListeningReturnsFalseWhenGetHasNotBeenCalled()
143+
{
144+
$driverConnection = $this->createStub(\Doctrine\DBAL\Connection::class);
145+
$connection = new PostgreSqlConnection(['table_name' => 'queue_table'], $driverConnection);
146+
147+
$this->assertFalse($connection->isListening());
148+
}
135149
}

src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/PostgreSqlConnection.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323
final class PostgreSqlConnection extends Connection
2424
{
25+
private bool $listening = false;
26+
2527
/**
2628
* * check_delayed_interval: The interval to check for delayed messages, in milliseconds. Set to 0 to disable checks. Default: 60000 (1 minute)
2729
* * get_notify_timeout: The length of time to wait for a response when calling PDO::pgsqlGetNotify (or Pdo\Pgsql::getNotify on PHP 8.4+), in milliseconds. Default: 0.
@@ -46,6 +48,11 @@ public function __destruct()
4648
$this->unlisten();
4749
}
4850

51+
public function isListening(): bool
52+
{
53+
return $this->listening;
54+
}
55+
4956
public function reset(): void
5057
{
5158
parent::reset();
@@ -62,6 +69,8 @@ public function get(): ?array
6269
// https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
6370
$this->executeStatement(\sprintf('LISTEN "%s"', $this->configuration['table_name']));
6471

72+
$this->listening = true;
73+
6574
// The condition should be removed once support for DBAL <3.3 is dropped
6675
if (method_exists($this->driverConnection, 'getNativeConnection')) {
6776
$wrappedConnection = $this->driverConnection->getNativeConnection();
@@ -91,6 +100,11 @@ public function get(): ?array
91100

92101
private function unlisten(): void
93102
{
103+
if (!$this->listening) {
104+
return;
105+
}
106+
94107
$this->executeStatement(\sprintf('UNLISTEN "%s"', $this->configuration['table_name']));
108+
$this->listening = false;
95109
}
96110
}

0 commit comments

Comments
 (0)
0