1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-12-01 04:43:29 +03:00

wrap backup sending in a try, and add delays

This commit is contained in:
Hubert Chathi
2018-10-10 19:31:28 -04:00
parent 258adda67c
commit da65f43983
2 changed files with 56 additions and 40 deletions

View File

@@ -171,6 +171,7 @@ describe("MegolmBackup", function() {
});
it('sends backups to the server', function () {
this.timeout(12000);
const groupSession = new global.Olm.OutboundGroupSession();
groupSession.create();
const ibGroupSession = new global.Olm.InboundGroupSession();
@@ -263,6 +264,7 @@ describe("MegolmBackup", function() {
});
it('retries when a backup fails', function () {
this.timeout(12000);
const groupSession = new global.Olm.OutboundGroupSession();
groupSession.create();
const ibGroupSession = new global.Olm.InboundGroupSession();

View File

@@ -969,15 +969,21 @@ Crypto.prototype.importRoomKeys = function(keys) {
Crypto.prototype._maybeSendKeyBackup = async function() {
if (!this._sendingBackups) {
this._sendingBackups = true;
try {
// wait between 0 and 10 seconds, to avoid backup requests from
// different clients hitting the server all at the same time when a
// new key is sent
await new Promise((resolve, reject) => {
setTimeout(resolve, Math.random() * 10000);
});
let numFailures = 0; // number of consecutive failures
while (1) {
if (!this.backupKey) {
this._sendingBackups = false;
return;
}
// FIXME: figure out what limit is reasonable
const sessions = await this._cryptoStore.getSessionsNeedingBackup(10);
if (!sessions.length) {
this._sendingBackups = false;
return;
}
const data = {};
@@ -1000,23 +1006,31 @@ Crypto.prototype._maybeSendKeyBackup = async function() {
};
}
let successful = false;
do {
if (!this.backupKey) {
this._sendingBackups = false;
return;
}
try {
await this._baseApis.sendKeyBackup(undefined, undefined, this.backupInfo.version, {rooms: data});
successful = true;
numFailures = 0;
await this._cryptoStore.unmarkSessionsNeedingBackup(sessions);
}
catch (e) {
console.log("send failed", e);
// FIXME: pause
catch (err) {
numFailures++;
console.log("send failed", err);
if (err.httpStatus === 400 || err.httpStatus === 403 || err.httpStatus === 401) {
// retrying probably won't help much, so we should give up
// FIXME: disable backups?
return;
}
} while (!successful);
// FIXME: pause between iterations?
}
if (numFailures) {
// exponential backoff if we have failures
await new Promise((resolve, reject) => {
setTimeout(resolve, 1000 * Math.pow(2, Math.min(numFailures - 1, 4)));
});
}
}
}
finally
{
this._sendingBackups = false;
}
}
}