Skip to content

Commit 25601f3

Browse files
authored
Merge pull request #1159 from mordilion/hotfix/wait-between-subsciption-requests
add subscription_interval as config for dbal subscription consumer
2 parents 9b1f0c3 + bc99baa commit 25601f3

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

Diff for: pkg/dbal/DbalContext.php

+7-10
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,21 @@ class DbalContext implements Context
3939
* Callable must return instance of Doctrine\DBAL\Connection once called.
4040
*
4141
* @param Connection|callable $connection
42-
* @param array $config
4342
*/
4443
public function __construct($connection, array $config = [])
4544
{
4645
$this->config = array_replace([
4746
'table_name' => 'enqueue',
4847
'polling_interval' => null,
48+
'subscription_polling_interval' => null,
4949
], $config);
5050

5151
if ($connection instanceof Connection) {
5252
$this->connection = $connection;
5353
} elseif (is_callable($connection)) {
5454
$this->connectionFactory = $connection;
5555
} else {
56-
throw new \InvalidArgumentException(sprintf(
57-
'The connection argument must be either %s or callable that returns %s.',
58-
Connection::class,
59-
Connection::class
60-
));
56+
throw new \InvalidArgumentException(sprintf('The connection argument must be either %s or callable that returns %s.', Connection::class, Connection::class));
6157
}
6258
}
6359

@@ -135,6 +131,10 @@ public function createSubscriptionConsumer(): SubscriptionConsumer
135131
$consumer->setRedeliveryDelay($this->config['redelivery_delay']);
136132
}
137133

134+
if (isset($this->config['subscription_polling_interval'])) {
135+
$consumer->setPollingInterval($this->config['subscription_polling_interval']);
136+
}
137+
138138
return $consumer;
139139
}
140140

@@ -202,10 +202,7 @@ public function getDbalConnection(): Connection
202202
if (false == $this->connection) {
203203
$connection = call_user_func($this->connectionFactory);
204204
if (false == $connection instanceof Connection) {
205-
throw new \LogicException(sprintf(
206-
'The factory must return instance of Doctrine\DBAL\Connection. It returns %s',
207-
is_object($connection) ? get_class($connection) : gettype($connection)
208-
));
205+
throw new \LogicException(sprintf('The factory must return instance of Doctrine\DBAL\Connection. It returns %s', is_object($connection) ? get_class($connection) : gettype($connection)));
209206
}
210207

211208
$this->connection = $connection;

Diff for: pkg/dbal/DbalSubscriptionConsumer.php

+19-3
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ class DbalSubscriptionConsumer implements SubscriptionConsumer
3737
private $redeliveryDelay;
3838

3939
/**
40-
* @param DbalContext $context
40+
* Time to wait between subscription requests in milliseconds.
41+
*
42+
* @var int
4143
*/
44+
private $pollingInterval = 200;
45+
4246
public function __construct(DbalContext $context)
4347
{
4448
$this->context = $context;
@@ -63,6 +67,18 @@ public function setRedeliveryDelay(int $redeliveryDelay): self
6367
return $this;
6468
}
6569

70+
public function getPollingInterval(): int
71+
{
72+
return $this->pollingInterval;
73+
}
74+
75+
public function setPollingInterval(int $msec): self
76+
{
77+
$this->pollingInterval = $msec;
78+
79+
return $this;
80+
}
81+
6682
public function consume(int $timeout = 0): void
6783
{
6884
if (empty($this->subscribers)) {
@@ -92,7 +108,7 @@ public function consume(int $timeout = 0): void
92108
* @var DbalConsumer
93109
* @var callable $callback
94110
*/
95-
list($consumer, $callback) = $this->subscribers[$message->getQueue()];
111+
[$consumer, $callback] = $this->subscribers[$message->getQueue()];
96112

97113
if (false === call_user_func($callback, $message, $consumer)) {
98114
return;
@@ -102,7 +118,7 @@ public function consume(int $timeout = 0): void
102118
} else {
103119
$currentQueueNames = [];
104120

105-
usleep(200000); // 200ms
121+
usleep($this->getPollingInterval() * 1000);
106122
}
107123

108124
if ($timeout && microtime(true) >= $now + $timeout) {

Diff for: pkg/dbal/Tests/DbalContextTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function testCouldBeConstructedWithEmptyConfiguration()
3939
$this->assertAttributeEquals([
4040
'table_name' => 'enqueue',
4141
'polling_interval' => null,
42+
'subscription_polling_interval' => null,
4243
], 'config', $factory);
4344
}
4445

@@ -47,11 +48,13 @@ public function testCouldBeConstructedWithCustomConfiguration()
4748
$factory = new DbalContext($this->createConnectionMock(), [
4849
'table_name' => 'theTableName',
4950
'polling_interval' => 12345,
51+
'subscription_polling_interval' => 12345,
5052
]);
5153

5254
$this->assertAttributeEquals([
5355
'table_name' => 'theTableName',
5456
'polling_interval' => 12345,
57+
'subscription_polling_interval' => 12345,
5558
], 'config', $factory);
5659
}
5760

0 commit comments

Comments
 (0)