1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-16 18:21:59 +03:00

Implement a keep-alive timer for /sync requests

When a /sync request fails, we spin up a keep-alive poll to /_matrix/client/r0
which 400s. We treat any HTTP response code as a success for the purposes of
polling the server. When a successful poll is done, we shoot the current /sync
request in the head immediately (via a hacky abort() on the promise) and retry
the /sync.
This commit is contained in:
Kegan Dougal
2016-01-21 17:17:27 +00:00
parent ff990914b2
commit 197144dcda
2 changed files with 55 additions and 4 deletions

View File

@@ -43,6 +43,8 @@ module.exports.PREFIX_V2_ALPHA = "/_matrix/client/v2_alpha";
*/
module.exports.PREFIX_IDENTITY_V1 = "/_matrix/identity/api/v1";
module.exports.PREFIX_R0 = "/_matrix/client/r0";
/**
* Construct a MatrixHttpApi.
* @constructor
@@ -416,8 +418,10 @@ module.exports.MatrixHttpApi.prototype = {
}, localTimeoutMs);
}
var reqPromise = defer.promise;
try {
this.opts.request(
var req = this.opts.request(
{
uri: uri,
method: method,
@@ -425,6 +429,7 @@ module.exports.MatrixHttpApi.prototype = {
qs: queryParams,
body: data,
json: true,
timeout: localTimeoutMs,
_matrix_opts: this.opts
},
function(err, response, body) {
@@ -438,6 +443,9 @@ module.exports.MatrixHttpApi.prototype = {
handlerFn(err, response, body);
}
);
// FIXME: This is EVIL, but I can't think of a better way to expose
// abort() operations on underlying HTTP requests :(
reqPromise.abort = req.abort.bind(req);
}
catch (ex) {
defer.reject(ex);
@@ -445,7 +453,7 @@ module.exports.MatrixHttpApi.prototype = {
callback(ex);
}
}
return defer.promise;
return reqPromise;
}
};