@@ -23,6 +23,7 @@ const terminate = require('./utils/terminate');
23
23
24
24
const APP_ADDRESS = Symbol ( 'Master#appAddress' ) ;
25
25
const REALPORT = Symbol ( 'Master#realport' ) ;
26
+ const WORKER_ADDRESSES = Symbol ( 'Master#workerAddresses' ) ;
26
27
27
28
28
29
class Master extends EventEmitter {
@@ -38,6 +39,7 @@ class Master extends EventEmitter {
38
39
* - {Object} [https] https options, { key, cert }, full path
39
40
* - {Array|String} [require] will inject into worker/agent process
40
41
* - {String} [pidFile] will save master pid to this file
42
+ * - {boolean} [nginxSticky] - use nginx sticky mode, default false
41
43
*/
42
44
constructor ( options ) {
43
45
super ( ) ;
@@ -59,6 +61,7 @@ class Master extends EventEmitter {
59
61
if ( process . env . EGG_SERVER_ENV === 'local' || process . env . NODE_ENV === 'development' ) {
60
62
this . logMethod = 'debug' ;
61
63
}
64
+ this [ WORKER_ADDRESSES ] = [ ] ;
62
65
63
66
// get the real framework info
64
67
const frameworkPath = this . options . framework ;
@@ -83,9 +86,14 @@ class Master extends EventEmitter {
83
86
84
87
this . ready ( ( ) => {
85
88
this . isStarted = true ;
86
- const stickyMsg = this . options . sticky ? ' with STICKY MODE!' : '' ;
87
- this . logger . info ( '[master] %s started on %s (%sms)%s' ,
88
- frameworkPkg . name , this [ APP_ADDRESS ] , Date . now ( ) - startTime , stickyMsg ) ;
89
+ if ( this . options . nginxSticky ) {
90
+ this . logger . info ( '[master] %s started on %j (%sms) with NGINX STICKY MODE!' ,
91
+ frameworkPkg . name , this [ WORKER_ADDRESSES ] . map ( getAddress ) , Date . now ( ) - startTime ) ;
92
+ } else {
93
+ const stickyMsg = this . options . sticky ? ' with STICKY MODE!' : '' ;
94
+ this . logger . info ( '[master] %s started on %s (%sms)%s' ,
95
+ frameworkPkg . name , this [ APP_ADDRESS ] , Date . now ( ) - startTime , stickyMsg ) ;
96
+ }
89
97
90
98
const action = 'egg-ready' ;
91
99
this . messenger . send ( { action, to : 'parent' , data : { port : this [ REALPORT ] , address : this [ APP_ADDRESS ] } } ) ;
@@ -299,14 +307,6 @@ class Master extends EventEmitter {
299
307
from : 'app' ,
300
308
} ) ;
301
309
} ) ;
302
- cluster . on ( 'listening' , ( worker , address ) => {
303
- this . messenger . send ( {
304
- action : 'app-start' ,
305
- data : { workerPid : worker . process . pid , address } ,
306
- to : 'master' ,
307
- from : 'app' ,
308
- } ) ;
309
- } ) ;
310
310
}
311
311
312
312
/**
@@ -460,15 +460,12 @@ class Master extends EventEmitter {
460
460
const worker = this . workerManager . getWorker ( data . workerPid ) ;
461
461
const address = data . address ;
462
462
463
- // worker should listen stickyWorkerPort when sticky mode
464
- if ( this . options . sticky ) {
465
- if ( String ( address . port ) !== String ( this . options . stickyWorkerPort ) ) {
466
- return ;
467
- }
468
- // worker should listen REALPORT when not sticky mode
469
- } else if ( ! isUnixSock ( address )
470
- && ( String ( address . port ) !== String ( this [ REALPORT ] ) ) ) {
471
- return ;
463
+ address . protocal = this . options . https ? 'https' : 'http' ;
464
+ if ( this . options . nginxSticky ) {
465
+ this [ WORKER_ADDRESSES ] . push ( address ) ;
466
+ } else {
467
+ address . port = this . options . sticky ? this [ REALPORT ] : address . port ;
468
+ this [ APP_ADDRESS ] = getAddress ( address ) ;
472
469
}
473
470
474
471
// send message to agent with alive workers
@@ -506,10 +503,6 @@ class Master extends EventEmitter {
506
503
worker . disableRefork = false ;
507
504
}
508
505
509
- address . protocal = this . options . https ? 'https' : 'http' ;
510
- address . port = this . options . sticky ? this [ REALPORT ] : address . port ;
511
- this [ APP_ADDRESS ] = getAddress ( address ) ;
512
-
513
506
if ( this . options . sticky ) {
514
507
this . startMasterSocketServer ( err => {
515
508
if ( err ) return this . ready ( err ) ;
@@ -623,7 +616,3 @@ function getAddress({ addressType, address, port, protocal }) {
623
616
const hostname = address || '127.0.0.1' ;
624
617
return `${ protocal } ://${ hostname } :${ port } ` ;
625
618
}
626
-
627
- function isUnixSock ( address ) {
628
- return address . addressType === - 1 ;
629
- }
0 commit comments