Skip to content

Commit

Permalink
Change initial key backup to background
Browse files Browse the repository at this point in the history
Alters the APIs used for initial key backup so that the actual upload happens in
the background after all session are marked for backup.
  • Loading branch information
jryans committed Jan 9, 2019
1 parent f60575b commit 90ff25c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -997,12 +997,16 @@ MatrixClient.prototype.sendKeyBackup = function(roomId, sessionId, version, data
);
};

MatrixClient.prototype.backupAllGroupSessions = function(version) {
/**
* Marks all group sessions as needing to be backed up and schedules them to
* upload in the background as soon as possible.
*/
MatrixClient.prototype.scheduleAllGroupSessionsForBackup = async function() {
if (this._crypto === null) {
throw new Error("End-to-end encryption disabled");
}

return this._crypto.backupAllGroupSessions(version);
await this._crypto.scheduleAllGroupSessionsForBackup();
};

MatrixClient.prototype.isValidRecoveryKey = function(recoveryKey) {
Expand Down
20 changes: 12 additions & 8 deletions src/crypto/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -988,15 +988,17 @@ Crypto.prototype.importRoomKeys = function(keys) {
/**
* Schedules sending all keys waiting to be sent to the backup, if not already
* scheduled. Retries if necessary.
*
* @param {number} maxDelay Maximum delay to wait in ms. 0 means no delay.
*/
Crypto.prototype.scheduleKeyBackupSend = async function() {
Crypto.prototype.scheduleKeyBackupSend = async function(maxDelay = 10000) {
if (this._sendingBackups) return;

try {
// wait between 0 and 10 seconds, to avoid backup
// wait between 0 and `maxDelay` seconds, to avoid backup
// requests from different clients hitting the server all at
// the same time when a new key is sent
const delay = Math.random() * 10000;
const delay = Math.random() * maxDelay;
await Promise.delay(delay);
let numFailures = 0; // number of consecutive failures
while (1) {
Expand Down Expand Up @@ -1124,7 +1126,11 @@ Crypto.prototype.backupGroupSession = async function(
this.scheduleKeyBackupSend();
};

Crypto.prototype.backupAllGroupSessions = async function(version) {
/**
* Marks all group sessions as needing to be backed up and schedules them to
* upload in the background as soon as possible.
*/
Crypto.prototype.scheduleAllGroupSessionsForBackup = async function() {
const remaining = await this._cryptoStore.doTxn(
'readwrite',
[
Expand All @@ -1142,10 +1148,8 @@ Crypto.prototype.backupAllGroupSessions = async function(version) {
);
this.emit("crypto.keyBackupSessionsRemaining", remaining);

let numKeysBackedUp;
do {
numKeysBackedUp = await this._backupPendingKeys(KEY_BACKUP_KEYS_PER_REQUEST);
} while (numKeysBackedUp > 0);
// Schedule keys to upload in the background as soon as possible.
this.scheduleKeyBackupSend(0 /* maxDelay */);
};

/* eslint-disable valid-jsdoc */ //https://github.com/eslint/eslint/issues/7307
Expand Down

0 comments on commit 90ff25c

Please sign in to comment.