Session recreation and improved message retry (#1735)
* Add message retry manager for session recreation and caching Introduces a MessageRetryManager to cache recent sent messages and manage automatic Signal session recreation for failed message retries. Adds new config options to enable these features, integrates retry manager into message send/receive logic, and provides periodic cache cleanup. Improves reliability of message delivery and retry handling. * Add buffer timeout and cache limit to event-buffer Introduces a buffer timeout to auto-flush events after 30 seconds and limits the history cache size to prevent memory bloat in event-buffer.ts. * Add session validation to SignalRepository Introduces a validateSession method to SignalRepository for checking session existence and state. Updates message retry logic to use the new validation, improving session recreation handling and retry management. * Improve message retry management and tracking Refactored message retry manager to use LRU caches for recent messages and session history, added retry counters and statistics tracking, and implemented phone request scheduling. Updated message receive and send logic to mark retry success and pass max retry count. Removed periodic cleanup logic in favor of cache TTLs for better resource management. * lint fix * Use validateSession for session checks in message send Replaces direct session existence checks with the improved validateSession method, which also checks for session staleness. Adds debug logging for failed session validations to aid troubleshooting. * Delete src/Utils/message-retry.ts --------- Co-authored-by: Rajeh Taher <rajeh@reforward.dev>
This commit is contained in:
committed by
GitHub
parent
f869317b0a
commit
ae0cb89714
+148
-21
@@ -60,7 +60,8 @@ import { extractGroupMetadata } from './groups'
|
||||
import { makeMessagesSocket } from './messages-send'
|
||||
|
||||
export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
const { logger, retryRequestDelayMs, maxMsgRetryCount, getMessage, shouldIgnoreJid } = config
|
||||
const { logger, retryRequestDelayMs, maxMsgRetryCount, getMessage, shouldIgnoreJid, enableAutoSessionRecreation } =
|
||||
config
|
||||
const sock = makeMessagesSocket(config)
|
||||
const {
|
||||
ev,
|
||||
@@ -77,7 +78,8 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
relayMessage,
|
||||
sendReceipt,
|
||||
uploadPreKeys,
|
||||
sendPeerDataOperationMessage
|
||||
sendPeerDataOperationMessage,
|
||||
messageRetryManager
|
||||
} = sock
|
||||
|
||||
/** this mutex ensures that each retryRequest will wait for the previous one to finish */
|
||||
@@ -169,23 +171,81 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
const { key: msgKey } = fullMessage
|
||||
const msgId = msgKey.id!
|
||||
|
||||
const key = `${msgId}:${msgKey?.participant}`
|
||||
let retryCount = msgRetryCache.get<number>(key) || 0
|
||||
if (retryCount >= maxMsgRetryCount) {
|
||||
logger.debug({ retryCount, msgId }, 'reached retry limit, clearing')
|
||||
msgRetryCache.del(key)
|
||||
return
|
||||
if (messageRetryManager) {
|
||||
// Check if we've exceeded max retries using the new system
|
||||
if (messageRetryManager.hasExceededMaxRetries(msgId)) {
|
||||
logger.debug({ msgId }, 'reached retry limit with new retry manager, clearing')
|
||||
messageRetryManager.markRetryFailed(msgId)
|
||||
return
|
||||
}
|
||||
|
||||
// Increment retry count using new system
|
||||
const retryCount = messageRetryManager.incrementRetryCount(msgId)
|
||||
|
||||
// Use the new retry count for the rest of the logic
|
||||
const key = `${msgId}:${msgKey?.participant}`
|
||||
msgRetryCache.set(key, retryCount)
|
||||
} else {
|
||||
// Fallback to old system
|
||||
const key = `${msgId}:${msgKey?.participant}`
|
||||
let retryCount = msgRetryCache.get<number>(key) || 0
|
||||
if (retryCount >= maxMsgRetryCount) {
|
||||
logger.debug({ retryCount, msgId }, 'reached retry limit, clearing')
|
||||
msgRetryCache.del(key)
|
||||
return
|
||||
}
|
||||
|
||||
retryCount += 1
|
||||
msgRetryCache.set(key, retryCount)
|
||||
}
|
||||
|
||||
retryCount += 1
|
||||
msgRetryCache.set(key, retryCount)
|
||||
const key = `${msgId}:${msgKey?.participant}`
|
||||
const retryCount = msgRetryCache.get<number>(key) || 1
|
||||
|
||||
const { account, signedPreKey, signedIdentityKey: identityKey } = authState.creds
|
||||
const fromJid = node.attrs.from!
|
||||
|
||||
if (retryCount === 1) {
|
||||
//request a resend via phone
|
||||
const msgId = await requestPlaceholderResend(msgKey)
|
||||
logger.debug(`sendRetryRequest: requested placeholder resend for message ${msgId}`)
|
||||
// Check if we should recreate the session
|
||||
let shouldRecreateSession = false
|
||||
let recreateReason = ''
|
||||
|
||||
if (enableAutoSessionRecreation && messageRetryManager) {
|
||||
try {
|
||||
// Check if we have a session with this JID
|
||||
const sessionId = signalRepository.jidToSignalProtocolAddress(fromJid)
|
||||
const hasSession = await signalRepository.validateSession(fromJid)
|
||||
const result = messageRetryManager.shouldRecreateSession(fromJid, retryCount, hasSession.exists)
|
||||
shouldRecreateSession = result.recreate
|
||||
recreateReason = result.reason
|
||||
|
||||
if (shouldRecreateSession) {
|
||||
logger.info({ fromJid, retryCount, reason: recreateReason }, 'recreating session for retry')
|
||||
// Delete existing session to force recreation
|
||||
await authState.keys.set({ session: { [sessionId]: null } })
|
||||
forceIncludeKeys = true
|
||||
}
|
||||
} catch (error) {
|
||||
logger.warn({ error, fromJid }, 'failed to check session recreation')
|
||||
}
|
||||
}
|
||||
|
||||
if (retryCount <= 2) {
|
||||
// Use new retry manager for phone requests if available
|
||||
if (messageRetryManager) {
|
||||
// Schedule phone request with delay (like whatsmeow)
|
||||
messageRetryManager.schedulePhoneRequest(msgId, async () => {
|
||||
try {
|
||||
const msgId = await requestPlaceholderResend(msgKey)
|
||||
logger.debug(`sendRetryRequest: requested placeholder resend for message ${msgId} (scheduled)`)
|
||||
} catch (error) {
|
||||
logger.warn({ error, msgId }, 'failed to send scheduled phone request')
|
||||
}
|
||||
})
|
||||
} else {
|
||||
// Fallback to immediate request
|
||||
const msgId = await requestPlaceholderResend(msgKey)
|
||||
logger.debug(`sendRetryRequest: requested placeholder resend for message ${msgId}`)
|
||||
}
|
||||
}
|
||||
|
||||
const deviceIdentity = encodeSignedDeviceIdentity(account!, true)
|
||||
@@ -223,7 +283,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
receipt.attrs.participant = node.attrs.participant
|
||||
}
|
||||
|
||||
if (retryCount > 1 || forceIncludeKeys) {
|
||||
if (retryCount > 1 || forceIncludeKeys || shouldRecreateSession) {
|
||||
const { update, preKeys } = await getNextPreKeys(authState, 1)
|
||||
|
||||
const [keyId] = Object.keys(preKeys)
|
||||
@@ -247,7 +307,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
|
||||
await sendNode(receipt)
|
||||
|
||||
logger.info({ msgAttrs: node.attrs, retryCount }, 'sent retry receipt')
|
||||
logger.info({ msgAttrs: node.attrs, retryCount, shouldRecreateSession, recreateReason }, 'sent retry receipt')
|
||||
})
|
||||
}
|
||||
|
||||
@@ -597,21 +657,77 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
}
|
||||
|
||||
const sendMessagesAgain = async (key: proto.IMessageKey, ids: string[], retryNode: BinaryNode) => {
|
||||
// todo: implement a cache to store the last 256 sent messages (copy whatsmeow)
|
||||
const msgs = await Promise.all(ids.map(id => getMessage({ ...key, id })))
|
||||
const remoteJid = key.remoteJid!
|
||||
const participant = key.participant || remoteJid
|
||||
|
||||
const retryCount = +retryNode.attrs.count! || 1
|
||||
|
||||
// Try to get messages from cache first, then fallback to getMessage
|
||||
const msgs: (proto.IMessage | undefined)[] = []
|
||||
for (const id of ids) {
|
||||
let msg: proto.IMessage | undefined
|
||||
|
||||
// Try to get from retry cache first if enabled
|
||||
if (messageRetryManager) {
|
||||
const cachedMsg = messageRetryManager.getRecentMessage(remoteJid, id)
|
||||
if (cachedMsg) {
|
||||
msg = cachedMsg.message
|
||||
logger.debug({ jid: remoteJid, id }, 'found message in retry cache')
|
||||
|
||||
// Mark retry as successful since we found the message
|
||||
messageRetryManager.markRetrySuccess(id)
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to getMessage if not found in cache
|
||||
if (!msg) {
|
||||
msg = await getMessage({ ...key, id })
|
||||
if (msg) {
|
||||
logger.debug({ jid: remoteJid, id }, 'found message via getMessage')
|
||||
// Also mark as successful if found via getMessage
|
||||
if (messageRetryManager) {
|
||||
messageRetryManager.markRetrySuccess(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
msgs.push(msg)
|
||||
}
|
||||
|
||||
// if it's the primary jid sending the request
|
||||
// just re-send the message to everyone
|
||||
// prevents the first message decryption failure
|
||||
const sendToAll = !jidDecode(participant)?.device
|
||||
await assertSessions([participant], true)
|
||||
|
||||
// Check if we should recreate session for this retry
|
||||
let shouldRecreateSession = false
|
||||
let recreateReason = ''
|
||||
|
||||
if (enableAutoSessionRecreation && messageRetryManager) {
|
||||
try {
|
||||
const sessionId = signalRepository.jidToSignalProtocolAddress(participant)
|
||||
|
||||
const hasSession = await signalRepository.validateSession(participant)
|
||||
const result = messageRetryManager.shouldRecreateSession(participant, retryCount, hasSession.exists)
|
||||
shouldRecreateSession = result.recreate
|
||||
recreateReason = result.reason
|
||||
|
||||
if (shouldRecreateSession) {
|
||||
logger.info({ participant, retryCount, reason: recreateReason }, 'recreating session for outgoing retry')
|
||||
await authState.keys.set({ session: { [sessionId]: null } })
|
||||
}
|
||||
} catch (error) {
|
||||
logger.warn({ error, participant }, 'failed to check session recreation for outgoing retry')
|
||||
}
|
||||
}
|
||||
|
||||
await assertSessions([participant], shouldRecreateSession)
|
||||
|
||||
if (isJidGroup(remoteJid)) {
|
||||
await authState.keys.set({ 'sender-key-memory': { [remoteJid]: null } })
|
||||
}
|
||||
|
||||
logger.debug({ participant, sendToAll }, 'forced new session for retry recp')
|
||||
logger.debug({ participant, sendToAll, shouldRecreateSession, recreateReason }, 'forced new session for retry recp')
|
||||
|
||||
for (const [i, msg] of msgs.entries()) {
|
||||
if (!ids[i]) continue
|
||||
@@ -816,6 +932,16 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
ev.emit('chats.phoneNumberShare', { lid: node.attrs.from!, jid: node.attrs.sender_pn })
|
||||
}
|
||||
|
||||
if (msg.key?.remoteJid && msg.key?.id && messageRetryManager) {
|
||||
messageRetryManager.addRecentMessage(msg.key.remoteJid, msg.key.id, msg.message!)
|
||||
logger.debug(
|
||||
{
|
||||
jid: msg.key.remoteJid,
|
||||
id: msg.key.id
|
||||
},
|
||||
'Added message to recent cache for retry receipts'
|
||||
)
|
||||
|
||||
if (msg.message?.protocolMessage?.lidMigrationMappingSyncMessage?.encodedMappingPayload) {
|
||||
try {
|
||||
const payload = msg.message.protocolMessage.lidMigrationMappingSyncMessage.encodedMappingPayload
|
||||
@@ -1409,6 +1535,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
sendRetryRequest,
|
||||
rejectCall,
|
||||
fetchMessageHistory,
|
||||
requestPlaceholderResend
|
||||
requestPlaceholderResend,
|
||||
messageRetryManager
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import {
|
||||
getStatusCodeForMediaRetry,
|
||||
getUrlFromDirectPath,
|
||||
getWAUploadToServer,
|
||||
MessageRetryManager,
|
||||
normalizeMessageContent,
|
||||
parseAndInjectE2ESessions,
|
||||
unixTimestampSeconds
|
||||
@@ -58,7 +59,9 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
generateHighQualityLinkPreview,
|
||||
options: axiosOptions,
|
||||
patchMessageBeforeSending,
|
||||
cachedGroupMetadata
|
||||
cachedGroupMetadata,
|
||||
enableRecentMessageCache,
|
||||
maxMsgRetryCount
|
||||
} = config
|
||||
const sock: NewsletterSocket = makeNewsletterSocket(makeGroupsSocket(config))
|
||||
const {
|
||||
@@ -81,7 +84,10 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
useClones: false
|
||||
})
|
||||
|
||||
// Prevent race conditions in Signal session encryption by user
|
||||
// Initialize message retry manager if enabled
|
||||
const messageRetryManager = enableRecentMessageCache ? new MessageRetryManager(logger, maxMsgRetryCount) : null
|
||||
|
||||
// Prevent race conditions in Signal session encryption by user
|
||||
const encryptionMutex = makeKeyedMutex()
|
||||
|
||||
let mediaConn: Promise<MediaConnInfo>
|
||||
@@ -389,6 +395,9 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
}
|
||||
|
||||
jidsRequiringFetch.push(jid)
|
||||
if (validation.reason) {
|
||||
logger.debug({ jid, reason: validation.reason }, 'session validation failed')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1118,6 +1127,11 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
logger.debug({ msgId }, `sending message to ${participants.length} devices`)
|
||||
|
||||
await sendNode(stanza)
|
||||
|
||||
// Add message to retry cache if enabled
|
||||
if (messageRetryManager && !participant) {
|
||||
messageRetryManager.addRecentMessage(destinationJid, msgId, message)
|
||||
}
|
||||
})
|
||||
|
||||
return msgId
|
||||
@@ -1215,6 +1229,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
sendPeerDataOperationMessage,
|
||||
createParticipantNodes,
|
||||
getUSyncDevices,
|
||||
messageRetryManager,
|
||||
updateMediaMessage: async (message: proto.IWebMessageInfo) => {
|
||||
const content = assertMediaContent(message.message)
|
||||
const mediaKey = content.mediaKey!
|
||||
|
||||
Reference in New Issue
Block a user