From b3f3a4bc60b2ac9d1d6f41b2814cdda4b9d22a27 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Fri, 8 Aug 2025 16:13:31 +0200 Subject: [PATCH] use Period instead of ElapsedPeriod to express timeouts --- CHANGELOG.md | 2 ++ benchmark/client.php | 2 +- fixtures/forever-consumer.php | 2 +- src/Factory.php | 4 ++-- src/Model/Basic/Message.php | 10 +++++----- src/Model/Connection/TuneOk.php | 10 +++++----- src/Transport/Connection.php | 10 +++++----- src/Transport/Connection/Handshake.php | 3 +-- src/Transport/Connection/Heartbeat.php | 12 ++++++------ src/Transport/Connection/MessageReader.php | 2 +- src/Transport/Protocol/Basic.php | 8 ++------ src/Transport/Protocol/Connection.php | 1 - tests/ClientTest.php | 6 +++--- tests/Model/Basic/MessageTest.php | 2 +- tests/Model/Connection/TuneOkTest.php | 2 +- tests/Transport/Connection/FrameReaderTest.php | 2 +- tests/Transport/ConnectionTest.php | 8 ++++---- tests/Transport/Protocol/BasicTest.php | 2 +- tests/Transport/Protocol/ConnectionTest.php | 2 +- tests/Transport/ProtocolTest.php | 2 +- 20 files changed, 44 insertions(+), 48 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 970a227..45b21fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ ### Changed - Requires `innmind/foundation:~1.5` +- `Innmind\AMQP\Factory::make()` timeout argument is now expressed via `Innmind\TimeContinuum\Period` +- `Innmind\AMQP\Model\Basic\Message` expiration is now expressed via `Innmind\TimeContinuum\Period` ### Fixed diff --git a/benchmark/client.php b/benchmark/client.php index a5bfd8e..81728ce 100644 --- a/benchmark/client.php +++ b/benchmark/client.php @@ -18,6 +18,6 @@ ->make( Transport::tcp(), Url::of('//guest:guest@localhost:5672/'), - Period::second(1)->asElapsedPeriod(), + Period::second(1), ) ->listenSignals($os->process()); diff --git a/fixtures/forever-consumer.php b/fixtures/forever-consumer.php index 898a0b7..ae2738b 100644 --- a/fixtures/forever-consumer.php +++ b/fixtures/forever-consumer.php @@ -18,7 +18,7 @@ ->make( Transport::tcp(), Url::of('//guest:guest@localhost:5672/'), - Period::second(1)->asElapsedPeriod(), + Period::second(1), ) ->listenSignals($os->process()) ->with(DeclareQueue::of('always-empty')) diff --git a/src/Factory.php b/src/Factory.php index b3b73ec..6c99f6c 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -6,7 +6,7 @@ use Innmind\OperatingSystem\OperatingSystem; use Innmind\IO\Sockets\Internet\Transport as Socket; use Innmind\Url\Url; -use Innmind\TimeContinuum\ElapsedPeriod; +use Innmind\TimeContinuum\Period; final class Factory { @@ -27,7 +27,7 @@ public static function of(OperatingSystem $os): self public function make( Socket $transport, Url $server, - ElapsedPeriod $timeout, + Period $timeout, ): Client { return Client::of( fn() => Transport\Connection::open( diff --git a/src/Model/Basic/Message.php b/src/Model/Basic/Message.php index 8d798ee..4acd162 100644 --- a/src/Model/Basic/Message.php +++ b/src/Model/Basic/Message.php @@ -17,7 +17,7 @@ }; use Innmind\TimeContinuum\{ PointInTime, - ElapsedPeriod, + Period, }; use Innmind\Filesystem\File\Content; use Innmind\Immutable\{ @@ -47,7 +47,7 @@ final class Message private Maybe $correlationId; /** @var Maybe */ private Maybe $replyTo; - /** @var Maybe */ + /** @var Maybe */ private Maybe $expiration; /** @var Maybe */ private Maybe $id; @@ -88,7 +88,7 @@ private function __construct(Sequence $chunks, int $length) $this->correlationId = Maybe::nothing(); /** @var Maybe */ $this->replyTo = Maybe::nothing(); - /** @var Maybe */ + /** @var Maybe */ $this->expiration = Maybe::nothing(); /** @var Maybe */ $this->id = Maybe::nothing(); @@ -268,7 +268,7 @@ public function withReplyTo(ReplyTo $replyTo): self } /** - * @return Maybe + * @return Maybe */ #[\NoDiscard] public function expiration(): Maybe @@ -277,7 +277,7 @@ public function expiration(): Maybe } #[\NoDiscard] - public function withExpiration(ElapsedPeriod $expiration): self + public function withExpiration(Period $expiration): self { $self = clone $this; $self->expiration = Maybe::just($expiration); diff --git a/src/Model/Connection/TuneOk.php b/src/Model/Connection/TuneOk.php index f68d782..4a8494a 100644 --- a/src/Model/Connection/TuneOk.php +++ b/src/Model/Connection/TuneOk.php @@ -3,7 +3,7 @@ namespace Innmind\AMQP\Model\Connection; -use Innmind\TimeContinuum\ElapsedPeriod; +use Innmind\TimeContinuum\Period; /** * @psalm-immutable @@ -12,12 +12,12 @@ final class TuneOk { private MaxChannels $maxChannels; private MaxFrameSize $maxFrameSize; - private ElapsedPeriod $heartbeat; + private Period $heartbeat; private function __construct( MaxChannels $maxChannels, MaxFrameSize $maxFrameSize, - ElapsedPeriod $heartbeat, + Period $heartbeat, ) { $this->maxChannels = $maxChannels; $this->maxFrameSize = $maxFrameSize; @@ -31,7 +31,7 @@ private function __construct( public static function of( MaxChannels $maxChannels, MaxFrameSize $maxFrameSize, - ElapsedPeriod $heartbeat, + Period $heartbeat, ): self { return new self($maxChannels, $maxFrameSize, $heartbeat); } @@ -55,7 +55,7 @@ public function maxFrameSize(): int } #[\NoDiscard] - public function heartbeat(): ElapsedPeriod + public function heartbeat(): Period { return $this->heartbeat; } diff --git a/src/Transport/Connection.php b/src/Transport/Connection.php index ade49dd..12673b9 100644 --- a/src/Transport/Connection.php +++ b/src/Transport/Connection.php @@ -30,7 +30,7 @@ }; use Innmind\Url\Url; use Innmind\TimeContinuum\{ - ElapsedPeriod, + Period, Clock, }; use Innmind\OperatingSystem\Remote; @@ -86,7 +86,7 @@ public static function open( Transport $transport, Url $server, Protocol $protocol, - ElapsedPeriod $timeout, + Period $timeout, Clock $clock, Remote $remote, ): Maybe { @@ -97,7 +97,7 @@ public static function open( ) ->map( static fn($socket) => $socket - ->timeoutAfter($timeout->asPeriod()) + ->timeoutAfter($timeout) ->toEncoding(Str\Encoding::ascii), ) ->flatMap( @@ -211,7 +211,7 @@ public function close(): Maybe public function tune( MaxChannels $maxChannels, MaxFrameSize $maxFrameSize, - ElapsedPeriod $heartbeat, + Period $heartbeat, ): Maybe { return $this ->send(static fn($protocol) => $protocol->connection()->tuneOk( @@ -225,7 +225,7 @@ public function tune( ->map(fn() => new self( $this->protocol, $this->heartbeat->adjust($heartbeat), - $this->socket->timeoutAfter($heartbeat->asPeriod()), + $this->socket->timeoutAfter($heartbeat), $maxChannels, $maxFrameSize, $this->frame, diff --git a/src/Transport/Connection/Handshake.php b/src/Transport/Connection/Handshake.php index c7d1297..7c654c1 100644 --- a/src/Transport/Connection/Handshake.php +++ b/src/Transport/Connection/Handshake.php @@ -87,8 +87,7 @@ private function maybeTune(Connection $connection, Frame $frame): Either ->get(2) ->keep(Instance::of(Value\UnsignedShortInteger::class)) ->map(static fn($value) => $value->original()) - ->map(Period::millisecond(...)) - ->map(static fn($period) => $period->asElapsedPeriod()); + ->map(Period::millisecond(...)); return Maybe::all($maxChannels, $maxFrameSize, $heartbeat) ->flatMap($connection->tune(...)) diff --git a/src/Transport/Connection/Heartbeat.php b/src/Transport/Connection/Heartbeat.php index cdac261..8892885 100644 --- a/src/Transport/Connection/Heartbeat.php +++ b/src/Transport/Connection/Heartbeat.php @@ -9,7 +9,7 @@ use Innmind\TimeContinuum\{ Clock, PointInTime, - ElapsedPeriod, + Period, }; use Innmind\Immutable\Sequence; @@ -19,12 +19,12 @@ final class Heartbeat { private Clock $clock; - private ElapsedPeriod $threshold; + private Period $threshold; private PointInTime $lastReceivedData; private function __construct( Clock $clock, - ElapsedPeriod $threshold, + Period $threshold, PointInTime $lastReceivedData, ) { $this->clock = $clock; @@ -32,7 +32,7 @@ private function __construct( $this->lastReceivedData = $lastReceivedData; } - public static function start(Clock $clock, ElapsedPeriod $threshold): self + public static function start(Clock $clock, Period $threshold): self { return new self($clock, $threshold, $clock->now()); } @@ -47,7 +47,7 @@ public function frames(): Sequence ->clock ->now() ->elapsedSince($this->lastReceivedData) - ->longerThan($this->threshold) + ->longerThan($this->threshold->asElapsedPeriod()) ) { $this->lastReceivedData = $this->clock->now(); @@ -62,7 +62,7 @@ public function active(): void $this->lastReceivedData = $this->clock->now(); } - public function adjust(ElapsedPeriod $threshold): self + public function adjust(Period $threshold): self { return new self( $this->clock, diff --git a/src/Transport/Connection/MessageReader.php b/src/Transport/Connection/MessageReader.php index d31b494..f9eb361 100644 --- a/src/Transport/Connection/MessageReader.php +++ b/src/Transport/Connection/MessageReader.php @@ -181,7 +181,7 @@ private function addProperties( ->asPredicate(), ) ->map(Period::millisecond(...)) - ->map(static fn($expiration) => $message->withExpiration($expiration->asElapsedPeriod())), + ->map(static fn($expiration) => $message->withExpiration($expiration)), ], [ 7, diff --git a/src/Transport/Protocol/Basic.php b/src/Transport/Protocol/Basic.php index e366885..43245ee 100644 --- a/src/Transport/Protocol/Basic.php +++ b/src/Transport/Protocol/Basic.php @@ -255,12 +255,8 @@ private function serializeProperties(Message $message): array static fn($expiration) => [ $flagBits | (1 << 8), ($properties)(ShortString::of(Str::of((string) ( - $expiration - ->asPeriod() - ->milliseconds() + - $expiration - ->asPeriod() - ->seconds() * 1000 + $expiration->milliseconds() + + $expiration->seconds() * 1000 )))), ], static fn() => [$flagBits, $properties], diff --git a/src/Transport/Protocol/Connection.php b/src/Transport/Protocol/Connection.php index 7a2cf69..047cb37 100644 --- a/src/Transport/Protocol/Connection.php +++ b/src/Transport/Protocol/Connection.php @@ -99,7 +99,6 @@ public function tuneOk(TuneOk $command): Sequence UnsignedShortInteger::of( $command ->heartbeat() - ->asPeriod() ->seconds(), ), )); diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 9fa170a..a22b783 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -68,7 +68,7 @@ public function setUp(): void $this->client = Factory::of($this->os)->make( Transport::tcp(), Url::of('//guest:guest@localhost:5672/'), - Period::second(1)->asElapsedPeriod(), + Period::second(1), ); } @@ -284,7 +284,7 @@ public function testGetMessageWithAllProperties() ->withPriority(Message\Priority::five) ->withCorrelationId(Message\CorrelationId::of('correlation')) ->withReplyTo(Message\ReplyTo::of('reply')) - ->withExpiration(Period::second(10)->asElapsedPeriod()) + ->withExpiration(Period::second(10)) ->withId(Message\Id::of('id')) ->withTimestamp($now = $this->os->clock()->now()) ->withType(Message\Type::of('type')) @@ -416,7 +416,7 @@ public function testGetMessageWithAllProperties() static fn() => null, )); $this->assertSame(10000, $message->expiration()->match( - static fn($value) => $value->asPeriod()->seconds() * 1000, + static fn($value) => $value->seconds() * 1000, static fn() => null, )); $this->assertSame('id', $message->id()->match( diff --git a/tests/Model/Basic/MessageTest.php b/tests/Model/Basic/MessageTest.php index 2da672d..f22f0ac 100644 --- a/tests/Model/Basic/MessageTest.php +++ b/tests/Model/Basic/MessageTest.php @@ -205,7 +205,7 @@ public function testExpiration() { $message = Message::of(Str::of('')); $message2 = $message->withExpiration( - $expected = Period::second(1)->asElapsedPeriod(), + $expected = Period::second(1), ); $this->assertInstanceOf(Message::class, $message2); diff --git a/tests/Model/Connection/TuneOkTest.php b/tests/Model/Connection/TuneOkTest.php index 2c6768e..c17c4cb 100644 --- a/tests/Model/Connection/TuneOkTest.php +++ b/tests/Model/Connection/TuneOkTest.php @@ -21,7 +21,7 @@ public function testInterface() $command = TuneOk::of( MaxChannels::of(1), MaxFrameSize::of(10), - $heartbeat = Period::second(1)->asElapsedPeriod(), + $heartbeat = Period::second(1), ); $this->assertSame(1, $command->maxChannels()); diff --git a/tests/Transport/Connection/FrameReaderTest.php b/tests/Transport/Connection/FrameReaderTest.php index 8611d1b..7ffdb8c 100644 --- a/tests/Transport/Connection/FrameReaderTest.php +++ b/tests/Transport/Connection/FrameReaderTest.php @@ -194,7 +194,7 @@ public function testReadHeader() ->withPriority(Priority::five) ->withCorrelationId(CorrelationId::of('correlation')) ->withReplyTo(ReplyTo::of('reply')) - ->withExpiration(Period::second(1)->asElapsedPeriod()) + ->withExpiration(Period::second(1)) ->withId(Id::of('id')) ->withTimestamp($now = PointInTime::now()) ->withType(MessageType::of('type')) diff --git a/tests/Transport/ConnectionTest.php b/tests/Transport/ConnectionTest.php index 9d1cd55..ab9a8bc 100644 --- a/tests/Transport/ConnectionTest.php +++ b/tests/Transport/ConnectionTest.php @@ -34,7 +34,7 @@ public function testInterface() Transport::tcp(), Url::of('//guest:guest@localhost:5672/'), $protocol = new Protocol($os->clock(), new ArgumentTranslator), - Period::second(1)->asElapsedPeriod(), + Period::second(1), $os->clock(), $os->remote(), $os->sockets(), @@ -76,7 +76,7 @@ public function testClose() Transport::tcp(), Url::of('//guest:guest@localhost:5672/'), $protocol = new Protocol($os->clock(), new ArgumentTranslator), - Period::second(1)->asElapsedPeriod(), + Period::second(1), $os->clock(), $os->remote(), $os->sockets(), @@ -100,7 +100,7 @@ public function testReturnFailureWhenReceivedFrameIsNotTheExpectedOne() Transport::tcp(), Url::of('//guest:guest@localhost:5672/'), new Protocol($os->clock(), new ArgumentTranslator), - Period::second(1)->asElapsedPeriod(), + Period::second(1), $os->clock(), $os->remote(), $os->sockets(), @@ -132,7 +132,7 @@ public function testReturnFailureWhenConnectionClosedByServer() Transport::tcp(), Url::of('//guest:guest@localhost:5672/'), $protocol = new Protocol($os->clock(), new ArgumentTranslator), - Period::second(1)->asElapsedPeriod(), + Period::second(1), $os->clock(), $os->remote(), $os->sockets(), diff --git a/tests/Transport/Protocol/BasicTest.php b/tests/Transport/Protocol/BasicTest.php index 06f2e74..1d8ea79 100644 --- a/tests/Transport/Protocol/BasicTest.php +++ b/tests/Transport/Protocol/BasicTest.php @@ -571,7 +571,7 @@ public function testPublishWithProperties() ->withPriority(Priority::five) ->withCorrelationId(CorrelationId::of('correlation')) ->withReplyTo(ReplyTo::of('reply')) - ->withExpiration(Period::second(1)->asElapsedPeriod()) + ->withExpiration(Period::second(1)) ->withId(Id::of('id')) ->withTimestamp($now = PointInTime::now()) ->withType(MessageType::of('type')) diff --git a/tests/Transport/Protocol/ConnectionTest.php b/tests/Transport/Protocol/ConnectionTest.php index 791012e..76fc328 100644 --- a/tests/Transport/Protocol/ConnectionTest.php +++ b/tests/Transport/Protocol/ConnectionTest.php @@ -370,7 +370,7 @@ public function testTuneOk() TuneOk::of( MaxChannels::of(1), MaxFrameSize::of(10), - Period::second(3)->asElapsedPeriod(), + Period::second(3), ), )->match( static fn($frame) => $frame, diff --git a/tests/Transport/ProtocolTest.php b/tests/Transport/ProtocolTest.php index 4dcf2f2..4588a68 100644 --- a/tests/Transport/ProtocolTest.php +++ b/tests/Transport/ProtocolTest.php @@ -82,7 +82,7 @@ public function testReadHeader() ->withPriority(Priority::five) ->withCorrelationId(CorrelationId::of('correlation')) ->withReplyTo(ReplyTo::of('reply')) - ->withExpiration(Period::second(1)->asElapsedPeriod()) + ->withExpiration(Period::second(1)) ->withId(Id::of('id')) ->withTimestamp($now = PointInTime::now()) ->withType(Type::of('type'))