From 471842d4f1571284f77529889d5c3ea283fe06e7 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Fri, 24 Nov 2023 18:15:11 +0000 Subject: [PATCH 1/3] Batch tracked users setup --- src/e2ee/RustEngine.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/e2ee/RustEngine.ts b/src/e2ee/RustEngine.ts index 8239b79c..03f5afaf 100644 --- a/src/e2ee/RustEngine.ts +++ b/src/e2ee/RustEngine.ts @@ -33,6 +33,9 @@ export const SYNC_LOCK_NAME = "sync"; export class RustEngine { public readonly lock = new AsyncLock(); + public readonly trackedUsersToAdd = new Set(); + public addTrackedUsersPromise: Promise|undefined; + private keyBackupVersion: KeyBackupVersion|undefined; private keyBackupWaiter = Promise.resolve(); @@ -80,8 +83,18 @@ export class RustEngine { } public async addTrackedUsers(userIds: string[]) { - await this.lock.acquire(SYNC_LOCK_NAME, async () => { - const uids = userIds.map(u => new UserId(u)); + // Add the new set of users to the pool + userIds.forEach(uId => this.trackedUsersToAdd.add(uId)); + if (this.addTrackedUsersPromise) { + // If we have a pending promise, don't create another lock requirement. + return; + } + return this.addTrackedUsersPromise = this.lock.acquire(SYNC_LOCK_NAME, async () => { + // Immediately clear this promise so that a new promise is queued up. + this.addTrackedUsersPromise = undefined; + const uids = [...this.trackedUsersToAdd].map(u => new UserId(u)); + // Clear the existing pool + this.trackedUsersToAdd.clear(); await this.machine.updateTrackedUsers(uids); const keysClaim = await this.machine.getMissingSessions(uids); From f656aba3c61f2ba81faff8c07926c62b4b2c209d Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Mon, 4 Dec 2023 10:36:00 +0000 Subject: [PATCH 2/3] Save an iteration --- src/e2ee/RustEngine.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/e2ee/RustEngine.ts b/src/e2ee/RustEngine.ts index 03f5afaf..60e18843 100644 --- a/src/e2ee/RustEngine.ts +++ b/src/e2ee/RustEngine.ts @@ -92,7 +92,11 @@ export class RustEngine { return this.addTrackedUsersPromise = this.lock.acquire(SYNC_LOCK_NAME, async () => { // Immediately clear this promise so that a new promise is queued up. this.addTrackedUsersPromise = undefined; - const uids = [...this.trackedUsersToAdd].map(u => new UserId(u)); + const uids = Array(this.trackedUsersToAdd.size); + let idx = 0; + for (const u of this.trackedUsersToAdd.values()) { + uids[idx++] = new UserId(u); + } // Clear the existing pool this.trackedUsersToAdd.clear(); await this.machine.updateTrackedUsers(uids); From e95019df733c10f7a1e30c1f9ba310ea6254b93b Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Mon, 4 Dec 2023 10:36:12 +0000 Subject: [PATCH 3/3] Add new --- src/e2ee/RustEngine.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/e2ee/RustEngine.ts b/src/e2ee/RustEngine.ts index 60e18843..d9466f78 100644 --- a/src/e2ee/RustEngine.ts +++ b/src/e2ee/RustEngine.ts @@ -92,7 +92,7 @@ export class RustEngine { return this.addTrackedUsersPromise = this.lock.acquire(SYNC_LOCK_NAME, async () => { // Immediately clear this promise so that a new promise is queued up. this.addTrackedUsersPromise = undefined; - const uids = Array(this.trackedUsersToAdd.size); + const uids = new Array(this.trackedUsersToAdd.size); let idx = 0; for (const u of this.trackedUsersToAdd.values()) { uids[idx++] = new UserId(u);