-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cluster: support stdio option for workers
This commit allows setupMaster() to configure the stdio channels for worker processes. Refs: nodejs/node-v0.x-archive#5727 Refs: #7811 PR-URL: #7838 Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
Showing
3 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const cluster = require('cluster'); | ||
const net = require('net'); | ||
|
||
if (cluster.isMaster) { | ||
const buf = Buffer.from('foobar'); | ||
|
||
cluster.setupMaster({ | ||
stdio: ['pipe', 'pipe', 'pipe', 'ipc', 'pipe'] | ||
}); | ||
|
||
const worker = cluster.fork(); | ||
const channel = worker.process.stdio[4]; | ||
let response = ''; | ||
|
||
worker.on('exit', common.mustCall((code, signal) => { | ||
assert.strictEqual(code, 0); | ||
assert.strictEqual(signal, null); | ||
})); | ||
|
||
channel.setEncoding('utf8'); | ||
channel.on('data', (data) => { | ||
response += data; | ||
|
||
if (response === buf.toString()) { | ||
worker.disconnect(); | ||
} | ||
}); | ||
channel.write(buf); | ||
} else { | ||
const pipe = new net.Socket({ fd: 4 }); | ||
|
||
pipe.unref(); | ||
pipe.on('data', (data) => { | ||
assert.ok(data instanceof Buffer); | ||
pipe.write(data); | ||
}); | ||
} |