Skip to content

Commit 7dd001c

Browse files
yashLadhatargos
authored andcommittedApr 22, 2020
lib: changed functional logic in cluster schedulers
Free pool in round_robin scheduler is implemented as an array. There were constant lookups being for distributing load on other workers in free pool. Reimplementing in Map will create will be more performant as compared to Array implementation. This was done for all in past but free wasn't implemented at that time. PR-URL: #32505 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 06d16cf commit 7dd001c

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed
 

‎lib/internal/cluster/round_robin_handle.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const {
4+
ArrayIsArray,
45
Boolean,
56
Map,
67
} = primordials;
@@ -16,7 +17,7 @@ module.exports = RoundRobinHandle;
1617
function RoundRobinHandle(key, address, port, addressType, fd, flags) {
1718
this.key = key;
1819
this.all = new Map();
19-
this.free = [];
20+
this.free = new Map();
2021
this.handles = [];
2122
this.handle = null;
2223
this.server = net.createServer(assert.fail);
@@ -77,10 +78,7 @@ RoundRobinHandle.prototype.remove = function(worker) {
7778
if (!existed)
7879
return false;
7980

80-
const index = this.free.indexOf(worker);
81-
82-
if (index !== -1)
83-
this.free.splice(index, 1);
81+
this.free.delete(worker.id);
8482

8583
if (this.all.size !== 0)
8684
return false;
@@ -97,21 +95,24 @@ RoundRobinHandle.prototype.remove = function(worker) {
9795

9896
RoundRobinHandle.prototype.distribute = function(err, handle) {
9997
this.handles.push(handle);
100-
const worker = this.free.shift();
98+
const [ workerEntry ] = this.free;
10199

102-
if (worker)
100+
if (ArrayIsArray(workerEntry)) {
101+
const [ workerId, worker ] = workerEntry;
102+
this.free.delete(workerId);
103103
this.handoff(worker);
104+
}
104105
};
105106

106107
RoundRobinHandle.prototype.handoff = function(worker) {
107-
if (this.all.has(worker.id) === false) {
108+
if (!this.all.has(worker.id)) {
108109
return; // Worker is closing (or has closed) the server.
109110
}
110111

111112
const handle = this.handles.shift();
112113

113114
if (handle === undefined) {
114-
this.free.push(worker); // Add to ready queue again.
115+
this.free.set(worker.id, worker); // Add to ready queue again.
115116
return;
116117
}
117118

‎lib/internal/cluster/shared_handle.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
const { Map } = primordials;
23
const assert = require('internal/assert');
34
const dgram = require('internal/dgram');
45
const net = require('net');
@@ -7,7 +8,7 @@ module.exports = SharedHandle;
78

89
function SharedHandle(key, address, port, addressType, fd, flags) {
910
this.key = key;
10-
this.workers = [];
11+
this.workers = new Map();
1112
this.handle = null;
1213
this.errno = 0;
1314

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

2627
SharedHandle.prototype.add = function(worker, send) {
27-
assert(!this.workers.includes(worker));
28-
this.workers.push(worker);
28+
assert(!this.workers.has(worker.id));
29+
this.workers.set(worker.id, worker);
2930
send(this.errno, null, this.handle);
3031
};
3132

3233
SharedHandle.prototype.remove = function(worker) {
33-
const index = this.workers.indexOf(worker);
34-
35-
if (index === -1)
36-
return false; // The worker wasn't sharing this handle.
34+
if (!this.workers.has(worker.id))
35+
return false;
3736

38-
this.workers.splice(index, 1);
37+
this.workers.delete(worker.id);
3938

40-
if (this.workers.length !== 0)
39+
if (this.workers.size !== 0)
4140
return false;
4241

4342
this.handle.close();

0 commit comments

Comments
 (0)
Please sign in to comment.