• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5// Monkey-patch `net.Server.listen`
6const net = require('net');
7const cluster = require('cluster');
8
9// Force round-robin scheduling policy
10// as Windows defaults to SCHED_NONE
11// https://nodejs.org/docs/latest/api/cluster.html#clusterschedulingpolicy
12cluster.schedulingPolicy = cluster.SCHED_RR;
13
14// Ensures that the `backlog` is used to create a `net.Server`.
15const kExpectedBacklog = 127;
16if (cluster.isMaster) {
17  const listen = net.Server.prototype.listen;
18
19  net.Server.prototype.listen = common.mustCall(
20    function(...args) {
21      const options = args[0];
22      if (typeof options === 'object') {
23        assert(options.backlog, kExpectedBacklog);
24      } else {
25        assert(args[1], kExpectedBacklog);
26      }
27      return listen.call(this, ...args);
28    }
29  );
30
31  const worker = cluster.fork();
32  worker.on('message', () => {
33    worker.disconnect();
34  });
35} else {
36  const server = net.createServer();
37
38  server.listen({
39    host: common.localhostIPv4,
40    port: 0,
41    backlog: kExpectedBacklog,
42  }, common.mustCall(() => {
43    process.send(true);
44  }));
45}
46