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

lib: changed functional logic in cluster schedulers #32505

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions lib/internal/cluster/round_robin_handle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
ArrayIsArray,
Boolean,
Map,
} = primordials;
Expand All @@ -15,7 +16,7 @@ module.exports = RoundRobinHandle;
function RoundRobinHandle(key, address, port, addressType, fd, flags) {
this.key = key;
this.all = new Map();
this.free = [];
this.free = new Map();
this.handles = [];
this.handle = null;
this.server = net.createServer(assert.fail);
Expand Down Expand Up @@ -73,10 +74,7 @@ RoundRobinHandle.prototype.remove = function(worker) {
if (!existed)
return false;

const index = this.free.indexOf(worker);

if (index !== -1)
this.free.splice(index, 1);
this.free.delete(worker.id);

if (this.all.size !== 0)
return false;
Expand All @@ -93,21 +91,24 @@ RoundRobinHandle.prototype.remove = function(worker) {

RoundRobinHandle.prototype.distribute = function(err, handle) {
this.handles.push(handle);
const worker = this.free.shift();
const [ workerEntry ] = this.free;

if (worker)
if (ArrayIsArray(workerEntry)) {
const [ workerId, worker ] = workerEntry;
this.free.delete(workerId);
this.handoff(worker);
}
};

RoundRobinHandle.prototype.handoff = function(worker) {
if (this.all.has(worker.id) === false) {
if (!this.all.has(worker.id)) {
return; // Worker is closing (or has closed) the server.
}

const handle = this.handles.shift();

if (handle === undefined) {
this.free.push(worker); // Add to ready queue again.
this.free.set(worker.id, worker); // Add to ready queue again.
return;
}

Expand Down
17 changes: 8 additions & 9 deletions lib/internal/cluster/shared_handle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
const { Map } = primordials;
const assert = require('internal/assert');
const dgram = require('internal/dgram');
const net = require('net');
Expand All @@ -7,7 +8,7 @@ module.exports = SharedHandle;

function SharedHandle(key, address, port, addressType, fd, flags) {
this.key = key;
this.workers = [];
this.workers = new Map();
this.handle = null;
this.errno = 0;

Expand All @@ -24,20 +25,18 @@ function SharedHandle(key, address, port, addressType, fd, flags) {
}

SharedHandle.prototype.add = function(worker, send) {
assert(!this.workers.includes(worker));
this.workers.push(worker);
assert(!this.workers.has(worker.id));
this.workers.set(worker.id, worker);
send(this.errno, null, this.handle);
};

SharedHandle.prototype.remove = function(worker) {
const index = this.workers.indexOf(worker);

if (index === -1)
return false; // The worker wasn't sharing this handle.
if (!this.workers.has(worker.id))
return false;

this.workers.splice(index, 1);
this.workers.delete(worker.id);

if (this.workers.length !== 0)
if (this.workers.size !== 0)
return false;

this.handle.close();
Expand Down