@@ -59,7 +59,7 @@ cluster.isWorker = 'NODE_WORKER_ID' in process.env;
5959cluster . isMaster = ! cluster . isWorker ;
6060
6161//The worker object is only used in a worker
62- cluster . worker = cluster . isWorker ? { } : null ;
62+ cluster . worker = cluster . isWorker ? new EventEmitter ( ) : null ;
6363//The workers array is oly used in the naster
6464cluster . workers = cluster . isMaster ? [ ] : null ;
6565
@@ -134,7 +134,7 @@ function handleWorkerMessage(message, worker) {
134134
135135 //echo callback id, if one was requested
136136 if ( message . _queryId ) {
137- worker . send ( { _queryId : message . _queryId } ) ;
137+ worker . send ( { _internal : true , _queryId : message . _queryId } ) ;
138138 }
139139 }
140140
@@ -155,7 +155,7 @@ function handleWorkerMessage(message, worker) {
155155 }
156156
157157 //echo callback id, with the fd handler associated with it
158- var response = { _queryId : message . _queryId } ;
158+ var response = { _internal : true , _queryId : message . _queryId } ;
159159 worker . send ( response , servers [ key ] ) ;
160160 }
161161
@@ -172,14 +172,14 @@ function handleWorkerMessage(message, worker) {
172172
173173 //echo callback id, if one was requested
174174 if ( message . _queryId ) {
175- worker . send ( { _queryId : message . _queryId } ) ;
175+ worker . send ( { _internal : true , _queryId : message . _queryId } ) ;
176176 }
177177 }
178178
179179 //echo callback id, if one was requested
180180 else {
181181 if ( message . _queryId ) {
182- worker . send ( { _queryId : message . _queryId } ) ;
182+ worker . send ( { _internal : true , _queryId : message . _queryId } ) ;
183183 }
184184 }
185185}
@@ -304,7 +304,7 @@ cluster._setupWorker = function() {
304304 } ) ;
305305
306306 //Tell master that the worker is online
307- cluster . worker . send ( {
307+ cluster . worker . respond ( {
308308 cmd : 'online' ,
309309 _internal : true
310310 } ) ;
@@ -313,19 +313,23 @@ cluster._setupWorker = function() {
313313 process . on ( 'message' , function ( msg , handle ) {
314314 debug ( "recv " + JSON . stringify ( msg ) ) ;
315315
316- if ( msg . _queryId && msg . _queryId in queryCallbacks ) {
317- var cb = queryCallbacks [ msg . _queryId ] ;
318- if ( typeof cb == 'function' ) {
319- cb ( msg , handle ) ;
316+ if ( msg . _internal && msg . _internal === true ) {
317+ if ( msg . _queryId && msg . _queryId in queryCallbacks ) {
318+ var cb = queryCallbacks [ msg . _queryId ] ;
319+ if ( typeof cb == 'function' ) {
320+ cb ( msg , handle ) ;
321+ }
322+ delete queryCallbacks [ msg . _queryId ] ;
320323 }
321- delete queryCallbacks [ msg . _queryId ] ;
324+ } else {
325+ cluster . worker . emit ( "message" , msg ) ;
322326 }
323327 } ) ;
324328} ;
325329
326- //Send message to the master, and run callback when the master echo
330+ // Send message to the master, and run callback when the master echo
327331if ( cluster . isWorker ) {
328- cluster . worker . send = function ( msg , cb ) {
332+ cluster . worker . respond = function ( msg , cb ) {
329333 // This can only be called from a worker.
330334 assert ( cluster . isWorker ) ;
331335
@@ -343,15 +347,14 @@ if (cluster.isWorker) {
343347 } ;
344348}
345349
346-
347350// Internal function. Called by lib/net.js when attempting to bind a server.
348351cluster . _getServer = function ( tcpSelf , address , port , addressType , cb ) {
349352 // This can only be called from a worker.
350353 assert ( cluster . isWorker ) ;
351354
352355 //Send a listening message to the master
353356 tcpSelf . once ( 'listening' , function ( ) {
354- cluster . worker . send ( {
357+ cluster . worker . respond ( {
355358 cmd : "listening" ,
356359 _internal : true ,
357360 address : address ,
@@ -369,7 +372,7 @@ cluster._getServer = function(tcpSelf, address, port, addressType, cb) {
369372 addressType : addressType
370373 } ;
371374 //The callback will be stored until the master has responed
372- cluster . worker . send ( message , function ( msg , handle ) {
375+ cluster . worker . respond ( message , function ( msg , handle ) {
373376 cb ( handle ) ;
374377 } ) ;
375378
0 commit comments