mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
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:
@@ -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) => {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
function init(list) {
|
||||
list._idleNext = list;
|
||||
list._idlePrev = list;
|
||||
return list;
|
||||
}
|
||||
|
||||
// Show the most idle item.
|
||||
|
||||
Reference in New Issue
Block a user