Skip to content

Commit

Permalink
lib: refactor to use mapping in cluster master
Browse files Browse the repository at this point in the history
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 <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
yashLadha authored and danielleadams committed Jan 12, 2021
1 parent 153be6c commit 95219ea
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lib/internal/cluster/master.js
Expand Up @@ -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) {
Expand Down

0 comments on commit 95219ea

Please sign in to comment.