diff --git a/src/lib/cloud-provider.js b/src/lib/cloud-provider.js index 9076f0b5598..73c37195a88 100644 --- a/src/lib/cloud-provider.js +++ b/src/lib/cloud-provider.js @@ -26,6 +26,7 @@ class CloudProvider { this.cloudHost = cloudHost; this.connectionAttempts = 0; + this._resetConnectionAttemptsTimeout = null; // A queue of messages to send which were received before the // connection was ready @@ -81,9 +82,11 @@ class CloudProvider { } onOpen () { - // Reset connection attempts to 1 to make sure any subsequent reconnects - // use connectionAttempts=1 to calculate timeout - this.connectionAttempts = 1; + this._resetConnectionAttemptsTimeout = setTimeout(() => { + this.connectionAttempts = 0; + this._resetConnectionAttemptsTimeout = null; + }, 10 * 1000); + this.writeToServer('handshake'); log.info(`Successfully connected to clouddata server.`); @@ -97,6 +100,11 @@ class CloudProvider { } onClose (e) { + if (this._resetConnectionAttemptsTimeout) { + clearTimeout(this._resetConnectionAttemptsTimeout); + this._resetConnectionAttemptsTimeout = null; + } + // tw: code 4002 is "Username Error" -- do not try to reconnect if (e && e.code === 4002) { log.info('Cloud username is invalid. Not reconnecting.'); @@ -251,6 +259,10 @@ class CloudProvider { clearTimeout(this._connectionTimeout); this._connectionTimeout = null; } + if (this._resetConnectionAttemptsTimeout) { + clearTimeout(this._resetConnectionAttemptsTimeout); + this._resetConnectionAttemptsTimeout = null; + } this.connectionAttempts = 0; }