From 4ca5ff9b9f2f4b9095e3a70e19a94ed718841251 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Tue, 8 Sep 2020 18:30:06 +0200 Subject: [PATCH] only load 50 olm sessions at once --- src/matrix/e2ee/olm/Encryption.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/matrix/e2ee/olm/Encryption.js b/src/matrix/e2ee/olm/Encryption.js index 680ce154..eb291c40 100644 --- a/src/matrix/e2ee/olm/Encryption.js +++ b/src/matrix/e2ee/olm/Encryption.js @@ -29,6 +29,9 @@ function findFirstSessionId(sessionIds) { } const OTK_ALGORITHM = "signed_curve25519"; +// only encrypt this amount of olm messages at once otherwise we run out of wasm memory +// with all the sessions loaded at the same time +const MAX_BATCH_SIZE = 50; export class Encryption { constructor({account, olm, olmUtil, ownUserId, storage, now, pickleKey, senderKeyLock}) { @@ -43,6 +46,16 @@ export class Encryption { } async encrypt(type, content, devices, hsApi) { + let messages = []; + for (let i = 0; i < devices.length ; i += MAX_BATCH_SIZE) { + const batchDevices = devices.slice(i, i + MAX_BATCH_SIZE); + const batchMessages = await this._encryptForMaxDevices(type, content, batchDevices, hsApi); + messages = messages.concat(batchMessages); + } + return messages; + } + + async _encryptForMaxDevices(type, content, devices, hsApi) { // TODO: see if we can only hold some of the locks until after the /keys/claim call (if needed) // take a lock on all senderKeys so decryption and other calls to encrypt (should not happen) // don't modify the sessions at the same time