perf(signal): eliminate 3 post-restart latency sources in migrateSession

perf(signal): eliminate 3 post-restart latency sources in migrateSession
This commit is contained in:
Renato Alcara
2026-02-27 00:12:29 -03:00
committed by GitHub
parent d73f2d0fc3
commit 73682d40c6
2 changed files with 43 additions and 8 deletions
+15 -3
View File
@@ -352,6 +352,11 @@ export const makeSocket = (config: SocketConfig) => {
// whose message contained "socket-query" → matched the circuit-breaker's
// "socket" pattern → incorrectly tripped the breaker after 5 timeouts.
const responsePromise = waitForMessage<any>(msgId, timeoutMs)
// Prevent unhandled-rejection if sendNode throws before we reach
// `await responsePromise` below. The error from sendNode still propagates
// to the caller; this only silences the secondary rejection from
// responsePromise (which waitForMessage will emit when ws closes).
responsePromise.catch(() => {})
// Await the send so that real sendNode failures (e.g. serialisation errors)
// are surfaced to the caller immediately rather than silently discarded.
@@ -1548,10 +1553,17 @@ export const makeSocket = (config: SocketConfig) => {
recordConnectionAttempt('success')
incrementActiveConnections()
// Start session cleanup scheduler
sessionCleanup.start()
// Defer session cleanup start by 5 s to avoid DB contention during the
// initial message flood right after CB:success. The heavyweight
// getAllSessionKeys() scan would otherwise compete with migrateSession()
// for the same storage locks while the offline-message backlog is draining.
// start() is idempotent (guarded by cleanupInterval check) so deferring is safe.
const _cleanupStartTimer = setTimeout(() => sessionCleanup.start(), 5_000)
if (typeof (_cleanupStartTimer as NodeJS.Timeout).unref === 'function') {
;(_cleanupStartTimer as NodeJS.Timeout).unref()
}
// Start session activity tracker
// Start session activity tracker immediately (lightweight, no DB scan)
sessionActivityTracker.start()
// Update server time offset from success node