cluster: use linkedlist for round_robin_handle

PR-URL: https://github.com/nodejs/node/pull/40615
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
twchn
2021-10-26 23:54:27 +08:00
committed by Rich Trott
parent 265a47d680
commit 4b65dec76d
2 changed files with 12 additions and 8 deletions

View File

@@ -2,15 +2,15 @@
const {
ArrayIsArray,
ArrayPrototypePush,
ArrayPrototypeShift,
Boolean,
ObjectCreate,
SafeMap,
} = primordials;
const assert = require('internal/assert');
const net = require('net');
const { sendHelper } = require('internal/cluster/utils');
const { append, init, isEmpty, peek, remove } = require('internal/linkedlist');
const { constants } = internalBinding('tcp_wrap');
module.exports = RoundRobinHandle;
@@ -19,7 +19,7 @@ function RoundRobinHandle(key, address, { port, fd, flags }) {
this.key = key;
this.all = new SafeMap();
this.free = new SafeMap();
this.handles = [];
this.handles = init(ObjectCreate(null));
this.handle = null;
this.server = net.createServer(assert.fail);
@@ -81,10 +81,11 @@ RoundRobinHandle.prototype.remove = function(worker) {
if (this.all.size !== 0)
return false;
for (const handle of this.handles) {
while (!isEmpty(this.handles)) {
const handle = peek(this.handles);
handle.close();
remove(handle);
}
this.handles = [];
this.handle.close();
this.handle = null;
@@ -92,7 +93,7 @@ RoundRobinHandle.prototype.remove = function(worker) {
};
RoundRobinHandle.prototype.distribute = function(err, handle) {
ArrayPrototypePush(this.handles, handle);
append(this.handles, handle);
// eslint-disable-next-line node-core/no-array-destructuring
const [ workerEntry ] = this.free; // this.free is a SafeMap
@@ -108,13 +109,15 @@ RoundRobinHandle.prototype.handoff = function(worker) {
return; // Worker is closing (or has closed) the server.
}
const handle = ArrayPrototypeShift(this.handles);
const handle = peek(this.handles);
if (handle === undefined) {
if (handle === null) {
this.free.set(worker.id, worker); // Add to ready queue again.
return;
}
remove(handle);
const message = { act: 'newconn', key: this.key };
sendHelper(worker.process, message, handle, (reply) => {

View File

@@ -3,6 +3,7 @@
function init(list) {
list._idleNext = list;
list._idlePrev = list;
return list;
}
// Show the most idle item.