Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4ae654b

Browse files
committed
Update the retry based on feedback (ttls -> ttl, add options to documentation and normalises the default values)
1 parent 4ef6a9a commit 4ae654b

File tree

4 files changed

+58
-22
lines changed

4 files changed

+58
-22
lines changed

src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpReceiverTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function testItPublishesTheMessageForRetryIfSuchConfiguration()
116116

117117
$connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
118118
$connection->method('get')->willReturn($envelope);
119-
$connection->method('getConnectionCredentials')->willReturn(array('retry' => array('attempts' => 3)));
119+
$connection->method('getConnectionConfiguration')->willReturn(array('retry' => array('attempts' => 3)));
120120
$connection->method('publishForRetry')->with($envelope)->willReturn(true);
121121

122122
$connection->expects($this->once())->method('ack')->with($envelope);
@@ -145,7 +145,7 @@ public function testItThrowsTheExceptionIfTheRetryPublishDidNotWork()
145145

146146
$connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
147147
$connection->method('get')->willReturn($envelope);
148-
$connection->method('getConnectionCredentials')->willReturn(array('retry' => array('attempts' => 3)));
148+
$connection->method('getConnectionConfiguration')->willReturn(array('retry' => array('attempts' => 3)));
149149
$connection->method('publishForRetry')->with($envelope)->willReturn(false);
150150

151151
$connection->expects($this->never())->method('ack')->with($envelope);

src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/ConnectionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public function testItRetriesTheMessage()
214214

215215
$retryQueue->expects($this->once())->method('setName')->with('retry_queue_1');
216216
$retryQueue->expects($this->once())->method('setArguments')->with(array(
217-
'x-message-ttl' => 30000,
217+
'x-message-ttl' => 10000,
218218
'x-dead-letter-exchange' => 'messages',
219219
));
220220

@@ -258,7 +258,7 @@ public function testItRetriesTheMessageWithADifferentRoutingKeyAndTTLs()
258258
'retry' => array(
259259
'attempts' => 3,
260260
'dead_routing_key' => 'my_dead_routing_key',
261-
'ttls' => array(30000, 60000, 120000),
261+
'ttl' => array(30000, 60000, 120000),
262262
),
263263
);
264264

src/Symfony/Component/Messenger/Transport/AmqpExt/AmqpReceiver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function receive(callable $handler): void
4343
if (null === $AMQPEnvelope) {
4444
$handler(null);
4545

46-
usleep($this->connection->getConnectionCredentials()['loop_sleep'] ?? 200000);
46+
usleep($this->connection->getConnectionConfiguration()['loop_sleep'] ?? 200000);
4747
if (\function_exists('pcntl_signal_dispatch')) {
4848
pcntl_signal_dispatch();
4949
}

src/Symfony/Component/Messenger/Transport/AmqpExt/Connection.php

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
class Connection
2020
{
21-
private $connectionCredentials;
21+
private $connectionConfiguration;
2222
private $exchangeConfiguration;
2323
private $queueConfiguration;
2424
private $debug;
@@ -44,9 +44,45 @@ class Connection
4444
*/
4545
private $amqpRetryExchange;
4646

47-
public function __construct(array $connectionCredentials, array $exchangeConfiguration, array $queueConfiguration, bool $debug = false, AmqpFactory $amqpFactory = null)
47+
/**
48+
* Available options:
49+
*
50+
* * host: Hostname of the AMQP service
51+
* * port: Port of the AMQP service
52+
* * vhost: Virtual Host to use with the AMQP service
53+
* * user: Username to use to connect the the AMQP service
54+
* * password: Username to use the connect to the AMQP service
55+
* * queue:
56+
* * name: Name of the queue
57+
* * routing_key: The routing key (if any) to use to push the messages to
58+
* * flags: Queue flags (Default: AMQP_DURABLE)
59+
* * arguments: Extra arguments
60+
* * exchange:
61+
* * name: Name of the exchange
62+
* * type: Type of exchange (Default: fanout)
63+
* * flags: Exchange flags (Default: AMQP_DURABLE)
64+
* * arguments: Extra arguments
65+
* * retry:
66+
* * attempts: Number of times it will try to retry
67+
* * routing_key_pattern: The pattern of the routing key (Default: "attempt_%attempt%")
68+
* * dead_queue: Name of the queue in which messages that retry more than attempts time are pushed to
69+
* * dead_routing_key: Routing key name for the dead queue (Default: "dead")
70+
* * queue_name_pattern: Pattern to use to create the queues (Default: "retry_queue_%attempt%")
71+
* * exchange_name: Name of the exchange to be used for the retried messages (Default: "retry")
72+
* * ttl: Key-value pairs of attempt number -> seconds to wait. If not configured, 10 seconds will be waited each attempt.
73+
* * auto-setup: Enable or not the auto-setup of queues and exchanges (Default: true)
74+
* * loop_sleep: Amount of micro-seconds to wait if no message are available (Default: 200000)
75+
*/
76+
public function __construct(array $connectionConfiguration, array $exchangeConfiguration, array $queueConfiguration, bool $debug = false, AmqpFactory $amqpFactory = null)
4877
{
49-
$this->connectionCredentials = $connectionCredentials;
78+
$this->connectionConfiguration = array_replace_recursive(array(
79+
'retry' => array(
80+
'routing_key_pattern' => 'attempt_%attempt%',
81+
'dead_routing_key' => 'dead',
82+
'exchange_name' => 'retry',
83+
'queue_name_pattern' => 'retry_queue_%attempt%',
84+
),
85+
), $connectionConfiguration);
5086
$this->debug = $debug;
5187
$this->exchangeConfiguration = $exchangeConfiguration;
5288
$this->queueConfiguration = $queueConfiguration;
@@ -111,26 +147,26 @@ public function publish(string $body, array $headers = array()): void
111147
*/
112148
public function publishForRetry(\AMQPEnvelope $message): bool
113149
{
114-
if (!isset($this->connectionCredentials['retry'])) {
150+
if (!isset($this->connectionConfiguration['retry'])) {
115151
return false;
116152
}
117153

118-
$retryConfiguration = $this->connectionCredentials['retry'];
154+
$retryConfiguration = $this->connectionConfiguration['retry'];
119155
$attemptNumber = ((int) $message->getHeader('symfony-messenger-attempts') ?: 0) + 1;
120156

121157
if ($this->shouldSetup()) {
122158
$this->setupRetry($retryConfiguration, $attemptNumber);
123159
}
124160

125161
$maximumAttempts = $retryConfiguration['attempts'] ?? 3;
126-
$routingKey = str_replace('%attempt%', $attemptNumber, $retryConfiguration['routing_key_pattern'] ?? 'attempt_%attempt%');
162+
$routingKey = str_replace('%attempt%', $attemptNumber, $retryConfiguration['routing_key_pattern']);
127163

128164
if ($attemptNumber > $maximumAttempts) {
129165
if (!isset($retryConfiguration['dead_queue'])) {
130166
return false;
131167
}
132168

133-
$routingKey = $retryConfiguration['dead_routing_key'] ?? 'dead';
169+
$routingKey = $retryConfiguration['dead_routing_key'];
134170
}
135171

136172
$this->retryExchange($retryConfiguration)->publish(
@@ -154,21 +190,21 @@ private function setupRetry(array $retryConfiguration, int $attemptNumber)
154190

155191
$queue = $this->retryQueue($retryConfiguration, $attemptNumber);
156192
$queue->declareQueue();
157-
$queue->bind($exchange->getName(), str_replace('%attempt%', $attemptNumber, $retryConfiguration['routing_key_pattern'] ?? 'attempt_%attempt%'));
193+
$queue->bind($exchange->getName(), str_replace('%attempt%', $attemptNumber, $retryConfiguration['routing_key_pattern']));
158194

159195
if (isset($retryConfiguration['dead_queue'])) {
160196
$queue = $this->amqpFactory->createQueue($this->channel());
161197
$queue->setName($retryConfiguration['dead_queue']);
162198
$queue->declareQueue();
163-
$queue->bind($exchange->getName(), $retryConfiguration['dead_routing_key'] ?? 'dead');
199+
$queue->bind($exchange->getName(), $retryConfiguration['dead_routing_key']);
164200
}
165201
}
166202

167203
private function retryExchange(array $retryConfiguration)
168204
{
169205
if (null === $this->amqpRetryExchange) {
170206
$this->amqpRetryExchange = $this->amqpFactory->createExchange($this->channel());
171-
$this->amqpRetryExchange->setName($retryConfiguration['name'] ?? 'retry');
207+
$this->amqpRetryExchange->setName($retryConfiguration['exchange_name']);
172208
$this->amqpRetryExchange->setType(AMQP_EX_TYPE_DIRECT);
173209
}
174210

@@ -178,9 +214,9 @@ private function retryExchange(array $retryConfiguration)
178214
private function retryQueue(array $retryConfiguration, int $attemptNumber)
179215
{
180216
$queue = $this->amqpFactory->createQueue($this->channel());
181-
$queue->setName(str_replace('%attempt%', $attemptNumber, $retryConfiguration['queue_name_pattern'] ?? 'retry_queue_%attempt%'));
217+
$queue->setName(str_replace('%attempt%', $attemptNumber, $retryConfiguration['queue_name_pattern']));
182218
$queue->setArguments(array(
183-
'x-message-ttl' => $retryConfiguration['ttls'][$attemptNumber - 1] ?? 30000, // 30 seconds by default
219+
'x-message-ttl' => $retryConfiguration['ttl'][$attemptNumber - 1] ?? 10000, // 10 seconds by default
184220
'x-dead-letter-exchange' => $this->exchange()->getName(),
185221
));
186222

@@ -250,8 +286,8 @@ public function setup(): void
250286
public function channel(): \AMQPChannel
251287
{
252288
if (null === $this->amqpChannel) {
253-
$connection = $this->amqpFactory->createConnection($this->connectionCredentials);
254-
$connectMethod = 'true' === ($this->connectionCredentials['persistent'] ?? 'false') ? 'pconnect' : 'connect';
289+
$connection = $this->amqpFactory->createConnection($this->connectionConfiguration);
290+
$connectMethod = 'true' === ($this->connectionConfiguration['persistent'] ?? 'false') ? 'pconnect' : 'connect';
255291

256292
if (false === $connection->{$connectMethod}()) {
257293
throw new \AMQPException('Could not connect to the AMQP server. Please verify the provided DSN.');
@@ -294,9 +330,9 @@ public function exchange(): \AMQPExchange
294330
return $this->amqpExchange;
295331
}
296332

297-
public function getConnectionCredentials(): array
333+
public function getConnectionConfiguration(): array
298334
{
299-
return $this->connectionCredentials;
335+
return $this->connectionConfiguration;
300336
}
301337

302338
private function clear(): void
@@ -308,6 +344,6 @@ private function clear(): void
308344

309345
private function shouldSetup(): bool
310346
{
311-
return !array_key_exists('auto-setup', $this->connectionCredentials) || !in_array($this->connectionCredentials['auto-setup'], array(false, 'false'), true);
347+
return !array_key_exists('auto-setup', $this->connectionConfiguration) || !in_array($this->connectionConfiguration['auto-setup'], array(false, 'false'), true);
312348
}
313349
}

0 commit comments

Comments
 (0)