Skip to content

Commit 43ed7b0

Browse files
authored
fix(utils): Move Node specific ANR impl. out of utils (#9258)
1 parent c9aaf8b commit 43ed7b0

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

packages/node/src/anr/index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,20 @@ function startChildProcess(options: Options): void {
138138
}
139139
}
140140

141+
function createHrTimer(): { getTimeMs: () => number; reset: () => void } {
142+
let lastPoll = process.hrtime();
143+
144+
return {
145+
getTimeMs: (): number => {
146+
const [seconds, nanoSeconds] = process.hrtime(lastPoll);
147+
return Math.floor(seconds * 1e3 + nanoSeconds / 1e6);
148+
},
149+
reset: (): void => {
150+
lastPoll = process.hrtime();
151+
},
152+
};
153+
}
154+
141155
function handleChildProcess(options: Options): void {
142156
function log(message: string): void {
143157
logger.log(`[ANR child process] ${message}`);
@@ -182,7 +196,7 @@ function handleChildProcess(options: Options): void {
182196
}
183197
}
184198

185-
const { poll } = watchdogTimer(options.pollInterval, options.anrThreshold, watchdogTimeout);
199+
const { poll } = watchdogTimer(createHrTimer, options.pollInterval, options.anrThreshold, watchdogTimeout);
186200

187201
process.on('message', () => {
188202
poll();

packages/utils/src/anr.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,27 @@ type WatchdogReturn = {
1010
enabled: (state: boolean) => void;
1111
};
1212

13+
type CreateTimerImpl = () => { getTimeMs: () => number; reset: () => void };
14+
1315
/**
1416
* A node.js watchdog timer
1517
* @param pollInterval The interval that we expect to get polled at
1618
* @param anrThreshold The threshold for when we consider ANR
1719
* @param callback The callback to call for ANR
1820
* @returns An object with `poll` and `enabled` functions {@link WatchdogReturn}
1921
*/
20-
export function watchdogTimer(pollInterval: number, anrThreshold: number, callback: () => void): WatchdogReturn {
21-
let lastPoll = process.hrtime();
22+
export function watchdogTimer(
23+
createTimer: CreateTimerImpl,
24+
pollInterval: number,
25+
anrThreshold: number,
26+
callback: () => void,
27+
): WatchdogReturn {
28+
const timer = createTimer();
2229
let triggered = false;
2330
let enabled = true;
2431

2532
setInterval(() => {
26-
const [seconds, nanoSeconds] = process.hrtime(lastPoll);
27-
const diffMs = Math.floor(seconds * 1e3 + nanoSeconds / 1e6);
33+
const diffMs = timer.getTimeMs();
2834

2935
if (triggered === false && diffMs > pollInterval + anrThreshold) {
3036
triggered = true;
@@ -40,7 +46,7 @@ export function watchdogTimer(pollInterval: number, anrThreshold: number, callba
4046

4147
return {
4248
poll: () => {
43-
lastPoll = process.hrtime();
49+
timer.reset();
4450
},
4551
enabled: (state: boolean) => {
4652
enabled = state;

0 commit comments

Comments
 (0)