fix: prevent overlapping poll cycles in managers

Guarding polling re-entry avoids stacked async polls under slow responses, and unref on pending-call cleanup timer reduces idle wakeups.
This commit is contained in:
YeonGyu-Kim
2026-02-17 03:06:40 +09:00
parent 14374095af
commit 3e06d280a9
6 changed files with 169 additions and 6 deletions
@@ -0,0 +1,38 @@
import { describe, test, expect } from "bun:test"
describe("pending-calls cleanup interval", () => {
test("starts cleanup once and unrefs timer", async () => {
//#given
const originalSetInterval = globalThis.setInterval
const setIntervalCalls: number[] = []
let unrefCalled = 0
globalThis.setInterval = ((
_handler: TimerHandler,
timeout?: number,
..._args: any[]
) => {
setIntervalCalls.push(timeout as number)
return {
unref: () => {
unrefCalled += 1
},
} as unknown as ReturnType<typeof setInterval>
}) as unknown as typeof setInterval
try {
const modulePath = new URL("./pending-calls.ts", import.meta.url).pathname
const pendingCallsModule = await import(`${modulePath}?pending-calls-test-once`)
//#when
pendingCallsModule.startPendingCallCleanup()
pendingCallsModule.startPendingCallCleanup()
//#then
expect(setIntervalCalls).toEqual([10_000])
expect(unrefCalled).toBe(1)
} finally {
globalThis.setInterval = originalSetInterval
}
})
})
+5 -1
View File
@@ -4,6 +4,7 @@ const pendingCalls = new Map<string, PendingCall>()
const PENDING_CALL_TTL = 60_000
let cleanupIntervalStarted = false
let cleanupInterval: ReturnType<typeof setInterval> | undefined
function cleanupOldPendingCalls(): void {
const now = Date.now()
@@ -17,7 +18,10 @@ function cleanupOldPendingCalls(): void {
export function startPendingCallCleanup(): void {
if (cleanupIntervalStarted) return
cleanupIntervalStarted = true
setInterval(cleanupOldPendingCalls, 10_000)
cleanupInterval = setInterval(cleanupOldPendingCalls, 10_000)
if (typeof cleanupInterval === "object" && "unref" in cleanupInterval) {
cleanupInterval.unref()
}
}
export function registerPendingCall(callID: string, pendingCall: PendingCall): void {