1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-09-01 21:21:58 +03:00

Merge branch 'develop' into push-rules-settings

# Conflicts:
#	lib/sync.js
This commit is contained in:
manuroe
2016-01-18 17:33:13 +01:00
parent 2e664adb32
commit 80f7220a7b
7 changed files with 255 additions and 29 deletions

View File

@@ -30,6 +30,8 @@ var utils = require("./utils");
var httpApi = require("./http-api");
var Filter = require("./filter");
var DEBUG = true;
// /sync requests allow you to set a timeout= but the request may continue
// beyond that and wedge forever, so we need to track how long we are willing
// to keep open the connection. This constant is *ADDED* to the timeout= value
@@ -42,6 +44,10 @@ function getFilterName(userId, suffix) {
return "FILTER_SYNC_" + userId + (suffix ? "_" + suffix : "");
}
function debuglog() {
if (!DEBUG) { return; }
console.log.apply(console, arguments);
}
/**
@@ -60,6 +66,7 @@ function SyncApi(client, opts) {
opts.pendingEventOrdering = opts.pendingEventOrdering || "chronological";
this.opts = opts;
this._peekRoomId = null;
this._lowClientTimeouts = false;
}
/**
@@ -107,7 +114,7 @@ SyncApi.prototype.syncLeftRooms = function() {
var localTimeoutMs = this.opts.pollTimeout + BUFFER_PERIOD_MS;
var qps = {
timeout: 1 // don't want to block since this is a single isolated req
timeout: 0 // don't want to block since this is a single isolated req
};
return this._getOrCreateFilter(
@@ -139,7 +146,8 @@ SyncApi.prototype.syncLeftRooms = function() {
return;
}
leaveObj.timeline = leaveObj.timeline || {};
var timelineEvents = self._mapSyncEventsFormat(leaveObj.timeline, room);
var timelineEvents =
self._mapSyncEventsFormat(leaveObj.timeline, room);
var stateEvents = self._mapSyncEventsFormat(leaveObj.state, room);
var paginationToken = (
leaveObj.timeline.limited ? leaveObj.timeline.prev_batch : null
@@ -223,7 +231,7 @@ SyncApi.prototype.stopPeeking = function() {
*/
SyncApi.prototype._peekPoll = function(roomId, token) {
if (this._peekRoomId !== roomId) {
console.log("Stopped peeking in room %s", roomId);
debuglog("Stopped peeking in room %s", roomId);
return;
}
@@ -253,7 +261,7 @@ SyncApi.prototype._peekPoll = function(roomId, token) {
* Main entry point
*/
SyncApi.prototype.sync = function() {
console.log("SyncApi.sync");
debuglog("SyncApi.sync");
var client = this.client;
var self = this;
@@ -284,7 +292,7 @@ SyncApi.prototype.sync = function() {
self._getOrCreateFilter(
getFilterName(client.credentials.userId), filter
).done(function(filterId) {
console.log("Using existing filter ID %s", filterId);
debuglog("Using existing filter ID %s", filterId);
self._sync({ filterId: filterId });
}, retryHandler(attempt, getFilter));
}
@@ -334,14 +342,32 @@ SyncApi.prototype._sync = function(syncOptions, attempt) {
if (attempt > 1) {
// we think the connection is dead. If it comes back up, we won't know
// about it till /sync returns. If the timeout= is high, this could
// be a long time. Set it to 1 when doing retries.
qps.timeout = 1;
// be a long time. Set it to 0 when doing retries.
qps.timeout = 0;
}
// normal timeout= plus buffer time
var clientSideTimeoutMs = this.opts.pollTimeout + BUFFER_PERIOD_MS;
if (self._lowClientTimeouts) {
debuglog("_lowClientTimeouts flag set.");
clientSideTimeoutMs = this.opts.pollTimeout;
}
var dateStamp = new Date();
dateStamp = dateStamp.getHours() + ":" + dateStamp.getMinutes() + ":" +
dateStamp.getSeconds() + "." + dateStamp.getMilliseconds();
debuglog("DEBUG[%s]: NEW _sync attempt=%s qp_timeout=%s cli_timeout=%s",
dateStamp, attempt, qps.timeout, clientSideTimeoutMs);
client._http.authedRequestWithPrefix(
undefined, "GET", "/sync", qps, undefined, httpApi.PREFIX_V2_ALPHA,
this.opts.pollTimeout + BUFFER_PERIOD_MS // normal timeout= plus buffer time
clientSideTimeoutMs
).done(function(data) {
debuglog("DEBUG[%s]: _sync RECV", dateStamp);
self._lowClientTimeouts = false;
// data looks like:
// {
// next_batch: $token,
@@ -420,7 +446,8 @@ SyncApi.prototype._sync = function(syncOptions, attempt) {
// Handle invites
inviteRooms.forEach(function(inviteObj) {
var room = inviteObj.room;
var stateEvents = self._mapSyncEventsFormat(inviteObj.invite_state, room);
var stateEvents =
self._mapSyncEventsFormat(inviteObj.invite_state, room);
self._processRoomEvents(room, stateEvents);
if (inviteObj.isBrandNewRoom) {
room.recalculate(client.credentials.userId);
@@ -458,8 +485,15 @@ SyncApi.prototype._sync = function(syncOptions, attempt) {
self._processRoomEvents(
room, stateEvents, timelineEvents, paginationToken
);
// XXX: should we be adding ephemeralEvents to the timeline?
// It feels like that for symmetry with room.addAccountData()
// there should be a room.addEphemeralEvents() or similar.
room.addEvents(ephemeralEvents);
room.addEvents(accountDataEvents);
// we deliberately don't add accountData to the timeline
room.addAccountData(accountDataEvents);
room.recalculate(client.credentials.userId);
if (joinObj.isBrandNewRoom) {
client.store.storeRoom(room);
@@ -475,7 +509,8 @@ SyncApi.prototype._sync = function(syncOptions, attempt) {
leaveRooms.forEach(function(leaveObj) {
// Do the bear minimum to register rejected invites / you leaving rooms
var room = leaveObj.room;
var timelineEvents = self._mapSyncEventsFormat(leaveObj.timeline, room);
var timelineEvents =
self._mapSyncEventsFormat(leaveObj.timeline, room);
room.addEvents(timelineEvents);
timelineEvents.forEach(function(e) { client.emit("event", e); });
});
@@ -496,10 +531,16 @@ SyncApi.prototype._sync = function(syncOptions, attempt) {
self._sync(syncOptions);
}, function(err) {
debuglog("DEBUG[%s]: RECV FAIL %s", dateStamp, require("util").inspect(err));
console.error("/sync error (%s attempts): %s", attempt, err);
console.error(err);
attempt += 1;
startSyncingRetryTimer(client, attempt, function(newAttempt) {
startSyncingRetryTimer(client, attempt, function(newAttempt, extendedWait) {
debuglog("DEBUG[%s]: Init new _sync new_attempt=%s extended_wait=%s",
dateStamp, newAttempt, extendedWait);
if (extendedWait) {
self._lowClientTimeouts = true;
}
self._sync(syncOptions, newAttempt);
});
updateSyncState(client, "ERROR", { error: err });
@@ -694,17 +735,19 @@ function startSyncingRetryTimer(client, attempt, fn) {
client._syncingRetry.timeoutId = setTimeout(function() {
var timeAfterWaitingMs = Date.now();
var timeDeltaMs = timeAfterWaitingMs - timeBeforeWaitingMs;
var extendedWait = false;
if (timeDeltaMs > (2 * timeToWaitMs)) {
// we've waited more than twice what we were supposed to. Reset the
// attempt number back to 1. This can happen when the comp goes to
// sleep whilst the timer is running.
newAttempt = 1;
extendedWait = true;
console.warn(
"Sync retry timer: Tried to wait %s ms but actually waited %s ms",
timeToWaitMs, timeDeltaMs
);
}
fn(newAttempt);
fn(newAttempt, extendedWait);
}, timeToWaitMs);
}