client icon indicating copy to clipboard operation
client copied to clipboard

queueWaitLimit check in possibly incorrect location?

Open cancodr opened this issue 1 year ago • 1 comments

First of all; great library. I've been able to subscribe/publish via PHP successfully.

I was looking for a way to break out of the loop after a pre-defined amount of time, in case the subscribed topic does not receive an update within a specified amount of time (ie: no reply timeout). I thought (perhaps incorrectly?), that the "queueWaitLimit" was for this purpose.

I struggled with it not working, but then realized that the if statement in the loop function is within the if ($exitWhenQueuesEmpty && $this->repository->countSubscriptions() === 0) check. Therefore, if there are subscriptions, the test for the queueWaitLimit will not happen.

I moved the if ($queueWaitLimit !== null && (microtime(true) - $loopStartedAt) > $queueWaitLimit) statement outside of the above if, and it works as expected now.

If my interpretation of the queueWaitLimit setting is incorrect, what would be the correct method to break out of the loop after a given timeout?

cancodr avatar Jul 03 '24 21:07 cancodr

The queueWaitLimit is supposed to prevent deadlocks when publishing messages with QoS > 0. It will simply exit even when there are unacknowledged published messages.

What is suited best for your use case is a loop event handler which exits after the provided seconds:

$mqtt->registerLoopEventHandler(function (MqttClient $client, float $elapsedTime) {
    if ($elapsedTime >= 10) {
        $client->interrupt();
    }
});

Namoshek avatar Jul 04 '24 08:07 Namoshek