From 5e18c84e530205fb061b35c8895497e359f3ba25 Mon Sep 17 00:00:00 2001 From: David Baker Date: Mon, 14 Mar 2016 16:50:00 +0000 Subject: [PATCH 1/3] Add a delay before we start polling the connectivity check endpoint to avoid tightlooping if the conn check succeeds but /sync etc fails. --- lib/sync.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) 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) { From d13fbd0e3e0e685d96c8c71edc78523be0e42823 Mon Sep 17 00:00:00 2001 From: David Baker Date: Mon, 14 Mar 2016 17:13:01 +0000 Subject: [PATCH 2/3] fix lint --- lib/sync.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sync.js b/lib/sync.js index b22f27ead..bdd7c0366 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -694,7 +694,7 @@ SyncApi.prototype._processSyncResponse = function(syncToken, data) { */ SyncApi.prototype._startKeepAlives = function(delay) { if (delay === undefined) { - delay = 5000 + Math.floor(Math.random() * 5000) + delay = 5000 + Math.floor(Math.random() * 5000); } if (this._keepAliveTimer !== null) { From 3752429b6555ae7a23f9449eed730b4296aa7845 Mon Sep 17 00:00:00 2001 From: David Baker Date: Mon, 14 Mar 2016 17:49:36 +0000 Subject: [PATCH 3/3] Fix the tests to tick the clock to 'wait' for sync retries. --- spec/unit/matrix-client.spec.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) 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); }; }