Skip to content
Merged
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
18 changes: 16 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public function subscribeIterator(
* @param ?non-empty-string $queueGroup
* @param positive-int $bufferSize
* @throws NatsException
* @throws \Throwable
*/
public function subscribe(
string $subject,
Expand All @@ -164,7 +165,13 @@ public function subscribe(

$this->subscribers[$subscriptionId] = [$handler->push(...), $subscription = $handler->subscription];

$this->connection($cancellation)->execute(Internal\Command::sub($subject, $subscriptionId, $queueGroup));
try {
$this->connection($cancellation)->execute(Internal\Command::sub($subject, $subscriptionId, $queueGroup));
} catch (\Throwable $e) {
unset($this->subscribers[$subscriptionId]);

throw $e;
}

return $subscription;
}
Expand Down Expand Up @@ -247,6 +254,7 @@ public function __destruct()
* @param callable(Delivery): void $handler
* @return \Closure(?Cancellation=): void
* @throws NatsException
* @throws \Throwable
*/
private function subscribeCallback(
string $subject,
Expand All @@ -264,7 +272,13 @@ static function (Delivery $delivery) use ($handler): bool {
null,
];

$this->connection($cancellation)->execute(Internal\Command::sub($subject, $subscriptionId));
try {
$this->connection($cancellation)->execute(Internal\Command::sub($subject, $subscriptionId));
} catch (\Throwable $e) {
unset($this->subscribers[$subscriptionId]);

throw $e;
}

return function (?Cancellation $cancellation = null) use ($subscriptionId): void {
$this->unsubscribe($subscriptionId, $cancellation);
Expand Down
1 change: 0 additions & 1 deletion src/Internal/Protocol/decodeHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ function parseStatus(string $line): ?array
if (\count($chunks) > 1) {
/**
* @var numeric-string $code
* @phpstan-ignore offsetAccess.notFound
*/
$code = $chunks[1];
$description = implode(' ', \array_slice($chunks, 2));
Expand Down
4 changes: 2 additions & 2 deletions src/JetStream/Internal/PullMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ private function digest(NatsDelivery $delivery, Subscription $subscription): voi
} elseif ($statusDescription?->is(Description::LeadershipChange) ?? false) {
$this->reset();
} elseif ($statusDescription?->is(Description::MaxBytesExceeded, Description::BatchCompleted, Description::RequestTimeout) ?? false) {
$messagesLeft = $delivery->message->headers?->get(Header\PendingMessages::header()) ?? 0;
$bytesLeft = $delivery->message->headers?->get(Header\PendingBytes::header()) ?? 0;
$messagesLeft = $delivery->message->headers->get(Header\PendingMessages::header()) ?? 0;
$bytesLeft = $delivery->message->headers->get(Header\PendingBytes::header()) ?? 0;

$this->pendingMessages -= $messagesLeft;

Expand Down