-
Notifications
You must be signed in to change notification settings - Fork 534
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
[Proposal] Add a DisconnectingHandler #410
Comments
PR #381 has now been accepted and this changes things a bit (quite a bit of the code you mention has been rewritten). It would be possible to call a I suspect that the time between the proposed I guess another option would be to move the call to A few other thoughts:
|
@MattBrittan Thanks a lot for the detailed response. Yes, I do see that a lot of code has now changed. :) To clarify my proposal included two parts:
Yes, [1] will add to the API and maintenance. But it gives the application flexibility to implement something that does more than just preventing Example: The client internally preventing a I accept your point that it may not be worth adding new API if the wind-down duration will always be very short. The other option you mentioned is inline with my thoughts on Additional Enhancement For a non-AutoConnecting client, any teardown deadlock will prevent the The ideal state here is a short teardown duration for the workers, and a guarantee that the teardown will always take place without any deadlocks. If setting |
Reg: a few other thoughts that you mentioned. Yes, I proposed calling I've not seen many implementations of out-of-bound Reconnect and auto-Reconnect, but my assumption on the implementation pattern is: [a] out-of-bound Reconnect clients would go through a full [b] AutoReconnect clients would go through a [sidenote] Yes :) You are correct in saying that an AutoReconnect client, with a persistent Thanks again :) |
not sure if this issue is related to yours 🤔 |
@pshirali apologies for not replying - I had not noticed that you had posted further on this issue. I can see the benefits of having a |
Thanks @MattBrittan. Unfortunately, it may be some more time before I begin to implement this. But I will take this up and submit a PR. Thanks again. |
If you are still thinking about implementing this please see tins comment (re a generic connection status notification function) |
In
Client.internalConnLost
, when a disconnection takes place, the worker goroutines are waited on till they get done. This is followed by logic for AutoReconnect and OnConnectionLost. The opportunity for an application to handle the actual disconnection takes place only after the workers are waited upon.While waiting, the application may continue to publish. The
client.Publish
calls don't terminate early as the connection status has not yet changed toreconnecting
ordisconnected
. Further,Client.IsConnected()
is influenced by theAutoReconnect
andConnectRetry
options, where aclient.Publish
is not prevented during reconnections attempted by the client.It is valuable for an application to have an opportunity to take action immediately, when a disconnection is detected, even while the mqtt client is still shutting down its workers. Applications which would want to prevent publishing (or do something more) when a disconnection is detected, would desire to know of the disconnection at the earliest possible moment of it occurring.
Proposed Enhancement [1]
Introduce
type DisconnectingHandler func(Client, error)
handler, with aSetDisconnectingHandler(..)
option inClientOptions
. This handler will be invoked as a goroutine before thec.closeStop()
inClient.internalConnLost
inclient.go
Additional Enhancement [2] --- (but, is a deviation from current behavior)
disconnecting
status.c.setConnected(disconnecting)
will set thedisconnecting
status just before where theDisconnectingHandler
would be called (and much before waiting for workers, AutoReconnect and OnConnectionLost)disconnecting
status could causeclient.IsConnected()
to return false, thus preventingclient.Publish(..)
.These changes would prevent a
client.Publish(..)
at the earliest possible instant when a client gets disconnected. Unfortunately, this will change behavior for all non-AutoReconnect clients, whereclient.Publish
will begin to fail slightly earlier than what they are used to seeing.At the moment I'm proposing only
[1]
, as[2]
may require some discussion, for possible future consideration.I'll be happy to work on this and submit a PR.
The text was updated successfully, but these errors were encountered: