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

Skip to content

[Messenger] [amqp-messenger] When heartbeat expired, disconnect to force a reconnect #47574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class Connection
*/
private $amqpDelayExchange;

/** @var int */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

our CS is to put these on many lines IIRC

private $lastActivityTime = 0;

public function __construct(array $connectionOptions, array $exchangeOptions, array $queuesOptions, AmqpFactory $amqpFactory = null)
{
if (!\extension_loaded('amqp')) {
Expand Down Expand Up @@ -347,6 +350,8 @@ private function publishOnExchange(\AMQPExchange $exchange, string $body, string
$attributes['delivery_mode'] = $attributes['delivery_mode'] ?? 2;
$attributes['timestamp'] = $attributes['timestamp'] ?? time();

$this->lastActivityTime = time();

$exchange->publish(
$body,
$routingKey,
Expand Down Expand Up @@ -510,6 +515,11 @@ static function (): bool {
}
);
}

$this->lastActivityTime = time();
} elseif (!empty($this->connectionOptions['heartbeat']) && time() > $this->lastActivityTime + ($this->connectionOptions['heartbeat'] * 2)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} elseif (0 < ($this->connectionOptions['heartbeat'] ?? 0) && time() > $this->lastActivityTime + 2 * $this->connectionOptions['heartbeat']) {

$disconnectMethod = 'true' === ($this->connectionOptions['persistent'] ?? 'false') ? 'pdisconnect' : 'disconnect';
$this->amqpChannel->getConnection()->{$disconnectMethod}();
}

return $this->amqpChannel;
Expand Down