Skip to content

Commit 95219ea

Browse files
yashLadhadanielleadams
authored andcommitted
lib: refactor to use mapping in cluster master
Cluster master message handler is basically doing the same thing for different message actions which can be avoided. Thus move to method mapping object and doing a single lookup to find the function to execute and it doesnot exists, skip the execution chain. PR-URL: #36250 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 153be6c commit 95219ea

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

lib/internal/cluster/master.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -248,19 +248,21 @@ cluster.disconnect = function(cb) {
248248
intercom.once('disconnect', cb);
249249
};
250250

251+
const methodMessageMapping = {
252+
close,
253+
exitedAfterDisconnect,
254+
listening,
255+
online,
256+
queryServer,
257+
};
258+
251259
function onmessage(message, handle) {
252260
const worker = this;
253261

254-
if (message.act === 'online')
255-
online(worker);
256-
else if (message.act === 'queryServer')
257-
queryServer(worker, message);
258-
else if (message.act === 'listening')
259-
listening(worker, message);
260-
else if (message.act === 'exitedAfterDisconnect')
261-
exitedAfterDisconnect(worker, message);
262-
else if (message.act === 'close')
263-
close(worker, message);
262+
const fn = methodMessageMapping[message.act];
263+
264+
if (typeof fn === 'function')
265+
fn(worker, message);
264266
}
265267

266268
function online(worker) {

0 commit comments

Comments
 (0)