Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't emit ERROR until a keepalive poke fails #223

Merged
merged 3 commits into from
Oct 7, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/http-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ module.exports.MatrixHttpApi.prototype = {
*/
request: function(callback, method, path, queryParams, data, opts) {
opts = opts || {};
var prefix = opts.prefix || this.opts.prefix;
var prefix = opts.prefix !== undefined ? opts.prefix : this.opts.prefix;
var fullUri = this.opts.baseUrl + prefix + path;

return this.requestOtherUrl(
Expand Down
45 changes: 34 additions & 11 deletions lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ SyncApi.prototype.sync = function() {

self._sync({ filterId: filterId });
}, function(err) {
self._startKeepAlives().done(function() {
self._startKeepAlives(0).done(function() {
getFilter();
});
self._updateSyncState("ERROR", { error: err });
Expand Down Expand Up @@ -546,12 +546,17 @@ SyncApi.prototype._sync = function(syncOptions) {
console.error(err);

debuglog("Starting keep-alive");
self._syncConnectionLost = true;
self._startKeepAlives().done(function() {
// Note that we do *not* mark the sync connection as
// lost yet: we only do this if a keepalive poke
// fails, since long lived HTTP connections will
// go away sometimes and we shouldn't treat this as
// erroneous.
// We sent the first keep alive immediately because
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sent -> send

// of this.
self._startKeepAlives(0).done(function() {
self._sync(syncOptions);
});
self._currentSyncRequest = null;
self._updateSyncState("ERROR", { error: err });
});
};

Expand Down Expand Up @@ -851,10 +856,14 @@ SyncApi.prototype._startKeepAlives = function(delay) {
clearTimeout(this._keepAliveTimer);
}
var self = this;
self._keepAliveTimer = setTimeout(
self._pokeKeepAlive.bind(self),
delay
);
if (delay > 0) {
self._keepAliveTimer = setTimeout(
self._pokeKeepAlive.bind(self),
delay
);
} else {
self._pokeKeepAlive();
}
if (!this._connectionReturnedDefer) {
this._connectionReturnedDefer = q.defer();
}
Expand All @@ -874,9 +883,15 @@ SyncApi.prototype._pokeKeepAlive = function() {
}
}

this.client._http.requestWithPrefix(
undefined, "GET", "/_matrix/client/versions", undefined,
undefined, "", 15 * 1000
this.client._http.request(
undefined, // callback
"GET", "/_matrix/client/versions",
undefined, // queryParams
undefined, // data
{
prefix: '',
localTimeoutMs: 15 * 1000,
}
).done(function() {
success();
}, function(err) {
Expand All @@ -892,6 +907,14 @@ SyncApi.prototype._pokeKeepAlive = function() {
self._pokeKeepAlive.bind(self),
5000 + Math.floor(Math.random() * 5000)
);
// If we haven't already marked this sync
// connection as gone-away, do so now and
// emit an error.
// Note we do this after setting the timer:
// this lets the unit tests advance the mock
// clock when the get the error.
self._syncConnectionLost = true;
self._updateSyncState("ERROR", { error: err });
}
});
};
Expand Down
24 changes: 21 additions & 3 deletions spec/unit/matrix-client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ describe("MatrixClient", function() {
// }
// items are popped off when processed and block if no items left.
];
var accept_keepalives;
var pendingLookup = null;
function httpReq(cb, method, path, qp, data, prefix) {
if (path === KEEP_ALIVE_PATH) {
if (path === KEEP_ALIVE_PATH && accept_keepalives) {
return q();
}
var next = httpLookups.shift();
Expand Down Expand Up @@ -143,8 +144,10 @@ describe("MatrixClient", function() {
client._http.authedRequest.andCallFake(httpReq);
client._http.authedRequestWithPrefix.andCallFake(httpReq);
client._http.requestWithPrefix.andCallFake(httpReq);
client._http.request.andCallFake(httpReq);

// set reasonable working defaults
accept_keepalives = true;
pendingLookup = null;
httpLookups = [];
httpLookups.push(PUSH_RULES_RESPONSE);
Expand Down Expand Up @@ -329,12 +332,19 @@ describe("MatrixClient", function() {
it("should transition ERROR -> PREPARED after /sync if prev failed",
function(done) {
var expectedStates = [];
accept_keepalives = false;
httpLookups = [];
httpLookups.push(PUSH_RULES_RESPONSE);
httpLookups.push(FILTER_RESPONSE);
httpLookups.push({
method: "GET", path: "/sync", error: { errcode: "NOPE_NOPE_NOPE" }
});
httpLookups.push({
method: "GET", path: KEEP_ALIVE_PATH, error: { errcode: "KEEPALIVE_FAIL" }
});
httpLookups.push({
method: "GET", path: KEEP_ALIVE_PATH, data: {}
});
httpLookups.push({
method: "GET", path: "/sync", data: SYNC_DATA
});
Expand All @@ -354,10 +364,14 @@ describe("MatrixClient", function() {
});

it("should transition SYNCING -> ERROR after a failed /sync", function(done) {
accept_keepalives = false;
var expectedStates = [];
httpLookups.push({
method: "GET", path: "/sync", error: { errcode: "NONONONONO" }
});
httpLookups.push({
method: "GET", path: KEEP_ALIVE_PATH, error: { errcode: "KEEPALIVE_FAIL" }
});

expectedStates.push(["PREPARED", null]);
expectedStates.push(["SYNCING", "PREPARED"]);
Expand Down Expand Up @@ -394,13 +408,17 @@ describe("MatrixClient", function() {
client.startClient();
});

it("should transition ERROR -> ERROR if multiple /sync fails", function(done) {
it("should transition ERROR -> ERROR if keepalive keeps failing", function(done) {
accept_keepalives = false;
var expectedStates = [];
httpLookups.push({
method: "GET", path: "/sync", error: { errcode: "NONONONONO" }
});
httpLookups.push({
method: "GET", path: "/sync", error: { errcode: "NONONONONO" }
method: "GET", path: KEEP_ALIVE_PATH, error: { errcode: "KEEPALIVE_FAIL" }
});
httpLookups.push({
method: "GET", path: KEEP_ALIVE_PATH, error: { errcode: "KEEPALIVE_FAIL" }
});

expectedStates.push(["PREPARED", null]);
Expand Down