Merge Envelope with Message
This commit is contained in:
@@ -1,59 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Nsq;
|
||||
|
||||
use LogicException;
|
||||
|
||||
final class Envelope
|
||||
{
|
||||
/**
|
||||
* @psalm-readonly
|
||||
*/
|
||||
public Message $message;
|
||||
|
||||
private bool $finished = false;
|
||||
|
||||
private Reader $connection;
|
||||
|
||||
public function __construct(Message $message, Reader $connection)
|
||||
{
|
||||
$this->message = $message;
|
||||
$this->connection = $connection;
|
||||
}
|
||||
|
||||
public function isFinished(): bool
|
||||
{
|
||||
return $this->finished;
|
||||
}
|
||||
|
||||
public function finish(): void
|
||||
{
|
||||
if ($this->finished) {
|
||||
throw new LogicException('Can\'t finish message as it already finished.');
|
||||
}
|
||||
|
||||
$this->connection->fin($this->message->id);
|
||||
$this->finished = true;
|
||||
}
|
||||
|
||||
public function requeue(int $timeout): void
|
||||
{
|
||||
if ($this->finished) {
|
||||
throw new LogicException('Can\'t requeue message as it already finished.');
|
||||
}
|
||||
|
||||
$this->connection->req($this->message->id, $timeout);
|
||||
$this->finished = true;
|
||||
}
|
||||
|
||||
public function touch(): void
|
||||
{
|
||||
if ($this->finished) {
|
||||
throw new LogicException('Can\'t touch message as it already finished.');
|
||||
}
|
||||
|
||||
$this->connection->touch($this->message->id);
|
||||
}
|
||||
}
|
@@ -4,24 +4,75 @@ declare(strict_types=1);
|
||||
|
||||
namespace Nsq;
|
||||
|
||||
/**
|
||||
* @psalm-immutable
|
||||
*/
|
||||
use LogicException;
|
||||
|
||||
final class Message
|
||||
{
|
||||
/**
|
||||
* @psalm-readonly
|
||||
*/
|
||||
public int $timestamp;
|
||||
|
||||
/**
|
||||
* @psalm-readonly
|
||||
*/
|
||||
public int $attempts;
|
||||
|
||||
/**
|
||||
* @psalm-readonly
|
||||
*/
|
||||
public string $id;
|
||||
|
||||
/**
|
||||
* @psalm-readonly
|
||||
*/
|
||||
public string $body;
|
||||
|
||||
public function __construct(int $timestamp, int $attempts, string $id, string $body)
|
||||
public function __construct(int $timestamp, int $attempts, string $id, string $body, Reader $reader)
|
||||
{
|
||||
$this->timestamp = $timestamp;
|
||||
$this->attempts = $attempts;
|
||||
$this->id = $id;
|
||||
$this->body = $body;
|
||||
|
||||
$this->connection = $reader;
|
||||
}
|
||||
|
||||
private bool $finished = false;
|
||||
|
||||
private Reader $connection;
|
||||
|
||||
public function isFinished(): bool
|
||||
{
|
||||
return $this->finished;
|
||||
}
|
||||
|
||||
public function finish(): void
|
||||
{
|
||||
if ($this->finished) {
|
||||
throw new LogicException('Can\'t finish message as it already finished.');
|
||||
}
|
||||
|
||||
$this->connection->fin($this->id);
|
||||
$this->finished = true;
|
||||
}
|
||||
|
||||
public function requeue(int $timeout): void
|
||||
{
|
||||
if ($this->finished) {
|
||||
throw new LogicException('Can\'t requeue message as it already finished.');
|
||||
}
|
||||
|
||||
$this->connection->req($this->id, $timeout);
|
||||
$this->finished = true;
|
||||
}
|
||||
|
||||
public function touch(): void
|
||||
{
|
||||
if ($this->finished) {
|
||||
throw new LogicException('Can\'t touch message as it already finished.');
|
||||
}
|
||||
|
||||
$this->connection->touch($this->id);
|
||||
}
|
||||
}
|
||||
|
@@ -44,7 +44,7 @@ final class Response
|
||||
return self::TYPE_RESPONSE === $this->type && self::HEARTBEAT === $this->buffer->bytes();
|
||||
}
|
||||
|
||||
public function toMessage(): Message
|
||||
public function toMessage(Reader $reader): Message
|
||||
{
|
||||
if (self::TYPE_MESSAGE !== $this->type) {
|
||||
throw new Exception(sprintf('Expecting "%s" type, but NSQ return: "%s"', self::TYPE_MESSAGE, $this->type));
|
||||
@@ -57,6 +57,6 @@ final class Response
|
||||
$id = $buffer->consume(Bytes::BYTES_ID);
|
||||
$body = $buffer->flush();
|
||||
|
||||
return new Message($timestamp, $attempts, $id, $body);
|
||||
return new Message($timestamp, $attempts, $id, $body, $reader);
|
||||
}
|
||||
}
|
||||
|
@@ -22,7 +22,7 @@ final class Subscriber
|
||||
}
|
||||
|
||||
/**
|
||||
* @psalm-return Generator<int, Envelope|null, int|float|null, void>
|
||||
* @psalm-return Generator<int, Message|null, int|float|null, void>
|
||||
*/
|
||||
public function subscribe(string $topic, string $channel, float $timeout = 0): Generator
|
||||
{
|
||||
@@ -31,9 +31,7 @@ final class Subscriber
|
||||
while (true) {
|
||||
$this->reader->rdy(1);
|
||||
|
||||
$message = $this->consume($timeout);
|
||||
|
||||
$command = yield null === $message ? null : new Envelope($message, $this->reader);
|
||||
$command = yield $this->consume($timeout);
|
||||
|
||||
if (self::STOP === $command) {
|
||||
break;
|
||||
@@ -70,6 +68,6 @@ final class Subscriber
|
||||
);
|
||||
}
|
||||
|
||||
return $response->toMessage();
|
||||
return $response->toMessage($this->reader);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user