Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: refactor to use mapping in cluster master #36250

Merged
merged 1 commit into from
Jan 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
yashLadha authored and aduh95 committed Jan 10, 2021
commit 88d8dde4796eebed13b091dc1457dbe070c89196
22 changes: 12 additions & 10 deletions lib/internal/cluster/primary.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,19 +252,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