From 6654c5945263a3ced718b94a89c70fdc9b061500 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Thu, 27 Sep 2018 09:49:38 -0400 Subject: [PATCH] cluster: use Map to track round robin workers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/23125 Reviewed-By: Gus Caplan Reviewed-By: Michaƫl Zasso Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Trivikram Kamat Reviewed-By: Denys Otrishko --- lib/internal/cluster/round_robin_handle.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/internal/cluster/round_robin_handle.js b/lib/internal/cluster/round_robin_handle.js index e351f433ab8..9f09d9fbf76 100644 --- a/lib/internal/cluster/round_robin_handle.js +++ b/lib/internal/cluster/round_robin_handle.js @@ -2,7 +2,6 @@ const assert = require('assert'); const net = require('net'); const { sendHelper } = require('internal/cluster/utils'); -const getOwnPropertyNames = Object.getOwnPropertyNames; const { internalBinding } = require('internal/bootstrap/loaders'); const uv = internalBinding('uv'); @@ -10,7 +9,7 @@ module.exports = RoundRobinHandle; function RoundRobinHandle(key, address, port, addressType, fd) { this.key = key; - this.all = {}; + this.all = new Map(); this.free = []; this.handles = []; this.handle = null; @@ -32,8 +31,8 @@ function RoundRobinHandle(key, address, port, addressType, fd) { } RoundRobinHandle.prototype.add = function(worker, send) { - assert(worker.id in this.all === false); - this.all[worker.id] = worker; + assert(this.all.has(worker.id) === false); + this.all.set(worker.id, worker); const done = () => { if (this.handle.getsockname) { @@ -62,16 +61,17 @@ RoundRobinHandle.prototype.add = function(worker, send) { }; RoundRobinHandle.prototype.remove = function(worker) { - if (worker.id in this.all === false) + const existed = this.all.delete(worker.id); + + if (!existed) return false; - delete this.all[worker.id]; const index = this.free.indexOf(worker); if (index !== -1) this.free.splice(index, 1); - if (getOwnPropertyNames(this.all).length !== 0) + if (this.all.size !== 0) return false; for (var handle; handle = this.handles.shift(); handle.close()) @@ -91,7 +91,7 @@ RoundRobinHandle.prototype.distribute = function(err, handle) { }; 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. }