MWE: Simplified transparent reconnect #316
carstenandrich
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In need of an asyncio MQTT client, I stumbled over aiomqtt. However, the Reconnection example did not satisfy my requirements for transparent reconnect. Eventually, I implemented a small asyncio wrapper around Paho MQTT inspired by Paho's
loop_asyncio.py
example. It reconnects transparently, i.e. without any user intervention, also supporting uninterrupted async iteration.I've intentionally kept the reconnect logic away from the
on_connect()
andon_disconnect()
callbacks, but instead integrated it into the same coroutine that periodically callsloop_misc()
to minimize implementation complexity by delegating connection state tracking to the wrapped Paho client. This approach could possibly simplify #287 a bit, as it obviates both Futures used to synchronize_reconnection(self)
with the callbacks.Unfortunately, I don't have the time to turn this into a PR. Instead, I've extracted an MWE from my Paho client wrapper, hoping that it could be useful for aiomqtt. What's currently missing, is an option to raise an exception if the very first connection attempt fails.
UPDATE: Refactored and improved below MWE. Asynchronous connect is now optional, successive reconnect attempts use incremental delay, messages preceding CONNACK are discarded, logging moved to a child class, and publish() will drop messages if the send buffer is full to avoid building an infinite backlog of QoS=0 messages.
Beta Was this translation helpful? Give feedback.
All reactions