Skip to content

Commit

Permalink
cluster: use linkedlist for round_robin_handle
Browse files Browse the repository at this point in the history
PR-URL: #40615
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
twchn authored and danielleadams committed Dec 13, 2021
1 parent 033a646 commit e924dc7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
19 changes: 11 additions & 8 deletions lib/internal/cluster/round_robin_handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -81,18 +81,19 @@ 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;
return true;
};

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

Expand All @@ -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) => {
Expand Down
1 change: 1 addition & 0 deletions lib/internal/linkedlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
function init(list) {
list._idleNext = list;
list._idlePrev = list;
return list;
}

// Show the most idle item.
Expand Down

0 comments on commit e924dc7

Please sign in to comment.