Skip to content

Commit 94251c4

Browse files
yashLadhatargos
authored andcommitted
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 <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent e0e73f6 commit 94251c4

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;
@@ -15,7 +16,7 @@ module.exports = RoundRobinHandle;
1516
function RoundRobinHandle(key, address, port, addressType, fd, flags) {
1617
this.key = key;
1718
this.all = new Map();
18-
this.free = [];
19+
this.free = new Map();
1920
this.handles = [];
2021
this.handle = null;
2122
this.server = net.createServer(assert.fail);
@@ -73,10 +74,7 @@ RoundRobinHandle.prototype.remove = function(worker) {
7374
if (!existed)
7475
return false;
7576

76-
const index = this.free.indexOf(worker);
77-
78-
if (index !== -1)
79-
this.free.splice(index, 1);
77+
this.free.delete(worker.id);
8078

8179
if (this.all.size !== 0)
8280
return false;
@@ -93,21 +91,24 @@ RoundRobinHandle.prototype.remove = function(worker) {
9391

9492
RoundRobinHandle.prototype.distribute = function(err, handle) {
9593
this.handles.push(handle);
96-
const worker = this.free.shift();
94+
const [ workerEntry ] = this.free;
9795

98-
if (worker)
96+
if (ArrayIsArray(workerEntry)) {
97+
const [ workerId, worker ] = workerEntry;
98+
this.free.delete(workerId);
9999
this.handoff(worker);
100+
}
100101
};
101102

102103
RoundRobinHandle.prototype.handoff = function(worker) {
103-
if (this.all.has(worker.id) === false) {
104+
if (!this.all.has(worker.id)) {
104105
return; // Worker is closing (or has closed) the server.
105106
}
106107

107108
const handle = this.handles.shift();
108109

109110
if (handle === undefined) {
110-
this.free.push(worker); // Add to ready queue again.
111+
this.free.set(worker.id, worker); // Add to ready queue again.
111112
return;
112113
}
113114

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)