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

cluster: use linkedlist for round_robin_handle #40615

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 11 additions & 8 deletions lib/internal/cluster/round_robin_handle.js
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
Expand Up @@ -3,6 +3,7 @@
function init(list) {
list._idleNext = list;
list._idlePrev = list;
return list;
}

// Show the most idle item.
Expand Down