Skip to content

Commit

Permalink
Process read syncs after batches of messages
Browse files Browse the repository at this point in the history
Co-authored-by: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com>
  • Loading branch information
automated-signal and indutny-signal committed Jul 30, 2021
1 parent e23eb38 commit 8d49a6a
Showing 1 changed file with 43 additions and 22 deletions.
65 changes: 43 additions & 22 deletions ts/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,12 @@ export async function startApp(): Promise<void> {
window.Signal.Services.lightSessionResetQueue = lightSessionResetQueue;
lightSessionResetQueue.pause();

const readSyncQueue = new window.PQueue({
concurrency: 1,
timeout: 1000 * 60 * 2,
});
readSyncQueue.pause();

window.Whisper.deliveryReceiptQueue = new window.PQueue({
concurrency: 1,
timeout: 1000 * 60 * 2,
Expand Down Expand Up @@ -2478,6 +2484,8 @@ export async function startApp(): Promise<void> {
window.waitForEmptyEventQueue = waitForEmptyEventQueue;

async function onEmpty() {
window.Signal.Util.setBatchingStrategy(false);

await Promise.all([
window.waitForAllBatchers(),
window.flushAllWaitBatchers(),
Expand All @@ -2496,6 +2504,7 @@ export async function startApp(): Promise<void> {

profileKeyResponseQueue.start();
lightSessionResetQueue.start();
readSyncQueue.start();
window.Whisper.deliveryReceiptQueue.start();
window.Whisper.Notifications.enable();

Expand All @@ -2513,8 +2522,6 @@ export async function startApp(): Promise<void> {
);
}

window.Signal.Util.setBatchingStrategy(false);

const attachmentDownloadQueue = window.attachmentDownloadQueue || [];

// NOTE: ts/models/messages.ts expects this global to become undefined
Expand Down Expand Up @@ -3777,29 +3784,43 @@ export async function startApp(): Promise<void> {
uuid: senderUuid,
});

window.log.info(
'read sync',
sender,
senderUuid,
envelopeTimestamp,
senderId,
'for message',
timestamp
);
if (readSyncQueue.isPaused) {
window.log.info(
'delaying processing of read sync',
sender,
senderUuid,
envelopeTimestamp,
senderId,
'for message',
timestamp
);
}

const receipt = ReadSyncs.getSingleton().add({
senderId,
sender,
senderUuid,
timestamp,
readAt,
});
readSyncQueue.add(() => {
window.log.info(
'read sync',
sender,
senderUuid,
envelopeTimestamp,
senderId,
'for message',
timestamp
);

receipt.on('remove', ev.confirm);
const receipt = ReadSyncs.getSingleton().add({
senderId,
sender,
senderUuid,
timestamp,
readAt,
});

receipt.on('remove', ev.confirm);

// Note: Here we wait, because we want read states to be in the database
// before we move on.
return ReadSyncs.getSingleton().onReceipt(receipt);
// Note: Here we wait, because we want read states to be in the database
// before we move on.
return ReadSyncs.getSingleton().onReceipt(receipt);
});
}

async function onVerified(ev: VerifiedEvent) {
Expand Down

0 comments on commit 8d49a6a

Please sign in to comment.