Skip to content

Commit

Permalink
QoS 1 does only acknowledge if the handlers did not throw an Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmarschall committed Nov 8, 2023
1 parent 4d6ecec commit c2980f3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
7 changes: 5 additions & 2 deletions src/Concerns/OffersHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,20 +255,23 @@ public function unregisterMessageReceivedEventHandler(\Closure $callback = null)
* @param string $message
* @param int $qualityOfService
* @param bool $retained
* @return void
* @return bool False if an Event Handler threw an Exception, True otherwise.
*/
private function runMessageReceivedEventHandlers(string $topic, string $message, int $qualityOfService, bool $retained): void
private function runMessageReceivedEventHandlers(string $topic, string $message, int $qualityOfService, bool $retained): bool
{
$one_callback_failed = false;
foreach ($this->messageReceivedEventHandlers as $handler) {
try {
call_user_func($handler, $this, $topic, $message, $qualityOfService, $retained);
} catch (\Throwable $e) {
$one_callback_failed = true;
$this->logger->error('Received message hook callback threw exception for received message on topic [{topic}].', [
'topic' => $topic,
'exception' => $e,
]);
}
}
return !$one_callback_failed;
}

/**
Expand Down
23 changes: 14 additions & 9 deletions src/MqttClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -744,11 +744,6 @@ protected function handleMessage(Message $message): void
{
// PUBLISH (incoming)
if ($message->getType()->equals(MessageType::PUBLISH())) {
if ($message->getQualityOfService() === self::QOS_AT_LEAST_ONCE) {
// QoS 1.
$this->sendPublishAcknowledgement($message->getMessageId());
}

if ($message->getQualityOfService() === self::QOS_EXACTLY_ONCE) {
// QoS 2, part 1.
try {
Expand All @@ -772,7 +767,13 @@ protected function handleMessage(Message $message): void
}

// For QoS 0 and QoS 1 we can deliver right away.
$this->deliverPublishedMessage($message->getTopic(), $message->getContent(), $message->getQualityOfService());
if ($this->deliverPublishedMessage($message->getTopic(), $message->getContent(), $message->getQualityOfService())) {
if ($message->getQualityOfService() === self::QOS_AT_LEAST_ONCE) {
// QoS 1: Acknowledge only if the handlers did not throw an Exception.
$this->sendPublishAcknowledgement($message->getMessageId());
}
}

return;
}

Expand Down Expand Up @@ -930,9 +931,9 @@ protected function allQueuesAreEmpty(): bool
* @param string $message
* @param int $qualityOfServiceLevel
* @param bool $retained
* @return void
* @return bool False if an Event Handler threw an Exception, True otherwise.
*/
protected function deliverPublishedMessage(string $topic, string $message, int $qualityOfServiceLevel, bool $retained = false): void
protected function deliverPublishedMessage(string $topic, string $message, int $qualityOfServiceLevel, bool $retained = false): bool
{
$subscribers = $this->repository->getSubscriptionsMatchingTopic($topic);

Expand All @@ -943,6 +944,8 @@ protected function deliverPublishedMessage(string $topic, string $message, int $
'subscribers' => count($subscribers),
]);

$one_callback_failed = false;

foreach ($subscribers as $subscriber) {
if ($subscriber->getCallback() === null) {
continue;
Expand All @@ -951,6 +954,8 @@ protected function deliverPublishedMessage(string $topic, string $message, int $
try {
call_user_func($subscriber->getCallback(), $topic, $message, $retained, $subscriber->getMatchedWildcards($topic));
} catch (\Throwable $e) {
$one_callback_failed = true;

$this->logger->error('Subscriber callback threw exception for published message on topic [{topic}].', [
'topic' => $topic,
'message' => $message,
Expand All @@ -959,7 +964,7 @@ protected function deliverPublishedMessage(string $topic, string $message, int $
}
}

$this->runMessageReceivedEventHandlers($topic, $message, $qualityOfServiceLevel, $retained);
return $this->runMessageReceivedEventHandlers($topic, $message, $qualityOfServiceLevel, $retained) && !$one_callback_failed;
}

/**
Expand Down

0 comments on commit c2980f3

Please sign in to comment.