Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QoS 1 should only acknowledge if the handlers did not throw an Exception #167

Closed
wants to merge 1 commit into from
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
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