From 09c58f7faebac18a91df6f250d65939434a4ff08 Mon Sep 17 00:00:00 2001 From: chaz6chez Date: Wed, 22 Nov 2023 13:43:01 +0800 Subject: [PATCH] https://www.workerman.net/q/11912 --- src/Client.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Client.php b/src/Client.php index 81e41bf..63b7d0b 100644 --- a/src/Client.php +++ b/src/Client.php @@ -29,6 +29,7 @@ class Client extends \Bunny\Async\Client * "user" => "guest", * "password" => "guest", * "timeout" => 10, + * "restart_interval" => 0, * "heartbeat" => 60, * "heartbeat_callback" => function(){} * ] {@see AbstractClient::__construct()} and {@see \Workerman\RabbitMQ\Client::authResponse()} @@ -213,8 +214,20 @@ public function disconnect($replyCode = 0, $replyText = "") $this->eventLoop->del($this->getStream(), EventInterface::EV_READ); $this->closeStream(); $this->init(); - if($replyCode !== 0){ - Worker::stopAll(0,"RabbitMQ client disconnected: [{$replyCode}] {$replyText}"); + if ($replyCode !== 0) { + if (($restartInterval = $this->options['restart_interval'] ?? 0) > 0) { + Worker::log("RabbitMQ client will restart in $restartInterval seconds. "); + $this->eventLoop->add( + $restartInterval, + EventInterface::EV_TIMER_ONCE, + function () use ($replyCode, $replyText, $restartInterval) { + Worker::stopAll(0,"RabbitMQ client disconnected: [{$replyCode}] {$replyText}"); + } + ); + return null; + } else { + Worker::stopAll(0,"RabbitMQ client disconnected: [{$replyCode}] {$replyText}"); + } } return $this; });