Skip to content

Commit

Permalink
cluster: use Map to track round robin workers
Browse files Browse the repository at this point in the history
PR-URL: #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>
  • Loading branch information
cjihrig authored and targos committed Oct 3, 2018
1 parent 22f51a6 commit 64f840a
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/internal/cluster/round_robin_handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
const assert = require('assert');
const net = require('net');
const { sendHelper } = require('internal/cluster/utils');
const getOwnPropertyNames = Object.getOwnPropertyNames;
const uv = process.binding('uv');

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;
Expand All @@ -31,8 +30,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) {
Expand Down Expand Up @@ -61,16 +60,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())
Expand All @@ -90,7 +90,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.
}

Expand Down

0 comments on commit 64f840a

Please sign in to comment.