Skip to content

Commit d3004ed

Browse files
appkit-frameworkWyriHaximus
authored andcommitted
Handle network level disconnect
1 parent 7c69330 commit d3004ed

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

spec/generate.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,13 @@ function amqpTypeToLength(string $type, string $e): array
283283
$connectionContent .= " private readonly Configuration \$configuration,\n";
284284
$connectionContent .= " private readonly Closure \$frameMax,\n";
285285
$connectionContent .= " ) {\n";
286+
$connectionContent .= " \$this->connection->on('close', function (): void {\n";
287+
$connectionContent .= " if (!\$this->client->isConnected()) {\n";
288+
$connectionContent .= " return;\n";
289+
$connectionContent .= " }\n";
290+
$connectionContent .= "\n";
291+
$connectionContent .= " \$this->client->disconnect(0, 'Connection lost', ClientInterface::RAW_CONNECTION_INACTIVE);\n";
292+
$connectionContent .= " });\n";
286293
$connectionContent .= " \$this->connection->on('data', function (string \$data): void {\n";
287294
$connectionContent .= " \$this->readBuffer->append(\$data);\n";
288295
$connectionContent .= "\n";

src/Connection.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ public function __construct(
6767
private readonly Configuration $configuration,
6868
private readonly Closure $frameMax,
6969
) {
70+
$this->connection->on('close', function (): void {
71+
if (!$this->client->isConnected()) {
72+
return;
73+
}
74+
75+
$this->client->disconnect(0, 'Connection lost', ClientInterface::RAW_CONNECTION_INACTIVE);
76+
});
7077
$this->connection->on('data', function (string $data): void {
7178
$this->readBuffer->append($data);
7279

test/ConnectionTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,52 @@ static function (): int {
189189

190190
self::assertSame($baseBuffer, $afterCloseBuffer);
191191
}
192+
193+
public function testConnectingEmittingCloseWillResultInFastClosure(): void
194+
{
195+
$client = new MockClientInterface(true);
196+
$mockConnection = new MockConnectionInterface();
197+
new Connection(
198+
$client,
199+
$mockConnection,
200+
new Buffer(),
201+
new Buffer(),
202+
new ProtocolReader(),
203+
new ProtocolWriter(),
204+
new Channels(),
205+
new Configuration(),
206+
static function (): int {
207+
return Constants::FRAME_MAX;
208+
},
209+
);
210+
211+
$mockConnection->emit('close');
212+
213+
self::assertSame(1, $client->getIsConnectedCount());
214+
self::assertSame(1, $client->getDisconnectCount());
215+
}
216+
217+
public function testConnectingEmittingCloseWillResultInFastClosureButNotWhenNotConnected(): void
218+
{
219+
$client = new MockClientInterface(false);
220+
$mockConnection = new MockConnectionInterface();
221+
new Connection(
222+
$client,
223+
$mockConnection,
224+
new Buffer(),
225+
new Buffer(),
226+
new ProtocolReader(),
227+
new ProtocolWriter(),
228+
new Channels(),
229+
new Configuration(),
230+
static function (): int {
231+
return Constants::FRAME_MAX;
232+
},
233+
);
234+
235+
$mockConnection->emit('close');
236+
237+
self::assertSame(1, $client->getIsConnectedCount());
238+
self::assertSame(0, $client->getDisconnectCount());
239+
}
192240
}

test/MockClientInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
final class MockClientInterface implements ClientInterface
1111
{
12+
private int $isConnectedCount = 0;
1213
private int $disconnectCount = 0;
1314
private bool $isConnected;
1415

@@ -29,9 +30,16 @@ public function disconnect(int $replyCode = 0, string $replyText = '', bool $con
2930

3031
public function isConnected(): bool
3132
{
33+
$this->isConnectedCount++;
34+
3235
return $this->isConnected;
3336
}
3437

38+
public function getIsConnectedCount(): int
39+
{
40+
return $this->isConnectedCount;
41+
}
42+
3543
public function getDisconnectCount(): int
3644
{
3745
return $this->disconnectCount;

0 commit comments

Comments
 (0)