@@ -15,12 +15,13 @@ const { constants } = internalBinding('tcp_wrap');
15
15
16
16
module . exports = RoundRobinHandle ;
17
17
18
- function RoundRobinHandle ( key , address , { port, fd, flags, backlog } ) {
18
+ function RoundRobinHandle ( key , address , { port, fd, flags, backlog, scheduler } ) {
19
19
this . key = key ;
20
20
this . all = new SafeMap ( ) ;
21
- this . free = new SafeMap ( ) ;
21
+ this . workers = new SafeMap ( ) ;
22
22
this . handles = init ( ObjectCreate ( null ) ) ;
23
23
this . handle = null ;
24
+ this . scheduler = scheduler ;
24
25
this . server = net . createServer ( assert . fail ) ;
25
26
26
27
if ( fd >= 0 )
@@ -47,6 +48,7 @@ function RoundRobinHandle(key, address, { port, fd, flags, backlog }) {
47
48
RoundRobinHandle . prototype . add = function ( worker , send ) {
48
49
assert ( this . all . has ( worker . id ) === false ) ;
49
50
this . all . set ( worker . id , worker ) ;
51
+ this . workers . set ( worker . id , worker ) ;
50
52
51
53
const done = ( ) => {
52
54
if ( this . handle . getsockname ) {
@@ -58,7 +60,7 @@ RoundRobinHandle.prototype.add = function(worker, send) {
58
60
send ( null , null , null ) ; // UNIX socket.
59
61
}
60
62
61
- this . handoff ( worker ) ; // In case there are connections pending.
63
+ this . handoff ( ) ; // In case there are connections pending.
62
64
} ;
63
65
64
66
if ( this . server === null )
@@ -77,7 +79,7 @@ RoundRobinHandle.prototype.remove = function(worker) {
77
79
if ( ! existed )
78
80
return false ;
79
81
80
- this . free . delete ( worker . id ) ;
82
+ this . workers . delete ( worker . id ) ;
81
83
82
84
if ( this . all . size !== 0 )
83
85
return false ;
@@ -95,28 +97,32 @@ RoundRobinHandle.prototype.remove = function(worker) {
95
97
96
98
RoundRobinHandle . prototype . distribute = function ( err , handle ) {
97
99
append ( this . handles , handle ) ;
98
- // eslint-disable-next-line node-core/no-array-destructuring
99
- const [ workerEntry ] = this . free ; // this.free is a SafeMap
100
+ this . handoff ( ) ;
101
+ } ;
102
+
103
+ RoundRobinHandle . scheduler = function ( workers ) {
104
+ const [ workerEntry ] = workers ;
100
105
101
106
if ( ArrayIsArray ( workerEntry ) ) {
102
107
const { 0 : workerId , 1 : worker } = workerEntry ;
103
- this . free . delete ( workerId ) ;
104
- this . handoff ( worker ) ;
108
+ workers . delete ( workerId ) ;
109
+ workers . set ( workerId , worker )
110
+ return worker ;
105
111
}
106
112
} ;
107
113
108
- RoundRobinHandle . prototype . handoff = function ( worker ) {
109
- if ( ! this . all . has ( worker . id ) ) {
110
- return ; // Worker is closing (or has closed) the server.
111
- }
112
-
114
+ RoundRobinHandle . prototype . handoff = function ( ) {
113
115
const handle = peek ( this . handles ) ;
114
116
115
117
if ( handle === null ) {
116
- this . free . set ( worker . id , worker ) ; // Add to ready queue again.
117
118
return ;
118
119
}
119
120
121
+ const worker = this . scheduler ( this . workers ) ;
122
+ if ( typeof worker === 'undefined' || ! this . all . has ( worker . id ) ) {
123
+ return ; // Worker is closing (or has closed) the server.
124
+ }
125
+
120
126
remove ( handle ) ;
121
127
122
128
const message = { act : 'newconn' , key : this . key } ;
@@ -127,6 +133,6 @@ RoundRobinHandle.prototype.handoff = function(worker) {
127
133
else
128
134
this . distribute ( 0 , handle ) ; // Worker is shutting down. Send to another.
129
135
130
- this . handoff ( worker ) ;
136
+ this . handoff ( ) ;
131
137
} ) ;
132
138
} ;
0 commit comments