Skip to content

Commit e54108f

Browse files
aduh95codebytere
authored andcommitted
cluster: refactor to use more primordials
PR-URL: #36011 Reviewed-By: Rich Trott <[email protected]>
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‎

Lines changed: 18 additions & 12 deletions
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
constassert=require('internal/assert');
@@ -12,9 +15,9 @@ const{owner_symbol } = require('internal/async_hooks').symbols;
1215
constWorker=require('internal/cluster/worker');
1316
const{ internal, sendHelper }=require('internal/cluster/utils');
1417
constcluster=newEventEmitter();
15-
consthandles=newMap();
16-
constindexes=newMap();
17-
constnoop=()=>{};
18+
consthandles=newSafeMap();
19+
constindexes=newSafeMap();
20+
constnoop=FunctionPrototype;
1821

1922
module.exports=cluster;
2023

@@ -49,7 +52,7 @@ cluster._setupWorker = function(){
4952
if(message.act==='newconn')
5053
onconnection(message,handle);
5154
elseif(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-
constindexesKey=[address,
66-
options.port,
67-
options.addressType,
68-
options.fd].join(':');
68+
constindexesKey=ArrayPrototypeJoin(
69+
[
70+
address,
71+
options.port,
72+
options.addressType,
73+
options.fd,
74+
],':');
6975

7076
letindex=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-
returnclose.apply(handle,arguments);
128+
returnReflectApply(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
returnthis;

‎lib/internal/cluster/master.js‎

Lines changed: 16 additions & 8 deletions
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
constassert=require('internal/assert');
@@ -23,7 +28,7 @@ const{validatePort } = require('internal/validators');
2328

2429
module.exports=cluster;
2530

26-
consthandles=newMap();
31+
consthandles=newSafeMap();
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
constsettings={
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
constnodeOptions=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
letinspectPort;
115123
if('inspectPort'incluster.settings){
116124
if(typeofcluster.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
returnfork(cluster.settings.exec,cluster.settings.args,{

‎lib/internal/cluster/round_robin_handle.js‎

Lines changed: 7 additions & 5 deletions
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
constassert=require('internal/assert');
@@ -15,8 +17,8 @@ module.exports = RoundRobinHandle;
1517

1618
functionRoundRobinHandle(key,address,{ port, fd, flags }){
1719
this.key=key;
18-
this.all=newMap();
19-
this.free=newMap();
20+
this.all=newSafeMap();
21+
this.free=newSafeMap();
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-
consthandle=this.handles.shift();
110+
consthandle=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‎

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

99
functionSharedHandle(key,address,{ port, addressType, fd, flags }){
1010
this.key=key;
11-
this.workers=newMap();
11+
this.workers=newSafeMap();
1212
this.handle=null;
1313
this.errno=0;
1414

‎lib/internal/cluster/utils.js‎

Lines changed: 4 additions & 3 deletions
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-
constcallbacks=newMap();
13+
constcallbacks=newSafeMap();
1314
letseq=0;
1415

1516
functionsendHelper(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‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

78
constEventEmitter=require('events');
@@ -13,7 +14,7 @@ function Worker(options){
1314
if(!(thisinstanceofWorker))
1415
returnnewWorker(options);
1516

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

1819
if(options===null||typeofoptions!=='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-
returnthis.process.send.apply(this.process,arguments);
46+
returnReflectApply(this.process.send,this.process,arguments);
4647
};
4748

4849
Worker.prototype.isDead=function(){

0 commit comments

Comments
(0)