perf: reduce message delivery latency across all connection scenarios (#239)
perf: reduce message delivery latency across all connection scenarios (#239)
This commit is contained in:
+6
-6
@@ -102,14 +102,14 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
/** this mutex ensures that messages from the same chat are processed in order, while allowing parallel processing of messages from different chats */
|
||||
const messageMutex = makeKeyedMutex()
|
||||
|
||||
/** this mutex ensures that receipts are processed in order */
|
||||
const receiptMutex = makeMutex()
|
||||
/** this mutex ensures that receipts from the same chat are processed in order, while allowing parallel processing across chats */
|
||||
const receiptMutex = makeKeyedMutex()
|
||||
|
||||
/** this mutex ensures that app state patches are processed in order */
|
||||
const appStatePatchMutex = makeMutex()
|
||||
|
||||
/** this mutex ensures that notifications are processed in order */
|
||||
const notificationMutex = makeMutex()
|
||||
/** this mutex ensures that notifications from the same chat are processed in order, while allowing parallel processing across chats */
|
||||
const notificationMutex = makeKeyedMutex()
|
||||
|
||||
// Timeout for AwaitingInitialSync state
|
||||
let awaitingSyncTimeout: NodeJS.Timeout | undefined
|
||||
@@ -1495,7 +1495,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
|
||||
awaitingSyncTimeout = setTimeout(() => {
|
||||
if (syncState === SyncState.AwaitingInitialSync) {
|
||||
logger.warn('Timeout in AwaitingInitialSync (4s), forcing state to Online and flushing buffer')
|
||||
logger.warn('Timeout in AwaitingInitialSync (2s), forcing state to Online and flushing buffer')
|
||||
syncState = SyncState.Online
|
||||
ev.flush()
|
||||
|
||||
@@ -1505,7 +1505,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
const accountSyncCounter = (authState.creds.accountSyncCounter || 0) + 1
|
||||
ev.emit('creds.update', { accountSyncCounter })
|
||||
}
|
||||
}, 4_000)
|
||||
}, 2_000)
|
||||
})
|
||||
|
||||
// When an app state sync key arrives (myAppStateKeyId is set) and there are
|
||||
|
||||
@@ -1282,7 +1282,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
|
||||
try {
|
||||
await Promise.all([
|
||||
receiptMutex.mutex(async () => {
|
||||
receiptMutex.mutex(jidNormalizedUser(remoteJid) || 'unknown', async () => {
|
||||
const status = getStatusFromReceiptType(attrs.type)
|
||||
if (
|
||||
typeof status !== 'undefined' &&
|
||||
@@ -1356,7 +1356,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
|
||||
try {
|
||||
await Promise.all([
|
||||
notificationMutex.mutex(async () => {
|
||||
notificationMutex.mutex(jidNormalizedUser(remoteJid) || 'unknown', async () => {
|
||||
const msg = await processNotification(node)
|
||||
if (msg) {
|
||||
const fromMe = areJidsSameUser(node.attrs.participant || remoteJid, authState.creds.me!.id)
|
||||
@@ -1985,7 +1985,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
let isProcessing = false
|
||||
|
||||
// Number of nodes to process before yielding to event loop
|
||||
const BATCH_SIZE = 10
|
||||
const BATCH_SIZE = 25
|
||||
|
||||
const enqueue = (type: MessageType, node: BinaryNode) => {
|
||||
nodes.push({ type, node })
|
||||
|
||||
@@ -1667,8 +1667,8 @@ export const makeSocket = (config: SocketConfig) => {
|
||||
// already-authenticated session (no QR re-scan needed). In this case we skip
|
||||
// the offline buffer entirely so live messages are not held hostage waiting for the
|
||||
// server to finish flushing the pending-message backlog (CB:ib,,offline).
|
||||
// For normal restarts (no stale routingInfo) the standard 5 s safety cap applies.
|
||||
const OFFLINE_BUFFER_TIMEOUT_MS = 5_000
|
||||
// For normal restarts (no stale routingInfo) the standard 2 s safety cap applies.
|
||||
const OFFLINE_BUFFER_TIMEOUT_MS = 2_000
|
||||
let offlineBufferTimeout: NodeJS.Timeout | undefined
|
||||
|
||||
process.nextTick(() => {
|
||||
|
||||
Reference in New Issue
Block a user