diff --git a/lib/sync.js b/lib/sync.js index 73b695643..b22f27ead 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -382,7 +382,7 @@ SyncApi.prototype.stop = function() { */ SyncApi.prototype.retryImmediately = function() { if (!this._connectionReturnedDefer) { return false; } - this._startKeepAlives(); + this._startKeepAlives(0); return true; }; @@ -686,13 +686,25 @@ SyncApi.prototype._processSyncResponse = function(syncToken, data) { }; /** + * Starts polling the connectivity check endpoint + * @param {number} delay How long to delay until the first poll. + * defaults to a short, randomised interval (to prevent + * tightlooping if /versions succeeds but /sync etc. fail). * @return {promise} */ -SyncApi.prototype._startKeepAlives = function() { +SyncApi.prototype._startKeepAlives = function(delay) { + if (delay === undefined) { + delay = 5000 + Math.floor(Math.random() * 5000) + } + if (this._keepAliveTimer !== null) { clearTimeout(this._keepAliveTimer); } - this._pokeKeepAlive(); + var self = this; + self._keepAliveTimer = setTimeout( + self._pokeKeepAlive.bind(self), + delay + ); if (!this._connectionReturnedDefer) { this._connectionReturnedDefer = q.defer(); } @@ -948,7 +960,7 @@ SyncApi.prototype._updateSyncState = function(newState, data) { */ SyncApi.prototype._onOnline = function() { debuglog("Browser thinks we are back online"); - this._startKeepAlives(); + this._startKeepAlives(0); }; function createNewUser(client, userId) {