diff --git a/lib/sync.js b/lib/sync.js index 73b695643..bdd7c0366 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) { diff --git a/spec/unit/matrix-client.spec.js b/spec/unit/matrix-client.spec.js index a2aad4a5b..738c32558 100644 --- a/spec/unit/matrix-client.spec.js +++ b/spec/unit/matrix-client.spec.js @@ -223,6 +223,7 @@ describe("MatrixClient", function() { if (state === "ERROR" && httpLookups.length > 0) { expect(httpLookups.length).toEqual(2); expect(client.retryImmediately()).toBe(true); + jasmine.Clock.tick(1); } else if (state === "PREPARED" && httpLookups.length === 0) { client.removeListener("sync", syncListener); done(); @@ -248,6 +249,7 @@ describe("MatrixClient", function() { expect(client.retryImmediately()).toBe( true, "retryImmediately returned false" ); + jasmine.Clock.tick(1); } else if (state === "SYNCING" && httpLookups.length === 0) { client.removeListener("sync", syncListener); done(); @@ -269,6 +271,7 @@ describe("MatrixClient", function() { if (state === "ERROR" && httpLookups.length > 0) { expect(httpLookups.length).toEqual(3); expect(client.retryImmediately()).toBe(true); + jasmine.Clock.tick(1); } else if (state === "PREPARED" && httpLookups.length === 0) { client.removeListener("sync", syncListener); done(); @@ -299,8 +302,8 @@ describe("MatrixClient", function() { client.removeListener("sync", syncListener); done(); } - // standard retry time is 4s - jasmine.Clock.tick(4001); + // standard retry time is 5 to 10 seconds + jasmine.Clock.tick(10000); }; }