-
Notifications
You must be signed in to change notification settings - Fork 69
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
Implement automatic reconnection #287
base: main
Are you sure you want to change the base?
Conversation
Thank you for looking into this! 👍 Automatic reconnect is definitely on the wish list of many of our users. :) I do like the idea to have "everything in one client" as opposed to my approach with a separate low-level and high-level client. At least the single-client approach is easier to grok for our users. 👍 I looked through the code, and it looks good. I'm (as always) a little bit concerned about adding extra internal state to our client (the new reconnect task). Increases the maintenance burden. In any case, for this feature I don't think we can avoid it if we want to use a single client. 😄 Aside: There is a lot of "reset state" going on (re-creating the futures). That's not your fault (or the fault of this PR) but my own fault (using futures in the first place). 😅 I strongly suggest that we look at alternatives for all the internal futures and background tasks. I'm thinking anyio (to no ones surprise I guess). :)
That is a concern of mine as well! Indeed, we would have to save all the subscriptions inside the client and then "resubscribe" when the connection is back online. These subscriptions are even more state to manage. :) Here is a suggestion that is a slight variation of the current approach:
This way, we get the separation of high-level and low-level and the easier maintenance that follows. It also allows allows to replace the
I see the benefits of this approach! 👍 I suggest that we do both 😄 That is, to have both Again, thank you for looking into this issue and very well done on the draft implementation. 👍 Let me know what you think of my comments above and do say if you have any questions. 😄 |
Thanks for your thoughts on this! Your reviews are one of the main reasons this project is so fun for me 😉 You're right about the internal state getting slightly out of control. While implementing this draft I already got bitten resetting futures while they were awaited elsewhere (which raises
I'm not sure I understand this point. Do you mean using streams and broadcast for the I'm on board with the |
Thank you for saying that. 😄 I don't have that much time these days but I do try to find it anyhow to at least do these reviews. :) It's a bit easier here during Easter.
I'm was leaning towards doing "streams and broadcasts" to do the reconnection itself (like in my sample code). E.g., keep the current In any case, let's be pragmatic and review our options here:
So in the larger perspective (time and resources being essential) I do actually lean towards option (1). Option 1 provides value here and now at the cost of future maintenance. 😄 If you agree, I think the next steps is to write out some test cases for this (to mitigate the maintenance cost). With tests in place, the pro-con calculation becomes easy since the maintenance cost goes towards zero. 😉 Again, thank you for all the time that you put into this PR (and the aiomqtt project in general). 👍 Let me know if you have any comments or questions. :) |
Thanks for elaborating, I think I understand what you mean now 😊 I'll play around with streams and broadcasts to understand them a little better and see how much work the second option would bring! You convinced me that that's the better option 😄 I'll report back once I have more, could take a bit, though, as I'm busy the next few days 😋 |
I just stumbled across this so sorry if I am late to the party. From my experience it might be better to split the client into a low level and high level because as mentioned it makes maintenance much easier, however I have no strong opinion on that. While looking through the PR I am not sure I understood everything correctly: I think he usage of the client is hard because there are multiple places where the disconnect error can occur. Typically I have a Edit: |
Hi there @frederikaalund @JonathanPlasse 😊
Frederik drafted how reconnection could look like a while ago already (thank you again, master of asyncio 🙏😄). I finally had some time to hack around with this! Some thoughts:
reconnect=True
parameter to the client and leave all method signatures the same.For now, I've implemented the reconnection background task and adapted the
publish
method to wait until the connection returns and the message could actually be published (both are probably still full of bugs). You can play around with it by shutting a local MQTT broker on and off (e.g. with./scripts/develop
) and by running:I'm still thinking about how to deal with existing subscriptions and last wills. We probably have to resend them when
clean_session=True
, otherwise they will cease to exist without notice to the user after a reconnection in the background.Happy to hear what your thoughts are on this (or anyone else's who wants to chip in) 😊