Improve cold start performance

This commit is contained in:
Josh Perez 2021-03-04 16:44:57 -05:00 committed by Josh Perez
parent c73e35b1b6
commit d82ce07942
39 changed files with 911 additions and 628 deletions

View file

@ -18,32 +18,38 @@
MessageCollection: Whisper.MessageCollection,
});
await Promise.all(
messages.map(async fromDB => {
const message = MessageController.register(fromDB.id, fromDB);
const messageIds = [];
const inMemoryMessages = [];
const messageCleanup = [];
window.log.info('Message expired', {
sentAt: message.get('sent_at'),
});
messages.forEach(dbMessage => {
const message = MessageController.register(dbMessage.id, dbMessage);
messageIds.push(message.id);
inMemoryMessages.push(message);
messageCleanup.push(message.cleanup());
});
// We delete after the trigger to allow the conversation time to process
// the expiration before the message is removed from the database.
await window.Signal.Data.removeMessage(message.id, {
Message: Whisper.Message,
});
// We delete after the trigger to allow the conversation time to process
// the expiration before the message is removed from the database.
await window.Signal.Data.removeMessages(messageIds);
await Promise.all(messageCleanup);
Whisper.events.trigger(
'messageExpired',
message.id,
message.conversationId
);
inMemoryMessages.forEach(message => {
window.log.info('Message expired', {
sentAt: message.get('sent_at'),
});
const conversation = message.getConversation();
if (conversation) {
conversation.trigger('expired', message);
}
})
);
Whisper.events.trigger(
'messageExpired',
message.id,
message.conversationId
);
const conversation = message.getConversation();
if (conversation) {
conversation.trigger('expired', message);
}
});
} catch (error) {
window.log.error(
'destroyExpiredMessages: Error deleting expired messages',

View file

@ -58,7 +58,9 @@
return;
}
const nextCheck = toAgeOut.get('received_at') + THIRTY_DAYS;
const receivedAt =
toAgeOut.get('received_at_ms') || toAgeOut.get('received_at');
const nextCheck = receivedAt + THIRTY_DAYS;
Whisper.TapToViewMessagesListener.nextCheck = nextCheck;
window.log.info(

View file

@ -6,6 +6,8 @@
window.Whisper = window.Whisper || {};
const messageLookup = Object.create(null);
const msgIDsBySender = new Map();
const msgIDsBySentAt = new Map();
const SECOND = 1000;
const MINUTE = SECOND * 60;
@ -31,10 +33,18 @@
timestamp: Date.now(),
};
msgIDsBySentAt.set(message.get('sent_at'), id);
msgIDsBySender.set(message.getSenderIdentifier(), id);
return message;
}
function unregister(id) {
const { message } = messageLookup[id] || {};
if (message) {
msgIDsBySender.delete(message.getSenderIdentifier());
msgIDsBySentAt.delete(message.get('sent_at'));
}
delete messageLookup[id];
}
@ -50,7 +60,7 @@
now - timestamp > FIVE_MINUTES &&
(!conversation || !conversation.messageCollection.length)
) {
delete messageLookup[message.id];
unregister(message.id);
}
}
}
@ -60,6 +70,22 @@
return existing && existing.message ? existing.message : null;
}
function findBySentAt(sentAt) {
const id = msgIDsBySentAt.get(sentAt);
if (!id) {
return null;
}
return getById(id);
}
function findBySender(sender) {
const id = msgIDsBySender.get(sender);
if (!id) {
return null;
}
return getById(id);
}
function _get() {
return messageLookup;
}
@ -70,6 +96,8 @@
register,
unregister,
cleanup,
findBySender,
findBySentAt,
getById,
_get,
};