cluster: restructure to same prototype for cluster child

Since `rr` and `shared` both belongs to the same prototype declaration
and differes only in the handler declaration, this can be abstracted to
a same type of function arguments passing.

PR-URL: https://github.com/nodejs/node/pull/36610
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Yash Ladha
2020-12-23 18:01:05 +05:30
committed by James M Snell
parent 5694f7f0bf
commit c199989c0c

View File

@@ -101,10 +101,13 @@ cluster._getServer = function(obj, options, cb) {
if (typeof obj._setServerData === 'function')
obj._setServerData(reply.data);
if (handle)
shared(reply, handle, indexesKey, index, cb); // Shared listen socket.
else
rr(reply, indexesKey, index, cb); // Round-robin.
if (handle) {
// Shared listen socket
shared(reply, { handle, indexesKey, index }, cb);
} else {
// Round-robin.
rr(reply, { indexesKey, index }, cb);
}
});
obj.once('listening', () => {
@@ -129,7 +132,7 @@ function removeIndexesKey(indexesKey, index) {
}
// Shared listen socket.
function shared(message, handle, indexesKey, index, cb) {
function shared(message, { handle, indexesKey, index }, cb) {
const key = message.key;
// Monkey-patch the close() method so we can keep track of when it's
// closed. Avoids resource leaks when the handle is short-lived.
@@ -146,8 +149,8 @@ function shared(message, handle, indexesKey, index, cb) {
cb(message.errno, handle);
}
// Round-robin. Primary distributes handles across workers.
function rr(message, indexesKey, index, cb) {
// Round-robin. Master distributes handles across workers.
function rr(message, { indexesKey, index }, cb) {
if (message.errno)
return cb(message.errno, null);