@@ -24,6 +24,7 @@ const terminate = require('./utils/terminate');
24
24
const PROTOCOL = Symbol ( 'Master#protocol' ) ;
25
25
const REAL_PORT = Symbol ( 'Master#real_port' ) ;
26
26
const APP_ADDRESS = Symbol ( 'Master#appAddress' ) ;
27
+ const WORKER_ADDRESSES = Symbol ( 'Master#workerAddresses' ) ;
27
28
28
29
class Master extends EventEmitter {
29
30
@@ -38,6 +39,7 @@ class Master extends EventEmitter {
38
39
* - {Object} [https] https options, { key, cert, ca }, 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 ( ) ;
@@ -60,6 +62,7 @@ class Master extends EventEmitter {
60
62
if ( process . env . EGG_SERVER_ENV === 'local' || process . env . NODE_ENV === 'development' ) {
61
63
this . logMethod = 'debug' ;
62
64
}
65
+ this [ WORKER_ADDRESSES ] = [ ] ;
63
66
64
67
// get the real framework info
65
68
const frameworkPath = this . options . framework ;
@@ -84,9 +87,14 @@ class Master extends EventEmitter {
84
87
85
88
this . ready ( ( ) => {
86
89
this . isStarted = true ;
87
- const stickyMsg = this . options . sticky ? ' with STICKY MODE!' : '' ;
88
- this . logger . info ( '[master] %s started on %s (%sms)%s' ,
89
- frameworkPkg . name , this [ APP_ADDRESS ] , Date . now ( ) - startTime , stickyMsg ) ;
90
+ if ( this . options . nginxSticky ) {
91
+ this . logger . info ( '[master] %s started on %j (%sms) with NGINX STICKY MODE!' ,
92
+ frameworkPkg . name , this [ WORKER_ADDRESSES ] . map ( getAddress ) , Date . now ( ) - startTime ) ;
93
+ } else {
94
+ const stickyMsg = this . options . sticky ? ' with STICKY MODE!' : '' ;
95
+ this . logger . info ( '[master] %s started on %s (%sms)%s' ,
96
+ frameworkPkg . name , this [ APP_ADDRESS ] , Date . now ( ) - startTime , stickyMsg ) ;
97
+ }
90
98
91
99
const action = 'egg-ready' ;
92
100
this . messenger . send ( {
@@ -361,17 +369,6 @@ class Master extends EventEmitter {
361
369
from : 'app' ,
362
370
} ) ;
363
371
} ) ;
364
- cluster . on ( 'listening' , ( worker , address ) => {
365
- this . messenger . send ( {
366
- action : 'app-start' ,
367
- data : {
368
- workerPid : worker . process . pid ,
369
- address,
370
- } ,
371
- to : 'master' ,
372
- from : 'app' ,
373
- } ) ;
374
- } ) ;
375
372
}
376
373
377
374
/**
@@ -547,16 +544,12 @@ class Master extends EventEmitter {
547
544
onAppStart ( data ) {
548
545
const worker = this . workerManager . getWorker ( data . workerPid ) ;
549
546
const address = data . address ;
550
-
551
- // worker should listen stickyWorkerPort when sticky mode
552
- if ( this . options . sticky ) {
553
- if ( String ( address . port ) !== String ( this . options . stickyWorkerPort ) ) {
554
- return ;
555
- }
556
- // worker should listen REALPORT when not sticky mode
557
- } else if ( ! isUnixSock ( address )
558
- && ( String ( address . port ) !== String ( this [ REAL_PORT ] ) ) ) {
559
- return ;
547
+ address . protocol = this [ PROTOCOL ] ;
548
+ if ( this . options . nginxSticky ) {
549
+ this [ WORKER_ADDRESSES ] . push ( address ) ;
550
+ } else {
551
+ address . port = this . options . sticky ? this [ REAL_PORT ] : address . port ;
552
+ this [ APP_ADDRESS ] = getAddress ( address ) ;
560
553
}
561
554
562
555
// send message to agent with alive workers
@@ -598,10 +591,6 @@ class Master extends EventEmitter {
598
591
worker . disableRefork = false ;
599
592
}
600
593
601
- address . protocol = this [ PROTOCOL ] ;
602
- address . port = this . options . sticky ? this [ REAL_PORT ] : address . port ;
603
- this [ APP_ADDRESS ] = getAddress ( address ) ;
604
-
605
594
if ( this . options . sticky ) {
606
595
this . startMasterSocketServer ( err => {
607
596
if ( err ) return this . ready ( err ) ;
@@ -720,7 +709,3 @@ function getAddress({
720
709
const hostname = address || '127.0.0.1' ;
721
710
return `${ protocol } ://${ hostname } :${ port } ` ;
722
711
}
723
-
724
- function isUnixSock ( address ) {
725
- return address . addressType === - 1 ;
726
- }
0 commit comments