Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch tracked users setup #40

Merged
merged 3 commits into from
Dec 4, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/e2ee/RustEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export const SYNC_LOCK_NAME = "sync";
export class RustEngine {
public readonly lock = new AsyncLock();

public readonly trackedUsersToAdd = new Set<string>();
public addTrackedUsersPromise: Promise<void>|undefined;

private keyBackupVersion: KeyBackupVersion|undefined;
private keyBackupWaiter = Promise.resolve();

Expand Down Expand Up @@ -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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth avoiding creating an extra array with the spread syntax.

Something like this should work:

Suggested change
const uids = [...this.trackedUsersToAdd].map(u => new UserId(u));
const uids = Array<UserId>(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);

const keysClaim = await this.machine.getMissingSessions(uids);
Expand Down
Loading