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:
Paulo Victor Lund
2025-09-07 14:20:51 -03:00
committed by GitHub
parent f869317b0a
commit ae0cb89714
11 changed files with 454 additions and 28 deletions
+51 -4
View File
@@ -66,7 +66,6 @@ type BaileysBufferableEventEmitter = BaileysEventEmitter & {
/**
* The event buffer logically consolidates different events into a single event
* making the data processing more efficient.
* @param ev the baileys event emitter
*/
export const makeEventBuffer = (logger: ILogger): BaileysBufferableEventEmitter => {
const ev = new EventEmitter()
@@ -74,6 +73,10 @@ export const makeEventBuffer = (logger: ILogger): BaileysBufferableEventEmitter
let data = makeBufferData()
let isBuffering = false
let bufferTimeout: NodeJS.Timeout | null = null
let bufferCount = 0
const MAX_HISTORY_CACHE_SIZE = 10000 // Limit the history cache size to prevent memory bloat
const BUFFER_TIMEOUT_MS = 30000 // 30 seconds
// take the generic event and fire it as a baileys event
ev.on('event', (map: BaileysEventData) => {
@@ -86,6 +89,21 @@ export const makeEventBuffer = (logger: ILogger): BaileysBufferableEventEmitter
if (!isBuffering) {
logger.debug('Event buffer activated')
isBuffering = true
bufferCount++
// Auto-flush after a timeout to prevent infinite buffering
if (bufferTimeout) {
clearTimeout(bufferTimeout)
}
bufferTimeout = setTimeout(() => {
if (isBuffering) {
logger.warn('Buffer timeout reached, auto-flushing')
flush()
}
}, BUFFER_TIMEOUT_MS)
} else {
bufferCount++
}
}
@@ -94,8 +112,21 @@ export const makeEventBuffer = (logger: ILogger): BaileysBufferableEventEmitter
return false
}
logger.debug('Flushing event buffer')
logger.debug({ bufferCount }, 'Flushing event buffer')
isBuffering = false
bufferCount = 0
// Clear timeout
if (bufferTimeout) {
clearTimeout(bufferTimeout)
bufferTimeout = null
}
// Clear history cache if it exceeds the max size
if (historyCache.size > MAX_HISTORY_CACHE_SIZE) {
logger.debug({ cacheSize: historyCache.size }, 'Clearing history cache')
historyCache.clear()
}
const newData = makeBufferData()
const chatUpdates = Object.values(data.chatUpdates)
@@ -148,9 +179,25 @@ export const makeEventBuffer = (logger: ILogger): BaileysBufferableEventEmitter
return async (...args) => {
buffer()
try {
return await work(...args)
const result = await work(...args)
// If this is the only buffer, flush after a small delay
if (bufferCount === 1) {
setTimeout(() => {
if (isBuffering && bufferCount === 1) {
flush()
}
}, 100) // Small delay to allow nested buffers
}
return result
} catch (error) {
throw error
} finally {
// Flushing is now controlled centrally by the state machine.
bufferCount = Math.max(0, bufferCount - 1)
if (bufferCount === 0) {
// Auto-flush when no other buffers are active
setTimeout(flush, 100)
}
}
}
},
+1
View File
@@ -15,3 +15,4 @@ export * from './use-multi-file-auth-state'
export * from './link-preview'
export * from './event-buffer'
export * from './process-message'
export * from './message-retry-manager'
+205
View File
@@ -0,0 +1,205 @@
import { LRUCache } from 'lru-cache'
import { proto } from '../../WAProto'
import type { ILogger } from './logger'
/** Number of sent messages to cache in memory for handling retry receipts */
const RECENT_MESSAGES_SIZE = 512
/** Timeout for session recreation - 1 hour */
const RECREATE_SESSION_TIMEOUT = 60 * 60 * 1000 // 1 hour in milliseconds
const PHONE_REQUEST_DELAY = 3000
export interface RecentMessageKey {
to: string
id: string
}
export interface RecentMessage {
message: proto.IMessage
timestamp: number
}
export interface SessionRecreateHistory {
[jid: string]: number // timestamp
}
export interface RetryCounter {
[messageId: string]: number
}
export interface PendingPhoneRequest {
[messageId: string]: NodeJS.Timeout
}
export interface RetryStatistics {
totalRetries: number
successfulRetries: number
failedRetries: number
mediaRetries: number
sessionRecreations: number
phoneRequests: number
}
export class MessageRetryManager {
private recentMessagesMap = new LRUCache<string, RecentMessage>({
max: RECENT_MESSAGES_SIZE
})
private sessionRecreateHistory = new LRUCache<string, number>({
ttl: RECREATE_SESSION_TIMEOUT * 2,
ttlAutopurge: true
})
private retryCounters = new LRUCache<string, number>({
ttl: 15 * 60 * 1000,
ttlAutopurge: true,
updateAgeOnGet: true
}) // 15 minutes TTL
private pendingPhoneRequests: PendingPhoneRequest = {}
private readonly maxMsgRetryCount: number = 5
private statistics: RetryStatistics = {
totalRetries: 0,
successfulRetries: 0,
failedRetries: 0,
mediaRetries: 0,
sessionRecreations: 0,
phoneRequests: 0
}
constructor(
private logger: ILogger,
maxMsgRetryCount: number
) {
this.maxMsgRetryCount = maxMsgRetryCount
}
/**
* Add a recent message to the cache for retry handling
*/
addRecentMessage(to: string, id: string, message: proto.IMessage): void {
const key: RecentMessageKey = { to, id }
const keyStr = this.keyToString(key)
// Add new message
this.recentMessagesMap.set(keyStr, {
message,
timestamp: Date.now()
})
this.logger.debug(`Added message to retry cache: ${to}/${id}`)
}
/**
* Get a recent message from the cache
*/
getRecentMessage(to: string, id: string): RecentMessage | undefined {
const key: RecentMessageKey = { to, id }
const keyStr = this.keyToString(key)
return this.recentMessagesMap.get(keyStr)
}
/**
* Check if a session should be recreated based on retry count and history
*/
shouldRecreateSession(jid: string, retryCount: number, hasSession: boolean): { reason: string; recreate: boolean } {
// If we don't have a session, always recreate
if (!hasSession) {
this.sessionRecreateHistory.set(jid, Date.now())
this.statistics.sessionRecreations++
return {
reason: "we don't have a Signal session with them",
recreate: true
}
}
// Only consider recreation if retry count > 1
if (retryCount < 2) {
return { reason: '', recreate: false }
}
const now = Date.now()
const prevTime = this.sessionRecreateHistory.get(jid)
// If no previous recreation or it's been more than an hour
if (!prevTime || now - prevTime > RECREATE_SESSION_TIMEOUT) {
this.sessionRecreateHistory.set(jid, now)
this.statistics.sessionRecreations++
return {
reason: 'retry count > 1 and over an hour since last recreation',
recreate: true
}
}
return { reason: '', recreate: false }
}
/**
* Increment retry counter for a message
*/
incrementRetryCount(messageId: string): number {
this.retryCounters.set(messageId, (this.retryCounters.get(messageId) || 0) + 1)
this.statistics.totalRetries++
return this.retryCounters.get(messageId)!
}
/**
* Get retry count for a message
*/
getRetryCount(messageId: string): number {
return this.retryCounters.get(messageId) || 0
}
/**
* Check if message has exceeded maximum retry attempts
*/
hasExceededMaxRetries(messageId: string): boolean {
return this.getRetryCount(messageId) >= this.maxMsgRetryCount
}
/**
* Mark retry as successful
*/
markRetrySuccess(messageId: string): void {
this.statistics.successfulRetries++
// Clean up retry counter for successful message
this.retryCounters.delete(messageId)
this.cancelPendingPhoneRequest(messageId)
}
/**
* Mark retry as failed
*/
markRetryFailed(messageId: string): void {
this.statistics.failedRetries++
this.retryCounters.delete(messageId)
}
/**
* Schedule a phone request with delay
*/
schedulePhoneRequest(messageId: string, callback: () => void, delay: number = PHONE_REQUEST_DELAY): void {
// Cancel any existing request for this message
this.cancelPendingPhoneRequest(messageId)
this.pendingPhoneRequests[messageId] = setTimeout(() => {
delete this.pendingPhoneRequests[messageId]
this.statistics.phoneRequests++
callback()
}, delay)
this.logger.debug(`Scheduled phone request for message ${messageId} with ${delay}ms delay`)
}
/**
* Cancel pending phone request
*/
cancelPendingPhoneRequest(messageId: string): void {
const timeout = this.pendingPhoneRequests[messageId]
if (timeout) {
clearTimeout(timeout)
delete this.pendingPhoneRequests[messageId]
this.logger.debug(`Cancelled pending phone request for message ${messageId}`)
}
}
private keyToString(key: RecentMessageKey): string {
return `${key.to}:${key.id}`
}
}