Skip to content

Commit 06b775c

Browse files
twchndanielleadams
authored andcommitted
cluster: use linkedlist for round_robin_handle
PR-URL: #40615 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f51ce33 commit 06b775c

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

‎lib/internal/cluster/round_robin_handle.js‎

Lines changed: 11 additions & 8 deletions
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
constassert=require('internal/assert');
1211
constnet=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=newSafeMap();
2121
this.free=newSafeMap();
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
returnfalse;
8383

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

8990
this.handle.close();
9091
this.handle=null;
9192
returntrue;
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-
consthandle=ArrayPrototypeShift(this.handles);
112+
consthandle=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
constmessage={act: 'newconn',key: this.key};
119122

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

‎lib/internal/linkedlist.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
functioninit(list){
44
list._idleNext=list;
55
list._idlePrev=list;
6+
returnlist;
67
}
78

89
// Show the most idle item.

0 commit comments

Comments
 (0)