fix(retry): resolve message delivery speed

fix(retry): resolve message delivery speed
This commit is contained in:
Renato Alcara
2026-02-26 22:09:18 -03:00
committed by GitHub
parent 416ad47789
commit d73f2d0fc3
4 changed files with 53 additions and 20 deletions
+26 -13
View File
@@ -506,16 +506,16 @@ export function makeLibSignalRepository(
// Get user's device list — use in-memory cache to avoid a DB round-trip on
// every incoming LID message. Cache is invalidated after 5 minutes.
// We use undefined to mean "not yet checked" and [] to mean "checked, no devices
// found" so that DB misses are also cached and don't cause a per-message lookup.
let userDevices: string[] | undefined = deviceListCache.get(user)
if (!userDevices) {
if (userDevices === undefined) {
const result = await parsedKeys.get('device-list', [user])
userDevices = result[user]
if (userDevices) {
deviceListCache.set(user, userDevices)
}
userDevices = result[user] ?? []
deviceListCache.set(user, userDevices)
}
if (!userDevices) {
if (userDevices.length === 0) {
return { migrated: 0, skipped: 0, total: 0 }
}
@@ -534,6 +534,12 @@ export function makeLibSignalRepository(
return !migratedSessionCache.has(deviceKey)
})
// All devices already confirmed as migrated — skip DB lookups entirely
if (uncachedDevices.length === 0) {
logger.debug({ fromJid, totalDevices: userDevices.length }, 'bulk device migration - all devices already cached, skipping')
return { migrated: 0, skipped: 0, total: userDevices.length }
}
// Bulk check session existence only for uncached devices
const deviceSessionKeys = uncachedDevices.map(device => `${user}.${device}`)
const existingSessions = await parsedKeys.get('session', deviceSessionKeys)
@@ -565,6 +571,15 @@ export function makeLibSignalRepository(
'bulk device migration complete - all user devices processed'
)
// No PN-format sessions found: all devices are already migrated to LID addressing.
// Cache them now so subsequent messages skip redundant DB lookups entirely.
if (deviceJids.length === 0) {
for (const device of uncachedDevices) {
migratedSessionCache.set(`${user}.${device}`, true)
}
return { migrated: 0, skipped: 0, total: userDevices.length }
}
// Single transaction for all migrations
return parsedKeys.transaction(
async (): Promise<{ migrated: number; skipped: number; total: number }> => {
@@ -630,14 +645,12 @@ export function makeLibSignalRepository(
if (Object.keys(sessionUpdates).length > 0) {
await parsedKeys.set({ session: sessionUpdates })
logger.debug({ migratedSessions: migratedCount }, 'bulk session migration complete')
}
// Cache device-level migrations
for (const op of migrationOps) {
if (sessionUpdates[op.toAddr.toString()]) {
const deviceKey = `${op.pnUser}.${op.deviceId}`
migratedSessionCache.set(deviceKey, true)
}
}
// Cache ALL processed devices (migrated, skipped, or closed-session) to prevent
// redundant DB lookups on subsequent messages from the same contact.
for (const op of migrationOps) {
migratedSessionCache.set(`${op.pnUser}.${op.deviceId}`, true)
}
const skippedCount = totalOps - migratedCount