Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cluster: use inspector utils #44592

Merged
merged 1 commit into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 11 additions & 27 deletions lib/internal/cluster/primary.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ const {
ArrayPrototypeSome,
ObjectKeys,
ObjectValues,
RegExpPrototypeExec,
SafeMap,
StringPrototypeStartsWith,
} = primordials;
const {
codes: {
ERR_SOCKET_BAD_PORT,
}
} = require('internal/errors');

const assert = require('internal/assert');
const { fork } = require('child_process');
Expand All @@ -18,14 +22,12 @@ const EventEmitter = require('events');
const RoundRobinHandle = require('internal/cluster/round_robin_handle');
const SharedHandle = require('internal/cluster/shared_handle');
const Worker = require('internal/cluster/worker');
const { getInspectPort, isUsingInspector } = require('internal/util/inspector');
const { internal, sendHelper } = require('internal/cluster/utils');
const cluster = new EventEmitter();
const intercom = new EventEmitter();
const SCHED_NONE = 1;
const SCHED_RR = 2;
const minPort = 1024;
const maxPort = 65535;
const { validatePort } = require('internal/validators');

module.exports = cluster;

Expand All @@ -40,7 +42,6 @@ cluster.SCHED_NONE = SCHED_NONE; // Leave it to the operating system.
cluster.SCHED_RR = SCHED_RR; // Primary distributes connections.

let ids = 0;
let debugPortOffset = 1;
let initialized = false;

// XXX(bnoordhuis) Fold cluster.schedulingPolicy into cluster.settings?
Expand Down Expand Up @@ -117,29 +118,12 @@ function setupSettingsNT(settings) {
function createWorkerProcess(id, env) {
const workerEnv = { ...process.env, ...env, NODE_UNIQUE_ID: `${id}` };
const execArgv = [...cluster.settings.execArgv];
const debugArgRegex = /--inspect(?:-brk|-port)?|--debug-port/;
const nodeOptions = process.env.NODE_OPTIONS || '';

// TODO(MoLow): Use getInspectPort from internal/util/inspector
if (ArrayPrototypeSome(execArgv,
(arg) => RegExpPrototypeExec(debugArgRegex, arg) !== null) ||
RegExpPrototypeExec(debugArgRegex, nodeOptions) !== null) {
let inspectPort;
if ('inspectPort' in cluster.settings) {
if (typeof cluster.settings.inspectPort === 'function')
inspectPort = cluster.settings.inspectPort();
else
inspectPort = cluster.settings.inspectPort;

validatePort(inspectPort);
} else {
inspectPort = process.debugPort + debugPortOffset;
if (inspectPort > maxPort)
inspectPort = inspectPort - maxPort + minPort - 1;
debugPortOffset++;
}

ArrayPrototypePush(execArgv, `--inspect-port=${inspectPort}`);
if (cluster.settings.inspectPort === null) {
throw new ERR_SOCKET_BAD_PORT('Port', null, true);
}
if (isUsingInspector(cluster.settings.execArgv)) {
ArrayPrototypePush(execArgv, `--inspect-port=${getInspectPort(cluster.settings.inspectPort)}`);
}

return fork(cluster.settings.exec, cluster.settings.args, {
Expand Down
18 changes: 9 additions & 9 deletions lib/internal/util/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
ObjectKeys,
ObjectPrototypeHasOwnProperty,
RegExpPrototypeExec,
SafeWeakMap,
} = primordials;

const { validatePort } = require('internal/validators');
Expand All @@ -17,19 +18,18 @@ const kMaxPort = 65535;
const kInspectArgRegex = /--inspect(?:-brk|-port)?|--debug-port/;
const kInspectMsgRegex = /Debugger listening on ws:\/\/\[?(.+?)\]?:(\d+)\/|Debugger attached|Waiting for the debugger to disconnect\.\.\./;

let _isUsingInspector;
function isUsingInspector() {
_isUsingInspector ??=
ArrayPrototypeSome(process.execArgv, (arg) => RegExpPrototypeExec(kInspectArgRegex, arg) !== null) ||
RegExpPrototypeExec(kInspectArgRegex, process.env.NODE_OPTIONS) !== null;
return _isUsingInspector;
const _isUsingInspector = new SafeWeakMap();
function isUsingInspector(execArgv = process.execArgv) {
if (!_isUsingInspector.has(execArgv)) {
_isUsingInspector.set(execArgv,
ArrayPrototypeSome(execArgv, (arg) => RegExpPrototypeExec(kInspectArgRegex, arg) !== null) ||
RegExpPrototypeExec(kInspectArgRegex, process.env.NODE_OPTIONS) !== null);
}
return _isUsingInspector.get(execArgv);
}

let debugPortOffset = 1;
function getInspectPort(inspectPort) {
if (!isUsingInspector()) {
return null;
}
if (typeof inspectPort === 'function') {
inspectPort = inspectPort();
} else if (inspectPort == null) {
Expand Down