Skip to content
Closed
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
use PHPyh\CodingStandard\PhpCsFixerCodingStandard;

$config = (new Config())
$config = new Config()
Copy link
Collaborator

Choose a reason for hiding this comment

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

We on php 8.3 now.

->setFinder(
Finder::create()
->in(__DIR__ . '/src')
Expand All @@ -20,7 +20,7 @@
->setParallelConfig(ParallelConfigFactory::detect())
->setCacheFile(__DIR__ . '/var/' . basename(__FILE__) . '.cache');

(new PhpCsFixerCodingStandard())->applyTo($config, [
new PhpCsFixerCodingStandard()->applyTo($config, [
'numeric_literal_separator' => false,
]);

Expand Down
9 changes: 9 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ services:
ports:
- "127.0.0.1:5672:5672"
- "127.0.0.1:15672:15672"

rabbitmq_low_memory:
image: rabbitmq:4.0-management-alpine
restart: always
ports:
- "127.0.0.1:5673:5672"
- "127.0.0.1:15673:15672"
volumes:
- ./docker/rabbitmq/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf
1 change: 1 addition & 0 deletions docker/rabbitmq/rabbitmq.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vm_memory_high_watermark.absolute = 130MiB
4 changes: 4 additions & 0 deletions src/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public function publish(
bool $mandatory = false,
bool $immediate = false,
): ?PublishConfirmation {
$this->connection->ensureNotBlocked();

/** @var ?PublishConfirmation $confirmation */
$confirmation = null;

Expand All @@ -110,6 +112,8 @@ public function publish(
*/
public function publishBatch(array $publishMessages): PublishBatchConfirmation
{
$this->connection->ensureNotBlocked();

/** @var list<PublishConfirmation> $confirmations */
$confirmations = [];

Expand Down
23 changes: 23 additions & 0 deletions src/Exception/ConnectionIsBlocked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Thesis\Amqp\Exception;

use Thesis\Amqp\AmqpException;

/**
* @api
*/
final class ConnectionIsBlocked extends \RuntimeException implements AmqpException
{
public function __construct(
public readonly string $reason,
?\Throwable $previous = null,
) {
parent::__construct(
message: 'Connection is blocked: ' . $reason,
previous: $previous,
);
}
}
36 changes: 35 additions & 1 deletion src/Internal/Io/AmqpConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Amp\Socket\Socket;
use Revolt\EventLoop;
use Thesis\AmpBridge\ReaderWriter as AmpReaderWriter;
use Thesis\Amqp\Exception\ConnectionIsBlocked;
use Thesis\Amqp\Exception\UnexpectedFrameReceived;
use Thesis\Amqp\Internal\Hooks;
use Thesis\Amqp\Internal\Protocol;
Expand All @@ -29,10 +30,16 @@ final class AmqpConnection implements Writer

private float $lastWrite = 0;

/**
* @phpstan-ignore property.unusedType
*/
private ?string $blockedReason = null;

private bool $closed = false;

public function __construct(
private readonly Socket $socket,
private readonly Hooks $hooks,
) {
$this->buffer = Buffer::empty();
$this->reader = new Protocol\Reader(
Expand Down Expand Up @@ -91,8 +98,28 @@ public function write(string $bytes): void
$this->lastWrite = Amp\now();
}

public function ioLoop(Hooks $hooks): void
public function setup(): void
{
$hooks = $this->hooks;

$blockedReason = &$this->blockedReason;

$hooks->anyOf(
0,
Protocol\Frame\ConnectionBlocked::class,
static function (Protocol\Frame\ConnectionBlocked $blocked) use (&$blockedReason): void {
$blockedReason = $blocked->reason;
},
);

$hooks->anyOf(
0,
Protocol\Frame\ConnectionUnblocked::class,
static function () use (&$blockedReason): void {
$blockedReason = null;
},
);

$reader = $this->reader;
$isClosed = &$this->closed;

Expand Down Expand Up @@ -127,6 +154,13 @@ public function heartbeat(int $interval): void
});
}

public function ensureNotBlocked(): void
{
if ($this->blockedReason !== null) {
throw new ConnectionIsBlocked($this->blockedReason);
}
}

public function close(): void
{
if (!$this->socket->isClosed()) {
Expand Down
6 changes: 3 additions & 3 deletions src/Internal/Io/AmqpConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function connect(): AmqpConnection
Frame\ConnectionOpenOk::class,
);

$connection->ioLoop($this->hooks);
$connection->setup();

$this->hooks->anyOf(0, Frame\ConnectionClose::class, static function () use ($connection): void {
$connection->writeFrame(Protocol\Method::connectionCloseOk());
Expand Down Expand Up @@ -93,7 +93,7 @@ private function createConnection(): AmqpConnection

foreach ($this->config->connectionUrls() as $url) {
try {
return new AmqpConnection($this->createSocket($url));
return new AmqpConnection($this->createSocket($url), $this->hooks);
} catch (\Throwable $e) {
$exceptions[] = "{$url}: {$e->getMessage()}";
}
Expand All @@ -109,7 +109,7 @@ private function createConnection(): AmqpConnection
*/
private function createSocket(string $url): Socket\Socket
{
$context = (new Socket\ConnectContext())
$context = new Socket\ConnectContext()
->withConnectTimeout($this->config->connectionTimeout);

if ($this->config->tcpNoDelay) {
Expand Down
28 changes: 28 additions & 0 deletions src/Internal/Protocol/Frame/ConnectionBlocked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Thesis\Amqp\Internal\Protocol\Frame;

use Thesis\Amqp\Internal\Io;
use Thesis\Amqp\Internal\Protocol\Frame;

/**
* @internal
*/
final readonly class ConnectionBlocked implements Frame
{
public function __construct(
public string $reason,
) {}

public static function read(Io\ReadBytes $reader): self
{
return new self($reader->readString());
}

public function write(Io\WriteBytes $writer): void
{
$writer->writeString($this->reason);
}
}
23 changes: 23 additions & 0 deletions src/Internal/Protocol/Frame/ConnectionUnblocked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Thesis\Amqp\Internal\Protocol\Frame;

use Thesis\Amqp\Internal\Io;
use Thesis\Amqp\Internal\Protocol\Frame;

/**
* @internal
*/
enum ConnectionUnblocked implements Frame
{
case frame;

public static function read(Io\ReadBytes $reader): self
{
return self::frame;
}

public function write(Io\WriteBytes $writer): void {}
}
2 changes: 2 additions & 0 deletions src/Internal/Protocol/Protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ enum Protocol
ClassMethod::CONNECTION_OPEN_OK => Frame\ConnectionOpenOk::class,
ClassMethod::CONNECTION_CLOSE => Frame\ConnectionClose::class,
ClassMethod::CONNECTION_CLOSE_OK => Frame\ConnectionCloseOk::class,
ClassMethod::CONNECTION_BLOCKED => Frame\ConnectionBlocked::class,
ClassMethod::CONNECTION_UNBLOCKED => Frame\ConnectionUnblocked::class,
],
ClassType::CHANNEL => [
ClassMethod::CHANNEL_OPEN_OK => Frame\ChannelOpenOkFrame::class,
Expand Down
Loading