Skip to content

Commit 9027b55

Browse files
committed
cluster: add schedulingPolicy to settings
Added `schedulingPolicy` option to settings so that the cluster owners can schedule the handler explicitly as well. They can use the name either `rr` or `none` for setting up the schedulingPolicy for setting up the primary process.
1 parent 2818fa6 commit 9027b55

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

doc/api/cluster.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,8 @@ changes:
947947
primary's `process.debugPort`.
948948
* `windowsHide` {boolean} Hide the forked processes console window that would
949949
normally be created on Windows systems. **Default:** `false`.
950+
* `schedulingPolicy` {string} policy to distribute load among the
951+
secondary processes.
950952

951953
After calling [`.setupPrimary()`][] (or [`.fork()`][]) this settings object will
952954
contain the settings, including the default values.

lib/internal/cluster/primary.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,37 @@ cluster.SCHED_RR = SCHED_RR; // Primary distributes connections.
4242
let ids = 0;
4343
let debugPortOffset = 1;
4444
let initialized = false;
45-
46-
// XXX(bnoordhuis) Fold cluster.schedulingPolicy into cluster.settings?
47-
let schedulingPolicy = process.env.NODE_CLUSTER_SCHED_POLICY;
48-
if (schedulingPolicy === 'rr')
49-
schedulingPolicy = SCHED_RR;
50-
else if (schedulingPolicy === 'none')
51-
schedulingPolicy = SCHED_NONE;
52-
else if (process.platform === 'win32') {
53-
// Round-robin doesn't perform well on
54-
// Windows due to the way IOCP is wired up.
55-
schedulingPolicy = SCHED_NONE;
56-
} else
57-
schedulingPolicy = SCHED_RR;
58-
59-
cluster.schedulingPolicy = schedulingPolicy;
45+
let schedulingPolicy = SCHED_RR;
46+
47+
const SCHEDULE_POLICY_NAME_MAP = new SafeMap()
48+
.set('rr', SCHED_RR)
49+
.set('none', SCHED_NONE);
50+
51+
function processSchedulingPolicy(settings = {}) {
52+
const schedulingPolicy = process.env.NODE_CLUSTER_SCHED_POLICY || settings.schedulingPolicy;
53+
if (schedulingPolicy === 'rr' || schedulingPolicy === 'none')
54+
return schedulingPolicy;
55+
if (process.platform === 'win32') {
56+
// Round-robin doesn't perform well on
57+
// Windows due to the way IOCP is wired up.
58+
return 'none';
59+
}
60+
return 'rr';
61+
}
6062

6163
cluster.setupPrimary = function(options) {
6264
const settings = {
6365
args: ArrayPrototypeSlice(process.argv, 2),
6466
exec: process.argv[1],
6567
execArgv: process.execArgv,
6668
silent: false,
69+
schedulingPolicy: cluster.schedulingPolicy || processSchedulingPolicy(options),
6770
...cluster.settings,
6871
...options
6972
};
7073

74+
assert(settings.schedulingPolicy);
75+
7176
// Tell V8 to write profile data for each process to a separate file.
7277
// Without --logfile=v8-%p.log, everything ends up in a single, unusable
7378
// file. (Unusable because what V8 logs are memory addresses and each
@@ -85,9 +90,7 @@ cluster.setupPrimary = function(options) {
8590
return process.nextTick(setupSettingsNT, settings);
8691

8792
initialized = true;
88-
schedulingPolicy = cluster.schedulingPolicy; // Freeze policy.
89-
assert(schedulingPolicy === SCHED_NONE || schedulingPolicy === SCHED_RR,
90-
`Bad cluster.schedulingPolicy: ${schedulingPolicy}`);
93+
schedulingPolicy = SCHEDULE_POLICY_NAME_MAP.get(cluster.schedulingPolicy); // Freeze policy.
9194

9295
process.nextTick(setupSettingsNT, settings);
9396

test/parallel/test-cluster-setup-primary-cumulative.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ assert.deepStrictEqual(cluster.settings, {
3434
args: process.argv.slice(2),
3535
exec: process.argv[1],
3636
execArgv: process.execArgv,
37+
schedulingPolicy: 'rr',
3738
silent: false,
3839
});
3940
console.log('ok sets defaults');
@@ -57,6 +58,7 @@ assert.deepStrictEqual(cluster.settings, {
5758
args: ['foo', 'bar'],
5859
exec: 'overridden',
5960
execArgv: ['baz', 'bang'],
61+
schedulingPolicy: 'rr',
6062
silent: false,
6163
});
6264
console.log('ok preserves current settings');

0 commit comments

Comments
 (0)