Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a4f0b13

Browse filesBrowse files
committed
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>
1 parent 0094adc commit a4f0b13
Copy full SHA for a4f0b13

File tree

Expand file treeCollapse file tree

3 files changed

+46
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+46
-0
lines changed
Open diff view settings
Collapse file

‎doc/api/cluster.md‎

Copy file name to clipboardExpand all lines: doc/api/cluster.md
+5Lines changed: 5 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,9 @@ values are `"rr"` and `"none"`.
626626
(Default=`process.argv.slice(2)`)
627627
* `silent` {Boolean} whether or not to send output to parent's stdio.
628628
(Default=`false`)
629+
* `stdio` {Array} Configures the stdio of forked processes. Because the
630+
cluster module relies on IPC to function, this configuration must contain an
631+
`'ipc'` entry. When this option is provided, it overrides `silent`.
629632
* `uid` {Number} Sets the user identity of the process. (See setuid(2).)
630633
* `gid` {Number} Sets the group identity of the process. (See setgid(2).)
631634

@@ -642,6 +645,8 @@ This object is not supposed to be changed or set manually, by you.
642645
(Default=`process.argv.slice(2)`)
643646
* `silent` {Boolean} whether or not to send output to parent's stdio.
644647
(Default=`false`)
648+
* `stdio` {Array} Configures the stdio of forked processes. When this option
649+
is provided, it overrides `silent`.
645650

646651
`setupMaster` is used to change the default 'fork' behavior. Once called,
647652
the settings will be present in `cluster.settings`.
Collapse file

‎lib/cluster.js‎

Copy file name to clipboardExpand all lines: lib/cluster.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ function masterInit() {
321321
env: workerEnv,
322322
silent: cluster.settings.silent,
323323
execArgv: execArgv,
324+
stdio: cluster.settings.stdio,
324325
gid: cluster.settings.gid,
325326
uid: cluster.settings.uid
326327
});
Collapse file
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const cluster = require('cluster');
5+
const net = require('net');
6+
7+
if (cluster.isMaster) {
8+
const buf = Buffer.from('foobar');
9+
10+
cluster.setupMaster({
11+
stdio: ['pipe', 'pipe', 'pipe', 'ipc', 'pipe']
12+
});
13+
14+
const worker = cluster.fork();
15+
const channel = worker.process.stdio[4];
16+
let response = '';
17+
18+
worker.on('exit', common.mustCall((code, signal) => {
19+
assert.strictEqual(code, 0);
20+
assert.strictEqual(signal, null);
21+
}));
22+
23+
channel.setEncoding('utf8');
24+
channel.on('data', (data) => {
25+
response += data;
26+
27+
if (response === buf.toString()) {
28+
worker.disconnect();
29+
}
30+
});
31+
channel.write(buf);
32+
} else {
33+
const pipe = new net.Socket({ fd: 4 });
34+
35+
pipe.unref();
36+
pipe.on('data', (data) => {
37+
assert.ok(data instanceof Buffer);
38+
pipe.write(data);
39+
});
40+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.