Skip to content

Commit 8775f97

Browse files
GurvanVgxsurfingmig
authored andcommitted
[Messenger] Fix amqp socket lost
Co-authored-by: Miguel Fernandez <[email protected]>
1 parent def93f2 commit 8775f97

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

Transport/Connection.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ class Connection
102102
*/
103103
private $amqpDelayExchange;
104104

105+
/**
106+
* @var int
107+
*/
108+
private $lastActivityTime = 0;
109+
105110
public function __construct(array $connectionOptions, array $exchangeOptions, array $queuesOptions, AmqpFactory $amqpFactory = null)
106111
{
107112
if (!\extension_loaded('amqp')) {
@@ -347,6 +352,8 @@ private function publishOnExchange(\AMQPExchange $exchange, string $body, string
347352
$attributes['delivery_mode'] = $attributes['delivery_mode'] ?? 2;
348353
$attributes['timestamp'] = $attributes['timestamp'] ?? time();
349354

355+
$this->lastActivityTime = time();
356+
350357
$exchange->publish(
351358
$body,
352359
$routingKey,
@@ -510,6 +517,11 @@ static function (): bool {
510517
}
511518
);
512519
}
520+
521+
$this->lastActivityTime = time();
522+
} elseif (0 < ($this->connectionOptions['heartbeat'] ?? 0) && time() > $this->lastActivityTime + 2 * $this->connectionOptions['heartbeat']) {
523+
$disconnectMethod = 'true' === ($this->connectionOptions['persistent'] ?? 'false') ? 'pdisconnect' : 'disconnect';
524+
$this->amqpChannel->getConnection()->{$disconnectMethod}();
513525
}
514526

515527
return $this->amqpChannel;

0 commit comments

Comments
 (0)