feat: Allow arbitrarily short MQTT.loop() timeouts with socket polling#259
Open
ide wants to merge 1 commit into
Open
feat: Allow arbitrarily short MQTT.loop() timeouts with socket polling#259ide wants to merge 1 commit into
ide wants to merge 1 commit into
Conversation
Why: `MQTT.loop()` previously required `timeout >= socket_timeout`. For sensor apps, that can delay publishing: if a sensor event happens while the app is waiting in `MQTT.loop()`, the app cannot publish until that wait returns. Lowering the global `socket_timeout` only partly helps, because broker connections can then warn when they take longer than the shorter timeout. `MQTT.loop()` should be able to check quickly, including with no wait, without changing the socket timeout used by other network operations. How: We register connected sockets with `select.poll()` when available. `MQTT.loop()` uses readiness polling before reading from the socket, and each poll wait is capped at the earlier of the remaining loop timeout or the next keepalive deadline. The old timeout guard is still used when polling is unavailable. Negative loop timeouts are rejected, pollers are cleaned up when sockets close, and poll error events are treated as readable so normal socket handling reports the error. Directly addresses adafruit#241, adafruit#195, and the short-loop-timeout/socket-timeout subcase in adafruit#148. Tests: - `python -m pytest tests/test_loop.py tests/test_reconnect.py` - `python -m tox -e lint -- --all-files` - `git diff --check upstream/main...HEAD` - Ran hardware polling benchmarks on a Raspberry Pi Pico 2 W with CircuitPython 10.2.1. The board supports `select.poll()` and `poller.ipoll()`. Idle polling returned no events as expected. `ipoll()` was a little faster than `poll()`, used almost no extra heap in the allocation check, and worked in paced 100 Hz and 1000 Hz loops without adding missed periods beyond the baseline.
cb787c1 to
28bfda9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
MQTT.loop()currently requirestimeout >= socket_timeout. For sensor apps this can delay publishing: if a sensor event happens while the app is waiting inMQTT.loop(), the app cannot publish until that wait returns. Lowering the globalsocket_timeoutonly partly helps, because broker connections can then warn when they take longer than the socket timeout to set up.Ideally
MQTT.loop()should be able to check quickly, including with no wait, without changing the socket timeout used by other network operations.Directly addresses #241, #195, and the short-loop-timeout/socket-timeout subcase in #148.
How
We register connected sockets with
select.poll()when available.MQTT.loop()uses readiness polling before reading from the socket, and each poll wait is capped at the earlier of the remaining loop timeout or the next keepalive deadline.The existing implementation is still used when polling is unavailable depending on the device.
In terms of edge cases, negative loop timeouts are rejected, pollers are cleaned up when sockets close, and poll error events are treated as readable so normal socket handling reports the error.
Test Plan
python -m pytest tests/test_loop.py tests/test_reconnect.pypython -m tox -e lint -- --all-filesI ran hardware tests and polling benchmarks on a Raspberry Pi Pico 2 W with CircuitPython 10.2.1. The board supports
select.poll()andpoller.ipoll(). Idle polling returned no events as expected.ipoll()was a little faster thanpoll(), used almost no extra heap in the allocation check (this was empirical motivation to supportipollin this PR), and worked in paced 100 Hz and 1000 Hz loops without adding missed periods beyond the baseline.