GeezerVison is showing github.com — cluster: refactor to use more primordials · nodejs/node@929e127
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/cluster/child.js +18-12Lines changed: 18 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | 3 | const { | 4 | | - Map, | | 4 | + ArrayPrototypeJoin, | | 5 | + FunctionPrototype, | 5 | 6 | ObjectAssign, | | 7 | + ReflectApply, | | 8 | + SafeMap, | 6 | 9 | } = primordials; | 7 | 10 |
| 8 | 11 | const assert = require('internal/assert'); | @@ -12,9 +15,9 @@ const { owner_symbol } = require('internal/async_hooks').symbols; | 12 | 15 | const Worker = require('internal/cluster/worker'); | 13 | 16 | const { internal, sendHelper } = require('internal/cluster/utils'); | 14 | 17 | 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; | 18 | 21 |
| 19 | 22 | module.exports = cluster; | 20 | 23 |
| @@ -49,7 +52,7 @@ cluster._setupWorker = function() { | 49 | 52 | if (message.act === 'newconn') | 50 | 53 | onconnection(message, handle); | 51 | 54 | else if (message.act === 'disconnect') | 52 | | - _disconnect.call(worker, true); | | 55 | + ReflectApply(_disconnect, worker, [true]); | 53 | 56 | } | 54 | 57 | }; | 55 | 58 |
| @@ -62,10 +65,13 @@ cluster._getServer = function(obj, options, cb) { | 62 | 65 | process.platform !== 'win32') | 63 | 66 | address = path.resolve(address); | 64 | 67 |
| 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 | + ], ':'); | 69 | 75 |
| 70 | 76 | let index = indexes.get(indexesKey); | 71 | 77 |
| @@ -119,7 +125,7 @@ function shared(message, handle, indexesKey, cb) { | 119 | 125 | send({ act: 'close', key }); | 120 | 126 | handles.delete(key); | 121 | 127 | indexes.delete(indexesKey); | 122 | | - return close.apply(handle, arguments); | | 128 | + return ReflectApply(close, handle, arguments); | 123 | 129 | }; | 124 | 130 | assert(handles.has(key) === false); | 125 | 131 | handles.set(key, handle); | @@ -228,9 +234,9 @@ function _disconnect(masterInitiated) { | 228 | 234 |
| 229 | 235 | // Extend generic Worker with methods specific to worker processes. | 230 | 236 | Worker.prototype.disconnect = function() { | 231 | | - if (![ 'disconnecting', 'destroying' ].includes(this.state)) { | | 237 | + if (this.state !== 'disconnecting' && this.state !== 'destroying') { | 232 | 238 | this.state = 'disconnecting'; | 233 | | - _disconnect.call(this); | | 239 | + ReflectApply(_disconnect, this, []); | 234 | 240 | } | 235 | 241 |
| 236 | 242 | return this; | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/cluster/master.js +16-8Lines changed: 16 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | 3 | const { | 4 | | - Map, | | 4 | + ArrayPrototypePush, | | 5 | + ArrayPrototypeSlice, | | 6 | + ArrayPrototypeSome, | 5 | 7 | ObjectKeys, | 6 | 8 | ObjectValues, | | 9 | + RegExpPrototypeTest, | | 10 | + SafeMap, | | 11 | + StringPrototypeStartsWith, | 7 | 12 | } = primordials; | 8 | 13 |
| 9 | 14 | const assert = require('internal/assert'); | @@ -23,7 +28,7 @@ const { validatePort } = require('internal/validators'); | 23 | 28 |
| 24 | 29 | module.exports = cluster; | 25 | 30 |
| 26 | | -const handles = new Map(); | | 31 | +const handles = new SafeMap(); | 27 | 32 | cluster.isWorker = false; | 28 | 33 | cluster.isMaster = true; | 29 | 34 | cluster.Worker = Worker; | @@ -53,7 +58,7 @@ cluster.schedulingPolicy = schedulingPolicy; | 53 | 58 |
| 54 | 59 | cluster.setupMaster = function(options) { | 55 | 60 | const settings = { | 56 | | - args: process.argv.slice(2), | | 61 | + args: ArrayPrototypeSlice(process.argv, 2), | 57 | 62 | exec: process.argv[1], | 58 | 63 | execArgv: process.execArgv, | 59 | 64 | silent: false, | @@ -65,8 +70,10 @@ cluster.setupMaster = function(options) { | 65 | 70 | // Without --logfile=v8-%p.log, everything ends up in a single, unusable | 66 | 71 | // file. (Unusable because what V8 logs are memory addresses and each | 67 | 72 | // 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='))) { | 70 | 77 | settings.execArgv = [...settings.execArgv, '--logfile=v8-%p.log']; | 71 | 78 | } | 72 | 79 |
| @@ -109,8 +116,9 @@ function createWorkerProcess(id, env) { | 109 | 116 | const nodeOptions = process.env.NODE_OPTIONS ? | 110 | 117 | process.env.NODE_OPTIONS : ''; | 111 | 118 |
| 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)) { | 114 | 122 | let inspectPort; | 115 | 123 | if ('inspectPort' in cluster.settings) { | 116 | 124 | if (typeof cluster.settings.inspectPort === 'function') | @@ -126,7 +134,7 @@ function createWorkerProcess(id, env) { | 126 | 134 | debugPortOffset++; | 127 | 135 | } | 128 | 136 |
| 129 | | - execArgv.push(`--inspect-port=${inspectPort}`); | | 137 | + ArrayPrototypePush(execArgv, `--inspect-port=${inspectPort}`); | 130 | 138 | } | 131 | 139 |
| 132 | 140 | return fork(cluster.settings.exec, cluster.settings.args, { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/cluster/round_robin_handle.js +7-5Lines changed: 7 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | const { | 4 | 4 | ArrayIsArray, | | 5 | + ArrayPrototypePush, | | 6 | + ArrayPrototypeShift, | 5 | 7 | Boolean, | 6 | | - Map, | | 8 | + SafeMap, | 7 | 9 | } = primordials; | 8 | 10 |
| 9 | 11 | const assert = require('internal/assert'); | @@ -15,8 +17,8 @@ module.exports = RoundRobinHandle; | 15 | 17 |
| 16 | 18 | function RoundRobinHandle(key, address, { port, fd, flags }) { | 17 | 19 | this.key = key; | 18 | | - this.all = new Map(); | 19 | | - this.free = new Map(); | | 20 | + this.all = new SafeMap(); | | 21 | + this.free = new SafeMap(); | 20 | 22 | this.handles = []; | 21 | 23 | this.handle = null; | 22 | 24 | this.server = net.createServer(assert.fail); | @@ -90,7 +92,7 @@ RoundRobinHandle.prototype.remove = function(worker) { | 90 | 92 | }; | 91 | 93 |
| 92 | 94 | RoundRobinHandle.prototype.distribute = function(err, handle) { | 93 | | - this.handles.push(handle); | | 95 | + ArrayPrototypePush(this.handles, handle); | 94 | 96 | const [ workerEntry ] = this.free; | 95 | 97 |
| 96 | 98 | if (ArrayIsArray(workerEntry)) { | @@ -105,7 +107,7 @@ RoundRobinHandle.prototype.handoff = function(worker) { | 105 | 107 | return; // Worker is closing (or has closed) the server. | 106 | 108 | } | 107 | 109 |
| 108 | | - const handle = this.handles.shift(); | | 110 | + const handle = ArrayPrototypeShift(this.handles); | 109 | 111 |
| 110 | 112 | if (handle === undefined) { | 111 | 113 | this.free.set(worker.id, worker); // Add to ready queue again. | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/cluster/shared_handle.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | | -const { Map } = primordials; | | 2 | +const { SafeMap } = primordials; | 3 | 3 | const assert = require('internal/assert'); | 4 | 4 | const dgram = require('internal/dgram'); | 5 | 5 | const net = require('net'); | @@ -8,7 +8,7 @@ module.exports = SharedHandle; | 8 | 8 |
| 9 | 9 | function SharedHandle(key, address, { port, addressType, fd, flags }) { | 10 | 10 | this.key = key; | 11 | | - this.workers = new Map(); | | 11 | + this.workers = new SafeMap(); | 12 | 12 | this.handle = null; | 13 | 13 | this.errno = 0; | 14 | 14 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/cluster/utils.js +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | 3 | const { | 4 | | - Map, | | 4 | + ReflectApply, | | 5 | + SafeMap, | 5 | 6 | } = primordials; | 6 | 7 |
| 7 | 8 | module.exports = { | 8 | 9 | sendHelper, | 9 | 10 | internal | 10 | 11 | }; | 11 | 12 |
| 12 | | -const callbacks = new Map(); | | 13 | +const callbacks = new SafeMap(); | 13 | 14 | let seq = 0; | 14 | 15 |
| 15 | 16 | function sendHelper(proc, message, handle, cb) { | @@ -44,6 +45,6 @@ function internal(worker, cb) { | 44 | 45 | } | 45 | 46 | } | 46 | 47 |
| 47 | | - fn.apply(worker, arguments); | | 48 | + ReflectApply(fn, worker, arguments); | 48 | 49 | }; | 49 | 50 | } |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/cluster/worker.js +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | const { | 4 | 4 | ObjectSetPrototypeOf, | | 5 | + ReflectApply, | 5 | 6 | } = primordials; | 6 | 7 |
| 7 | 8 | const EventEmitter = require('events'); | @@ -13,7 +14,7 @@ function Worker(options) { | 13 | 14 | if (!(this instanceof Worker)) | 14 | 15 | return new Worker(options); | 15 | 16 |
| 16 | | - EventEmitter.call(this); | | 17 | + ReflectApply(EventEmitter, this, []); | 17 | 18 |
| 18 | 19 | if (options === null || typeof options !== 'object') | 19 | 20 | options = {}; | @@ -38,11 +39,11 @@ ObjectSetPrototypeOf(Worker.prototype, EventEmitter.prototype); | 38 | 39 | ObjectSetPrototypeOf(Worker, EventEmitter); | 39 | 40 |
| 40 | 41 | Worker.prototype.kill = function() { | 41 | | - this.destroy.apply(this, arguments); | | 42 | + ReflectApply(this.destroy, this, arguments); | 42 | 43 | }; | 43 | 44 |
| 44 | 45 | Worker.prototype.send = function() { | 45 | | - return this.process.send.apply(this.process, arguments); | | 46 | + return ReflectApply(this.process.send, this.process, arguments); | 46 | 47 | }; | 47 | 48 |
| 48 | 49 | Worker.prototype.isDead = function() { | |
|