cluster: use Map to track round robin workers
PR-URL: https://github.com/nodejs/node/pull/23125 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com>
This commit is contained in:
parent
97dad7e33b
commit
6654c59452
@ -2,7 +2,6 @@
|
|||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const net = require('net');
|
const net = require('net');
|
||||||
const { sendHelper } = require('internal/cluster/utils');
|
const { sendHelper } = require('internal/cluster/utils');
|
||||||
const getOwnPropertyNames = Object.getOwnPropertyNames;
|
|
||||||
const { internalBinding } = require('internal/bootstrap/loaders');
|
const { internalBinding } = require('internal/bootstrap/loaders');
|
||||||
const uv = internalBinding('uv');
|
const uv = internalBinding('uv');
|
||||||
|
|
||||||
@ -10,7 +9,7 @@ module.exports = RoundRobinHandle;
|
|||||||
|
|
||||||
function RoundRobinHandle(key, address, port, addressType, fd) {
|
function RoundRobinHandle(key, address, port, addressType, fd) {
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.all = {};
|
this.all = new Map();
|
||||||
this.free = [];
|
this.free = [];
|
||||||
this.handles = [];
|
this.handles = [];
|
||||||
this.handle = null;
|
this.handle = null;
|
||||||
@ -32,8 +31,8 @@ function RoundRobinHandle(key, address, port, addressType, fd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
RoundRobinHandle.prototype.add = function(worker, send) {
|
RoundRobinHandle.prototype.add = function(worker, send) {
|
||||||
assert(worker.id in this.all === false);
|
assert(this.all.has(worker.id) === false);
|
||||||
this.all[worker.id] = worker;
|
this.all.set(worker.id, worker);
|
||||||
|
|
||||||
const done = () => {
|
const done = () => {
|
||||||
if (this.handle.getsockname) {
|
if (this.handle.getsockname) {
|
||||||
@ -62,16 +61,17 @@ RoundRobinHandle.prototype.add = function(worker, send) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
RoundRobinHandle.prototype.remove = function(worker) {
|
RoundRobinHandle.prototype.remove = function(worker) {
|
||||||
if (worker.id in this.all === false)
|
const existed = this.all.delete(worker.id);
|
||||||
|
|
||||||
|
if (!existed)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
delete this.all[worker.id];
|
|
||||||
const index = this.free.indexOf(worker);
|
const index = this.free.indexOf(worker);
|
||||||
|
|
||||||
if (index !== -1)
|
if (index !== -1)
|
||||||
this.free.splice(index, 1);
|
this.free.splice(index, 1);
|
||||||
|
|
||||||
if (getOwnPropertyNames(this.all).length !== 0)
|
if (this.all.size !== 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (var handle; handle = this.handles.shift(); handle.close())
|
for (var handle; handle = this.handles.shift(); handle.close())
|
||||||
@ -91,7 +91,7 @@ RoundRobinHandle.prototype.distribute = function(err, handle) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
RoundRobinHandle.prototype.handoff = function(worker) {
|
RoundRobinHandle.prototype.handoff = function(worker) {
|
||||||
if (worker.id in this.all === false) {
|
if (this.all.has(worker.id) === false) {
|
||||||
return; // Worker is closing (or has closed) the server.
|
return; // Worker is closing (or has closed) the server.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user