Skip to content

Commit

Permalink
Merge pull request #2392 from matrix-org/matthew/fix-sync-acc-overlap
Browse files Browse the repository at this point in the history
Prevent overlapping sync accumulator persists
  • Loading branch information
ara4n authored May 21, 2022
2 parents a9516d0 + c12932b commit 25afb7c
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/store/indexeddb-local-backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ export class LocalIndexedDBStoreBackend implements IIndexedDBBackend {
private db: IDBDatabase = null;
private disconnected = true;
private _isNewlyCreated = false;
private isPersisting = false;
private pendingUserPresenceData: UserTuple[] = [];

/**
* Does the actual reading from and writing to the indexeddb
Expand Down Expand Up @@ -401,11 +403,24 @@ export class LocalIndexedDBStoreBackend implements IIndexedDBBackend {
public async syncToDatabase(userTuples: UserTuple[]): Promise<void> {
const syncData = this.syncAccumulator.getJSON(true);

await Promise.all([
this.persistUserPresenceEvents(userTuples),
this.persistAccountData(syncData.accountData),
this.persistSyncData(syncData.nextBatch, syncData.roomsData),
]);
if (this.isPersisting) {
logger.warn("Skipping syncToDatabase() as persist already in flight");
this.pendingUserPresenceData.push(...userTuples);
return;
} else {
userTuples.unshift(...this.pendingUserPresenceData);
this.isPersisting = true;
}

try {
await Promise.all([
this.persistUserPresenceEvents(userTuples),
this.persistAccountData(syncData.accountData),
this.persistSyncData(syncData.nextBatch, syncData.roomsData),
]);
} finally {
this.isPersisting = false;
}
}

/**
Expand All @@ -427,7 +442,9 @@ export class LocalIndexedDBStoreBackend implements IIndexedDBBackend {
nextBatch,
roomsData,
}); // put == UPSERT
return txnAsPromise(txn).then();
return txnAsPromise(txn).then(() => {
logger.log("Persisted sync data up to", nextBatch);
});
});
}

Expand Down

0 comments on commit 25afb7c

Please sign in to comment.