22
33const {
44 ArrayIsArray,
5- ArrayPrototypePush,
6- ArrayPrototypeShift,
75 Boolean,
6+ ObjectCreate,
87 SafeMap,
98} = primordials ;
109
1110const assert = require ( 'internal/assert' ) ;
1211const net = require ( 'net' ) ;
1312const { sendHelper } = require ( 'internal/cluster/utils' ) ;
13+ const { append, init, isEmpty, peek, remove } = require ( 'internal/linkedlist' ) ;
1414const { constants } = internalBinding ( 'tcp_wrap' ) ;
1515
1616module . exports = RoundRobinHandle ;
@@ -19,7 +19,7 @@ function RoundRobinHandle(key, address, { port, fd, flags }) {
1919this . key = key ;
2020this . all = new SafeMap ( ) ;
2121this . free = new SafeMap ( ) ;
22- this . handles = [ ] ;
22+ this . handles = init ( ObjectCreate ( null ) ) ;
2323this . handle = null ;
2424this . server = net . createServer ( assert . fail ) ;
2525
@@ -81,18 +81,19 @@ RoundRobinHandle.prototype.remove = function(worker) {
8181if ( this . all . size !== 0 )
8282return false ;
8383
84- for ( const handle of this . handles ) {
84+ while ( ! isEmpty ( this . handles ) ) {
85+ const handle = peek ( this . handles ) ;
8586handle . close ( ) ;
87+ remove ( handle ) ;
8688}
87- this . handles = [ ] ;
8889
8990this . handle . close ( ) ;
9091this . handle = null ;
9192return true ;
9293} ;
9394
9495RoundRobinHandle . 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
9798const [ workerEntry ] = this . free ; // this.free is a SafeMap
9899
@@ -108,13 +109,15 @@ RoundRobinHandle.prototype.handoff = function(worker) {
108109return ; // 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 ) {
114115this . free . set ( worker . id , worker ) ; // Add to ready queue again.
115116return ;
116117}
117118
119+ remove ( handle ) ;
120+
118121const message = { act : 'newconn' , key : this . key } ;
119122
120123sendHelper ( worker . process , message , handle , ( reply ) => {
0 commit comments