From d8a212590001796cf4a8e9f5a15650f116e4d94d Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Tue, 14 Dec 2021 19:39:08 +0530 Subject: [PATCH] process: add `getActiveResourcesInfo()` This is supposed to be a public alternative of the private APIs, `process._getActiveResources()` and `process._getActiveHandles()`. When called, it returns an array of strings containing the types of the active resources that are currently keeping the event loop alive. Signed-off-by: Darshan Sen PR-URL: https://github.com/nodejs/node/pull/40813 Reviewed-By: Stephen Belanger Reviewed-By: Vladimir de Turckheim Reviewed-By: Matteo Collina --- doc/api/process.md | 38 ++++++++++++++++ lib/internal/bootstrap/node.js | 21 ++++++++- lib/internal/timers.js | 13 ++++++ lib/timers.js | 3 ++ src/node_process_methods.cc | 34 ++++++++++++++ test/parallel/test-handle-wrap-isrefed.js | 26 +++++++++++ ...getactiveresources-track-active-handles.js | 44 +++++++++++++++++++ ...etactiveresources-track-active-requests.js | 11 +++++ ...activeresources-track-interval-lifetime.js | 21 +++++++++ ...etactiveresources-track-multiple-timers.js | 20 +++++++++ ...getactiveresources-track-timer-lifetime.js | 41 +++++++++++++++++ .../test-process-getactiveresources.js | 9 ++++ .../test-net-connect-econnrefused.js | 5 +-- 13 files changed, 281 insertions(+), 5 deletions(-) create mode 100644 test/parallel/test-process-getactiveresources-track-active-handles.js create mode 100644 test/parallel/test-process-getactiveresources-track-active-requests.js create mode 100644 test/parallel/test-process-getactiveresources-track-interval-lifetime.js create mode 100644 test/parallel/test-process-getactiveresources-track-multiple-timers.js create mode 100644 test/parallel/test-process-getactiveresources-track-timer-lifetime.js create mode 100644 test/parallel/test-process-getactiveresources.js diff --git a/doc/api/process.md b/doc/api/process.md index 28db286bd90fd2..d6096325a14b61 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -1817,6 +1817,44 @@ a code. Specifying a code to [`process.exit(code)`][`process.exit()`] will override any previous setting of `process.exitCode`. +## `process.getActiveResourcesInfo()` + + + +> Stability: 1 - Experimental + +* Returns: {string\[]} + +The `process.getActiveResourcesInfo()` method returns an array of strings +containing the types of the active resources that are currently keeping the +event loop alive. + +```mjs +import { getActiveResourcesInfo } from 'process'; +import { setTimeout } from 'timers'; + +console.log('Before:', getActiveResourcesInfo()); +setTimeout(() => {}, 1000); +console.log('After:', getActiveResourcesInfo()); +// Prints: +// Before: [ 'CloseReq', 'TTYWrap', 'TTYWrap', 'TTYWrap' ] +// After: [ 'CloseReq', 'TTYWrap', 'TTYWrap', 'TTYWrap', 'Timeout' ] +``` + +```cjs +const { getActiveResourcesInfo } = require('process'); +const { setTimeout } = require('timers'); + +console.log('Before:', getActiveResourcesInfo()); +setTimeout(() => {}, 1000); +console.log('After:', getActiveResourcesInfo()); +// Prints: +// Before: [ 'TTYWrap', 'TTYWrap', 'TTYWrap' ] +// After: [ 'TTYWrap', 'TTYWrap', 'TTYWrap', 'Timeout' ] +``` + ## `process.getegid()`