Skip to content

Commit e924dc7

Browse files
twchndanielleadams
authored andcommitted
cluster: use linkedlist for round_robin_handle
PR-URL: #40615 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 033a646 commit e924dc7

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

lib/internal/cluster/round_robin_handle.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
const {
44
ArrayIsArray,
5-
ArrayPrototypePush,
6-
ArrayPrototypeShift,
75
Boolean,
6+
ObjectCreate,
87
SafeMap,
98
} = primordials;
109

1110
const assert = require('internal/assert');
1211
const net = require('net');
1312
const { sendHelper } = require('internal/cluster/utils');
13+
const { append, init, isEmpty, peek, remove } = require('internal/linkedlist');
1414
const { constants } = internalBinding('tcp_wrap');
1515

1616
module.exports = RoundRobinHandle;
@@ -19,7 +19,7 @@ function RoundRobinHandle(key, address, { port, fd, flags }) {
1919
this.key = key;
2020
this.all = new SafeMap();
2121
this.free = new SafeMap();
22-
this.handles = [];
22+
this.handles = init(ObjectCreate(null));
2323
this.handle = null;
2424
this.server = net.createServer(assert.fail);
2525

@@ -81,18 +81,19 @@ RoundRobinHandle.prototype.remove = function(worker) {
8181
if (this.all.size !== 0)
8282
return false;
8383

84-
for (const handle of this.handles) {
84+
while (!isEmpty(this.handles)) {
85+
const handle = peek(this.handles);
8586
handle.close();
87+
remove(handle);
8688
}
87-
this.handles = [];
8889

8990
this.handle.close();
9091
this.handle = null;
9192
return true;
9293
};
9394

9495
RoundRobinHandle.prototype.distribute = function(err, handle) {
95-
ArrayPrototypePush(this.handles, handle);
96+
append(this.handles, handle);
9697
// eslint-disable-next-line node-core/no-array-destructuring
9798
const [ workerEntry ] = this.free; // this.free is a SafeMap
9899

@@ -108,13 +109,15 @@ RoundRobinHandle.prototype.handoff = function(worker) {
108109
return; // Worker is closing (or has closed) the server.
109110
}
110111

111-
const handle = ArrayPrototypeShift(this.handles);
112+
const handle = peek(this.handles);
112113

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

119+
remove(handle);
120+
118121
const message = { act: 'newconn', key: this.key };
119122

120123
sendHelper(worker.process, message, handle, (reply) => {

lib/internal/linkedlist.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
function init(list) {
44
list._idleNext = list;
55
list._idlePrev = list;
6+
return list;
67
}
78

89
// Show the most idle item.

0 commit comments

Comments
 (0)