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: https://github.com/nodejs/node/pull/36250
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Yash Ladha authored and Danielle Adams committed 2021-01-12 07:10:56 -05:00
1 parent 153be6c80e
commit 95219eac08
1 file changed
+12 -10
+12 -10
View File
@@ -248,19 +248,21 @@ cluster.disconnect = function(cb) {
intercom.once('disconnect', cb);
};
const methodMessageMapping = {
close,
exitedAfterDisconnect,
listening,
online,
queryServer,
};
function onmessage(message, handle) {
const worker = this;
if (message.act === 'online')
online(worker);
else if (message.act === 'queryServer')
queryServer(worker, message);
else if (message.act === 'listening')
listening(worker, message);
else if (message.act === 'exitedAfterDisconnect')
exitedAfterDisconnect(worker, message);
else if (message.act === 'close')
close(worker, message);
const fn = methodMessageMapping[message.act];
if (typeof fn === 'function')
fn(worker, message);
}
function online(worker) {