You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-28 05:03:59 +03:00
Change initial key backup to background
Alters the APIs used for initial key backup so that the actual upload happens in the background after all session are marked for backup.
This commit is contained in:
@@ -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) {
|
if (this._crypto === null) {
|
||||||
throw new Error("End-to-end encryption disabled");
|
throw new Error("End-to-end encryption disabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._crypto.backupAllGroupSessions(version);
|
await this._crypto.scheduleAllGroupSessionsForBackup();
|
||||||
};
|
};
|
||||||
|
|
||||||
MatrixClient.prototype.isValidRecoveryKey = function(recoveryKey) {
|
MatrixClient.prototype.isValidRecoveryKey = function(recoveryKey) {
|
||||||
|
|||||||
@@ -988,15 +988,17 @@ Crypto.prototype.importRoomKeys = function(keys) {
|
|||||||
/**
|
/**
|
||||||
* Schedules sending all keys waiting to be sent to the backup, if not already
|
* Schedules sending all keys waiting to be sent to the backup, if not already
|
||||||
* scheduled. Retries if necessary.
|
* 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;
|
if (this._sendingBackups) return;
|
||||||
|
|
||||||
try {
|
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
|
// requests from different clients hitting the server all at
|
||||||
// the same time when a new key is sent
|
// the same time when a new key is sent
|
||||||
const delay = Math.random() * 10000;
|
const delay = Math.random() * maxDelay;
|
||||||
await Promise.delay(delay);
|
await Promise.delay(delay);
|
||||||
let numFailures = 0; // number of consecutive failures
|
let numFailures = 0; // number of consecutive failures
|
||||||
while (1) {
|
while (1) {
|
||||||
@@ -1118,7 +1120,11 @@ Crypto.prototype.backupGroupSession = async function(
|
|||||||
this.scheduleKeyBackupSend();
|
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() {
|
||||||
await this._cryptoStore.doTxn(
|
await this._cryptoStore.doTxn(
|
||||||
'readwrite',
|
'readwrite',
|
||||||
[
|
[
|
||||||
@@ -1137,10 +1143,8 @@ Crypto.prototype.backupAllGroupSessions = async function(version) {
|
|||||||
const remaining = await this._cryptoStore.countSessionsNeedingBackup();
|
const remaining = await this._cryptoStore.countSessionsNeedingBackup();
|
||||||
this.emit("crypto.keyBackupSessionsRemaining", remaining);
|
this.emit("crypto.keyBackupSessionsRemaining", remaining);
|
||||||
|
|
||||||
let numKeysBackedUp;
|
// Schedule keys to upload in the background as soon as possible.
|
||||||
do {
|
this.scheduleKeyBackupSend(0 /* maxDelay */);
|
||||||
numKeysBackedUp = await this._backupPendingKeys(KEY_BACKUP_KEYS_PER_REQUEST);
|
|
||||||
} while (numKeysBackedUp > 0);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* eslint-disable valid-jsdoc */ //https://github.com/eslint/eslint/issues/7307
|
/* eslint-disable valid-jsdoc */ //https://github.com/eslint/eslint/issues/7307
|
||||||
|
|||||||
Reference in New Issue
Block a user