Skip to content

Commit ae30026

Browse files
author
Radovan Kepák
authored
Add sync disconnect to the Bunny\Client so we can disconnect synced without Promises. The main reason for this is, that client itself is sync (async is in async/client.php) and if connection is lost during app, it will end fatal error with exception, that can`t be catch. This will put the disconnect back to the sync state, so no more promise and no mo trouble if connection is dropped. (#47)
1 parent 9ca3e87 commit ae30026

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Connection/Client.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
namespace Contributte\RabbitMQ\Connection;
66

77
use Bunny\Client as BunnyClient;
8+
use Bunny\ClientStateEnum;
89
use Bunny\Exception\BunnyException;
10+
use Bunny\Exception\ClientException;
911
use Bunny\Protocol\HeartbeatFrame;
1012

1113
class Client extends BunnyClient
@@ -17,4 +19,31 @@ public function sendHeartbeat(): void {
1719
$this->getWriter()->appendFrame(new HeartbeatFrame(), $this->writeBuffer);
1820
$this->flushWriteBuffer();
1921
}
22+
23+
public function syncDisconnect(): bool
24+
{
25+
try {
26+
if ($this->state !== ClientStateEnum::CONNECTED) {
27+
return false;
28+
}
29+
30+
$this->state = ClientStateEnum::DISCONNECTING;
31+
32+
foreach ($this->channels as $channel) {
33+
$channelId = $channel->getChannelId();
34+
35+
$this->channelClose($channelId, 0, '', 0, 0);
36+
$this->removeChannel($channelId);
37+
}
38+
39+
$this->connectionClose(0, '', 0, 0);
40+
$this->closeStream();
41+
} catch (ClientException $e) {
42+
// swallow, we do not care we are not connected, we want to close connection anyway
43+
}
44+
45+
$this->init();
46+
47+
return true;
48+
}
2049
}

src/Connection/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function __construct(
5656
public function __destruct()
5757
{
5858
if ($this->bunnyClient->isConnected()) {
59-
$this->bunnyClient->disconnect();
59+
$this->bunnyClient->syncDisconnect();
6060
}
6161
}
6262

0 commit comments

Comments
 (0)