feat: refreshes the token and reconnects if publishing fails #228
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.
This pull request introduces robust support for automatic reconnection and JWT token refresh for the Fleet MQTT connection. The changes ensure that the agent can recover from authentication or connectivity failures by refreshing its token and re-establishing the connection without manual intervention. The implementation tracks consecutive failures in heartbeat, capabilities, and group membership publishing, triggering a reconnection sequence when thresholds are reached. Additionally, the
AuthTokenManagernow caches credentials and token state to enable seamless refreshes.Automatic reconnection and JWT refresh:
reconnectChantofleetConfigManagerandMQTTConnectionfor signaling reconnection requests, and implemented logic to trigger reconnection after repeated failures in heartbeat, capabilities, or group membership publishing. [1] [2] [3] [4] [5]fleetConfigManager.Startthat listens for reconnect requests and calls a newrefreshAndReconnectmethod to refresh the JWT token and re-establish the MQTT connection.Auth token management improvements:
AuthTokenManagerto cache token credentials and expiration time, enabling token refresh via a newRefreshTokenmethod and expiration checks viaIsTokenExpired. [1] [2] [3]Connection lifecycle enhancements:
Reconnectmethod toMQTTConnectionto cleanly disconnect, reset failure counters, and reconnect with new credentials.Test updates:
reconnectChanargument and heartbeat changes, ensuring coverage of the new reconnection logic. [1] [2] [3] [4] [5]