From 9cfb3c8e9503b939f13771b39e520ce26c24c768 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Mon, 9 Nov 2020 16:49:16 +0100 Subject: [PATCH] only check to pre-share new megolm session every minute --- src/matrix/e2ee/RoomEncryption.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/matrix/e2ee/RoomEncryption.js b/src/matrix/e2ee/RoomEncryption.js index 94522e60..4829a8d7 100644 --- a/src/matrix/e2ee/RoomEncryption.js +++ b/src/matrix/e2ee/RoomEncryption.js @@ -20,6 +20,10 @@ import {mergeMap} from "../../utils/mergeMap.js"; import {makeTxnId} from "../common.js"; const ENCRYPTED_TYPE = "m.room.encrypted"; +// how often ensureMessageKeyIsShared can check if it needs to +// create a new outbound session +// note that encrypt could still create a new session +const MIN_PRESHARE_INTERVAL = 60 * 1000; // 1min function encodeMissingSessionKey(senderKey, sessionId) { return `${senderKey}|${sessionId}`; @@ -55,6 +59,7 @@ export class RoomEncryption { this._clock = clock; this._disposed = false; this._isFlushingRoomKeyShares = false; + this._lastKeyPreShareTime = null; } async enableSessionBackup(sessionBackup) { @@ -248,6 +253,10 @@ export class RoomEncryption { /** shares the encryption key for the next message if needed */ async ensureMessageKeyIsShared(hsApi) { + if (this._lastKeyPreShareTime && this._lastKeyPreShareTime.measure() < MIN_PRESHARE_INTERVAL) { + return; + } + this._lastKeyPreShareTime = this._clock.createMeasure(); const roomKeyMessage = await this._megolmEncryption.ensureOutboundSession(this._room.id, this._encryptionParams); if (roomKeyMessage) { await this._deviceTracker.trackRoom(this._room);