Skip to content

Commit e54108f

Browse files
aduh95codebytere
authored andcommitted
cluster: refactor to use more primordials
PR-URL: #36011 Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 63a138e commit e54108f

File tree

6 files changed

+51
-33
lines changed

6 files changed

+51
-33
lines changed

lib/internal/cluster/child.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
'use strict';
22

33
const {
4-
Map,
4+
ArrayPrototypeJoin,
5+
FunctionPrototype,
56
ObjectAssign,
7+
ReflectApply,
8+
SafeMap,
69
} = primordials;
710

811
const assert = require('internal/assert');
@@ -12,9 +15,9 @@ const { owner_symbol } = require('internal/async_hooks').symbols;
1215
const Worker = require('internal/cluster/worker');
1316
const { internal, sendHelper } = require('internal/cluster/utils');
1417
const cluster = new EventEmitter();
15-
const handles = new Map();
16-
const indexes = new Map();
17-
const noop = () => {};
18+
const handles = new SafeMap();
19+
const indexes = new SafeMap();
20+
const noop = FunctionPrototype;
1821

1922
module.exports = cluster;
2023

@@ -49,7 +52,7 @@ cluster._setupWorker = function() {
4952
if (message.act === 'newconn')
5053
onconnection(message, handle);
5154
else if (message.act === 'disconnect')
52-
_disconnect.call(worker, true);
55+
ReflectApply(_disconnect, worker, [true]);
5356
}
5457
};
5558

@@ -62,10 +65,13 @@ cluster._getServer = function(obj, options, cb) {
6265
process.platform !== 'win32')
6366
address = path.resolve(address);
6467

65-
const indexesKey = [address,
66-
options.port,
67-
options.addressType,
68-
options.fd ].join(':');
68+
const indexesKey = ArrayPrototypeJoin(
69+
[
70+
address,
71+
options.port,
72+
options.addressType,
73+
options.fd,
74+
], ':');
6975

7076
let index = indexes.get(indexesKey);
7177

@@ -119,7 +125,7 @@ function shared(message, handle, indexesKey, cb) {
119125
send({ act: 'close', key });
120126
handles.delete(key);
121127
indexes.delete(indexesKey);
122-
return close.apply(handle, arguments);
128+
return ReflectApply(close, handle, arguments);
123129
};
124130
assert(handles.has(key) === false);
125131
handles.set(key, handle);
@@ -228,9 +234,9 @@ function _disconnect(masterInitiated) {
228234

229235
// Extend generic Worker with methods specific to worker processes.
230236
Worker.prototype.disconnect = function() {
231-
if (![ 'disconnecting', 'destroying' ].includes(this.state)) {
237+
if (this.state !== 'disconnecting' && this.state !== 'destroying') {
232238
this.state = 'disconnecting';
233-
_disconnect.call(this);
239+
ReflectApply(_disconnect, this, []);
234240
}
235241

236242
return this;

lib/internal/cluster/master.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
'use strict';
22

33
const {
4-
Map,
4+
ArrayPrototypePush,
5+
ArrayPrototypeSlice,
6+
ArrayPrototypeSome,
57
ObjectKeys,
68
ObjectValues,
9+
RegExpPrototypeTest,
10+
SafeMap,
11+
StringPrototypeStartsWith,
712
} = primordials;
813

914
const assert = require('internal/assert');
@@ -23,7 +28,7 @@ const { validatePort } = require('internal/validators');
2328

2429
module.exports = cluster;
2530

26-
const handles = new Map();
31+
const handles = new SafeMap();
2732
cluster.isWorker = false;
2833
cluster.isMaster = true;
2934
cluster.Worker = Worker;
@@ -53,7 +58,7 @@ cluster.schedulingPolicy = schedulingPolicy;
5358

5459
cluster.setupMaster = function(options) {
5560
const settings = {
56-
args: process.argv.slice(2),
61+
args: ArrayPrototypeSlice(process.argv, 2),
5762
exec: process.argv[1],
5863
execArgv: process.execArgv,
5964
silent: false,
@@ -65,8 +70,10 @@ cluster.setupMaster = function(options) {
6570
// Without --logfile=v8-%p.log, everything ends up in a single, unusable
6671
// file. (Unusable because what V8 logs are memory addresses and each
6772
// process has its own memory mappings.)
68-
if (settings.execArgv.some((s) => s.startsWith('--prof')) &&
69-
!settings.execArgv.some((s) => s.startsWith('--logfile='))) {
73+
if (ArrayPrototypeSome(settings.execArgv,
74+
(s) => StringPrototypeStartsWith(s, '--prof')) &&
75+
!ArrayPrototypeSome(settings.execArgv,
76+
(s) => StringPrototypeStartsWith(s, '--logfile='))) {
7077
settings.execArgv = [...settings.execArgv, '--logfile=v8-%p.log'];
7178
}
7279

@@ -109,8 +116,9 @@ function createWorkerProcess(id, env) {
109116
const nodeOptions = process.env.NODE_OPTIONS ?
110117
process.env.NODE_OPTIONS : '';
111118

112-
if (execArgv.some((arg) => arg.match(debugArgRegex)) ||
113-
nodeOptions.match(debugArgRegex)) {
119+
if (ArrayPrototypeSome(execArgv,
120+
(arg) => RegExpPrototypeTest(debugArgRegex, arg)) ||
121+
RegExpPrototypeTest(debugArgRegex, nodeOptions)) {
114122
let inspectPort;
115123
if ('inspectPort' in cluster.settings) {
116124
if (typeof cluster.settings.inspectPort === 'function')
@@ -126,7 +134,7 @@ function createWorkerProcess(id, env) {
126134
debugPortOffset++;
127135
}
128136

129-
execArgv.push(`--inspect-port=${inspectPort}`);
137+
ArrayPrototypePush(execArgv, `--inspect-port=${inspectPort}`);
130138
}
131139

132140
return fork(cluster.settings.exec, cluster.settings.args, {

lib/internal/cluster/round_robin_handle.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
const {
44
ArrayIsArray,
5+
ArrayPrototypePush,
6+
ArrayPrototypeShift,
57
Boolean,
6-
Map,
8+
SafeMap,
79
} = primordials;
810

911
const assert = require('internal/assert');
@@ -15,8 +17,8 @@ module.exports = RoundRobinHandle;
1517

1618
function RoundRobinHandle(key, address, { port, fd, flags }) {
1719
this.key = key;
18-
this.all = new Map();
19-
this.free = new Map();
20+
this.all = new SafeMap();
21+
this.free = new SafeMap();
2022
this.handles = [];
2123
this.handle = null;
2224
this.server = net.createServer(assert.fail);
@@ -90,7 +92,7 @@ RoundRobinHandle.prototype.remove = function(worker) {
9092
};
9193

9294
RoundRobinHandle.prototype.distribute = function(err, handle) {
93-
this.handles.push(handle);
95+
ArrayPrototypePush(this.handles, handle);
9496
const [ workerEntry ] = this.free;
9597

9698
if (ArrayIsArray(workerEntry)) {
@@ -105,7 +107,7 @@ RoundRobinHandle.prototype.handoff = function(worker) {
105107
return; // Worker is closing (or has closed) the server.
106108
}
107109

108-
const handle = this.handles.shift();
110+
const handle = ArrayPrototypeShift(this.handles);
109111

110112
if (handle === undefined) {
111113
this.free.set(worker.id, worker); // Add to ready queue again.

lib/internal/cluster/shared_handle.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
const { Map } = primordials;
2+
const { SafeMap } = primordials;
33
const assert = require('internal/assert');
44
const dgram = require('internal/dgram');
55
const net = require('net');
@@ -8,7 +8,7 @@ module.exports = SharedHandle;
88

99
function SharedHandle(key, address, { port, addressType, fd, flags }) {
1010
this.key = key;
11-
this.workers = new Map();
11+
this.workers = new SafeMap();
1212
this.handle = null;
1313
this.errno = 0;
1414

lib/internal/cluster/utils.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
'use strict';
22

33
const {
4-
Map,
4+
ReflectApply,
5+
SafeMap,
56
} = primordials;
67

78
module.exports = {
89
sendHelper,
910
internal
1011
};
1112

12-
const callbacks = new Map();
13+
const callbacks = new SafeMap();
1314
let seq = 0;
1415

1516
function sendHelper(proc, message, handle, cb) {
@@ -44,6 +45,6 @@ function internal(worker, cb) {
4445
}
4546
}
4647

47-
fn.apply(worker, arguments);
48+
ReflectApply(fn, worker, arguments);
4849
};
4950
}

lib/internal/cluster/worker.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
ObjectSetPrototypeOf,
5+
ReflectApply,
56
} = primordials;
67

78
const EventEmitter = require('events');
@@ -13,7 +14,7 @@ function Worker(options) {
1314
if (!(this instanceof Worker))
1415
return new Worker(options);
1516

16-
EventEmitter.call(this);
17+
ReflectApply(EventEmitter, this, []);
1718

1819
if (options === null || typeof options !== 'object')
1920
options = {};
@@ -38,11 +39,11 @@ ObjectSetPrototypeOf(Worker.prototype, EventEmitter.prototype);
3839
ObjectSetPrototypeOf(Worker, EventEmitter);
3940

4041
Worker.prototype.kill = function() {
41-
this.destroy.apply(this, arguments);
42+
ReflectApply(this.destroy, this, arguments);
4243
};
4344

4445
Worker.prototype.send = function() {
45-
return this.process.send.apply(this.process, arguments);
46+
return ReflectApply(this.process.send, this.process, arguments);
4647
};
4748

4849
Worker.prototype.isDead = function() {

0 commit comments

Comments
 (0)