当前位置:网站首页>Using file descriptor passing to implement your own cluster

Using file descriptor passing to implement your own cluster

2020-12-07 10:27:56 osc_ cyn8i2xg

parent.js

const childProcess = require('child_process');
const net = require('net');
const workers = [];
const workerNum = 10    ;
const handle = net._createServerHandle('127.0.0.1', 11111, 4);

for (let i = 0; i < workerNum; i++) {
    const worker = childProcess.fork('child.js', {env: {index: i}});
    workers.push(worker);
    worker.send(null ,handle);
}

child.js

const net = require('net');
process.on('message', (message, handle) => {
    net.createServer(() => {
        console.log(process.env.index, 'receive connection');
    }).listen({handle, port: 11111});
});

client.js

const net = require('net');
for (let i = 0; i < 50; i++) {
    net.connect({port: 11111});
}

test : perform parent.js, And then execute client.js.

github Warehouse

版权声明
本文为[osc_ cyn8i2xg]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201207102143230g.html