feat: Mutex redesign - isolate event processing for better ordering and throughput (#2137)

* fix: improve message resend logic by adding checks for message IDs

* Revert "fix: improve message resend logic by adding checks for message IDs"

This reverts commit c03f9d8e6fc6cbfbb9d1f8f67c169700e704213d.

* fix: introduce separate mutexes for message, receipt, app state patch, and notification processing

* fix: remove unused processing mutex for notifications
This commit is contained in:
Matheus Filype
2025-12-11 23:34:59 -03:00
committed by GitHub
parent 8e93934eb7
commit 829fa8d601
3 changed files with 25 additions and 10 deletions
+17 -4
View File
@@ -72,8 +72,18 @@ export const makeChatsSocket = (config: SocketConfig) => {
let privacySettings: { [_: string]: string } | undefined
let syncState: SyncState = SyncState.Connecting
/** this mutex ensures that the notifications (receipts, messages etc.) are processed in order */
const processingMutex = makeMutex()
/** this mutex ensures that messages are processed in order */
const messageMutex = makeMutex()
/** this mutex ensures that receipts are processed in order */
const receiptMutex = makeMutex()
/** 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()
// Timeout for AwaitingInitialSync state
let awaitingSyncTimeout: NodeJS.Timeout | undefined
@@ -748,7 +758,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
let initial: LTHashState
let encodeResult: { patch: proto.ISyncdPatch; state: LTHashState }
await processingMutex.mutex(async () => {
await appStatePatchMutex.mutex(async () => {
await authState.keys.transaction(async () => {
logger.debug({ patch: patchCreate }, 'applying app patch')
@@ -1180,7 +1190,10 @@ export const makeChatsSocket = (config: SocketConfig) => {
...sock,
createCallLink,
getBotListV2,
processingMutex,
messageMutex,
receiptMutex,
appStatePatchMutex,
notificationMutex,
fetchPrivacySettings,
upsertMessage,
appPatch,