Move reconnect logic to ConnectionWatchdog #544
Merged
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.
The existing reconnect logic executes when a
Disconnected
event fires, and this event can fire while the client is attempting a connection, which causes the reconnect logic to be executed again, and ultimately a ton of reconnect attempts are made concurrently.I could have added a semaphore to prevent this re-entrance, but there was no good way to ensure that reconnect logic would continue to execute if a connection connected successfully, but disconnected before the semaphore was released.
This PR adds a
ConnectionWatchdog
class that uses a timer to check the status of the connection and if the client is disconnected, the reconnect logic is executed. The watchdog starts when a qualifying disconnect event is fired, and stops when a connect event is fired.There shouldn't be a way for the watchdog to remain disabled, but if so, the logic can be adjusted to run all the time (if the timer interval is increased to maybe 500ms)
Closes #331