Skip to content

Commit

Permalink
Ensure single upload for storage service
Browse files Browse the repository at this point in the history
Co-authored-by: Josh Perez <60019601+josh-signal@users.noreply.github.com>
  • Loading branch information
automated-signal and josh-signal committed Sep 16, 2021
1 parent 3c2bd55 commit c48f5aa
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions ts/services/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,18 @@ async function generateManifest(
});

if (deleteKeys.length !== pendingDeletes.size) {
const localDeletes = deleteKeys.map(key =>
redactStorageID(arrayBufferToBase64(key))
);
const remoteDeletes: Array<string> = [];
pendingDeletes.forEach(id => remoteDeletes.push(redactStorageID(id)));
window.log.error(
'Delete key sizes do not match',
'local',
localDeletes.join(','),
'remote',
remoteDeletes.join(',')
);
throw new Error('invalid write delete keys length do not match');
}
if (newItems.size !== pendingInserts.size) {
Expand Down Expand Up @@ -1031,7 +1043,9 @@ async function processRemoteRecords(
return 0;
}

async function sync(): Promise<Proto.ManifestRecord | undefined> {
async function sync(
ignoreConflicts = false
): Promise<Proto.ManifestRecord | undefined> {
if (!isStorageWriteFeatureEnabled()) {
window.log.info(
'storageService.sync: Not starting desktop.storage is falsey'
Expand Down Expand Up @@ -1077,7 +1091,7 @@ async function sync(): Promise<Proto.ManifestRecord | undefined> {
window.storage.put('manifestVersion', version);

const hasConflicts = await processManifest(manifest);
if (hasConflicts) {
if (hasConflicts && !ignoreConflicts) {
await upload(true);
}

Expand Down Expand Up @@ -1151,7 +1165,10 @@ async function upload(fromSync = false): Promise<void> {
// Syncing before we upload so that we repair any unknown records and
// records with errors as well as ensure that we have the latest up to date
// manifest.
previousManifest = await sync();
// We are going to upload after this sync so we can ignore any conflicts
// that arise during the sync.
const ignoreConflicts = true;
previousManifest = await sync(ignoreConflicts);
}

const localManifestVersion = window.storage.get('manifestVersion', 0);
Expand Down

0 comments on commit c48f5aa

Please sign in to comment.