Skip to content

Commit 64f840a

Browse files
cjihrigtargos
authored andcommitted
cluster: use Map to track round robin workers
PR-URL: #23125 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Denys Otrishko <[email protected]>
1 parent 22f51a6 commit 64f840a

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

lib/internal/cluster/round_robin_handle.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
const assert = require('assert');
33
const net = require('net');
44
const { sendHelper } = require('internal/cluster/utils');
5-
const getOwnPropertyNames = Object.getOwnPropertyNames;
65
const uv = process.binding('uv');
76

87
module.exports = RoundRobinHandle;
98

109
function RoundRobinHandle(key, address, port, addressType, fd) {
1110
this.key = key;
12-
this.all = {};
11+
this.all = new Map();
1312
this.free = [];
1413
this.handles = [];
1514
this.handle = null;
@@ -31,8 +30,8 @@ function RoundRobinHandle(key, address, port, addressType, fd) {
3130
}
3231

3332
RoundRobinHandle.prototype.add = function(worker, send) {
34-
assert(worker.id in this.all === false);
35-
this.all[worker.id] = worker;
33+
assert(this.all.has(worker.id) === false);
34+
this.all.set(worker.id, worker);
3635

3736
const done = () => {
3837
if (this.handle.getsockname) {
@@ -61,16 +60,17 @@ RoundRobinHandle.prototype.add = function(worker, send) {
6160
};
6261

6362
RoundRobinHandle.prototype.remove = function(worker) {
64-
if (worker.id in this.all === false)
63+
const existed = this.all.delete(worker.id);
64+
65+
if (!existed)
6566
return false;
6667

67-
delete this.all[worker.id];
6868
const index = this.free.indexOf(worker);
6969

7070
if (index !== -1)
7171
this.free.splice(index, 1);
7272

73-
if (getOwnPropertyNames(this.all).length !== 0)
73+
if (this.all.size !== 0)
7474
return false;
7575

7676
for (var handle; handle = this.handles.shift(); handle.close())
@@ -90,7 +90,7 @@ RoundRobinHandle.prototype.distribute = function(err, handle) {
9090
};
9191

9292
RoundRobinHandle.prototype.handoff = function(worker) {
93-
if (worker.id in this.all === false) {
93+
if (this.all.has(worker.id) === false) {
9494
return; // Worker is closing (or has closed) the server.
9595
}
9696

0 commit comments

Comments
 (0)