1
1
'use strict' ;
2
2
3
3
const {
4
- ArrayIsArray,
5
4
Boolean,
6
5
ObjectCreate,
7
6
SafeMap,
@@ -15,12 +14,12 @@ const { constants } = internalBinding('tcp_wrap');
15
14
16
15
module . exports = RoundRobinHandle ;
17
16
18
- function RoundRobinHandle ( key , address , { port, fd, flags, backlog } ) {
17
+ function RoundRobinHandle ( key , address , { port, fd, flags, backlog, scheduler } ) {
19
18
this . key = key ;
20
- this . all = new SafeMap ( ) ;
21
- this . free = new SafeMap ( ) ;
19
+ this . workers = new SafeMap ( ) ;
22
20
this . handles = init ( ObjectCreate ( null ) ) ;
23
21
this . handle = null ;
22
+ this . scheduler = scheduler ;
24
23
this . server = net . createServer ( assert . fail ) ;
25
24
26
25
if ( fd >= 0 )
@@ -45,8 +44,8 @@ function RoundRobinHandle(key, address, { port, fd, flags, backlog }) {
45
44
}
46
45
47
46
RoundRobinHandle . prototype . add = function ( worker , send ) {
48
- assert ( this . all . has ( worker . id ) === false ) ;
49
- this . all . set ( worker . id , worker ) ;
47
+ assert ( this . workers . has ( worker . id ) === false ) ;
48
+ this . workers . set ( worker . id , worker ) ;
50
49
51
50
const done = ( ) => {
52
51
if ( this . handle . getsockname ) {
@@ -58,7 +57,7 @@ RoundRobinHandle.prototype.add = function(worker, send) {
58
57
send ( null , null , null ) ; // UNIX socket.
59
58
}
60
59
61
- this . handoff ( worker ) ; // In case there are connections pending.
60
+ this . handoff ( ) ; // In case there are connections pending.
62
61
} ;
63
62
64
63
if ( this . server === null )
@@ -72,14 +71,12 @@ RoundRobinHandle.prototype.add = function(worker, send) {
72
71
} ;
73
72
74
73
RoundRobinHandle . prototype . remove = function ( worker ) {
75
- const existed = this . all . delete ( worker . id ) ;
74
+ const existed = this . workers . delete ( worker . id ) ;
76
75
77
76
if ( ! existed )
78
77
return false ;
79
78
80
- this . free . delete ( worker . id ) ;
81
-
82
- if ( this . all . size !== 0 )
79
+ if ( this . workers . size !== 0 )
83
80
return false ;
84
81
85
82
while ( ! isEmpty ( this . handles ) ) {
@@ -95,25 +92,37 @@ RoundRobinHandle.prototype.remove = function(worker) {
95
92
96
93
RoundRobinHandle . prototype . distribute = function ( err , handle ) {
97
94
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
-
101
- if ( ArrayIsArray ( workerEntry ) ) {
102
- const { 0 : workerId , 1 : worker } = workerEntry ;
103
- this . free . delete ( workerId ) ;
104
- this . handoff ( worker ) ;
105
- }
95
+ this . handoff ( ) ;
106
96
} ;
107
97
108
- RoundRobinHandle . prototype . handoff = function ( worker ) {
109
- if ( ! this . all . has ( worker . id ) ) {
110
- return ; // Worker is closing (or has closed) the server.
98
+ RoundRobinHandle . scheduler = function ( workers ) {
99
+ if ( workers . size > 0 ) {
100
+ const { 0 : workerId , 1 : worker } = workers . entries ( ) . next ( ) . value ;
101
+ workers . delete ( workerId ) ;
102
+ workers . set ( workerId , worker ) ;
103
+ return worker ;
111
104
}
105
+ } ;
112
106
107
+ RoundRobinHandle . prototype . handoff = function ( ) {
113
108
const handle = peek ( this . handles ) ;
114
109
115
110
if ( handle === null ) {
116
- this . free . set ( worker . id , worker ) ; // Add to ready queue again.
111
+ return ;
112
+ }
113
+
114
+ let socket ;
115
+ if ( this . scheduler . exposeSocket === true ) {
116
+ socket = new net . Socket ( {
117
+ handle,
118
+ readable : false ,
119
+ writable : false ,
120
+ pauseOnCreate : true
121
+ } ) ;
122
+ }
123
+
124
+ const worker = this . scheduler . execute ( this . workers , socket ) ;
125
+ if ( typeof worker === 'undefined' ) {
117
126
return ;
118
127
}
119
128
@@ -127,6 +136,6 @@ RoundRobinHandle.prototype.handoff = function(worker) {
127
136
else
128
137
this . distribute ( 0 , handle ) ; // Worker is shutting down. Send to another.
129
138
130
- this . handoff ( worker ) ;
139
+ this . handoff ( ) ;
131
140
} ) ;
132
141
} ;
0 commit comments