@@ -20,10 +20,11 @@ if (cluster.isMaster) {
20
20
let reqCount = 0 ;
21
21
let lastSession = null ;
22
22
let shootOnce = false ;
23
+ let workerPort = null ;
23
24
24
25
function shoot ( ) {
25
- console . error ( '[master] connecting' ) ;
26
- const c = tls . connect ( common . PORT , {
26
+ console . error ( '[master] connecting' , workerPort ) ;
27
+ const c = tls . connect ( workerPort , {
27
28
session : lastSession ,
28
29
rejectUnauthorized : false
29
30
} , function ( ) {
@@ -42,11 +43,12 @@ if (cluster.isMaster) {
42
43
43
44
function fork ( ) {
44
45
const worker = cluster . fork ( ) ;
45
- worker . on ( 'message' , function ( msg ) {
46
+ worker . on ( 'message' , function ( { msg, port } ) {
46
47
console . error ( '[master] got %j' , msg ) ;
47
48
if ( msg === 'reused' ) {
48
49
++ reusedCount ;
49
50
} else if ( msg === 'listening' && ! shootOnce ) {
51
+ workerPort = port || workerPort ;
50
52
shootOnce = true ;
51
53
shoot ( ) ;
52
54
}
@@ -78,15 +80,19 @@ const options = {
78
80
79
81
const server = tls . createServer ( options , function ( c ) {
80
82
if ( c . isSessionReused ( ) ) {
81
- process . send ( 'reused' ) ;
83
+ process . send ( { msg : 'reused' } ) ;
82
84
} else {
83
- process . send ( 'not-reused' ) ;
85
+ process . send ( { msg : 'not-reused' } ) ;
84
86
}
85
87
c . end ( ) ;
86
88
} ) ;
87
89
88
- server . listen ( common . PORT , function ( ) {
89
- process . send ( 'listening' ) ;
90
+ server . listen ( 0 , function ( ) {
91
+ const { port } = server . address ( ) ;
92
+ process . send ( {
93
+ msg : 'listening' ,
94
+ port,
95
+ } ) ;
90
96
} ) ;
91
97
92
98
process . on ( 'message' , function listener ( msg ) {
0 commit comments