-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cluster: send connection to other server when worker drop it
- Loading branch information
Showing
2 changed files
with
67 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const net = require('net'); | ||
const cluster = require('cluster'); | ||
const tmpdir = require('../common/tmpdir'); | ||
|
||
let connectionCount = 0; | ||
let listenCount = 0; | ||
let worker1; | ||
let worker2; | ||
|
||
function request(path) { | ||
for (let i = 0; i < 10; i++) { | ||
net.connect(path); | ||
} | ||
} | ||
|
||
function handleMessage(message) { | ||
assert.match(message.action, /listen|connection/); | ||
if (message.action === 'listen') { | ||
if (++listenCount === 2) { | ||
request(common.PIPE); | ||
} | ||
} else if (message.action === 'connection') { | ||
if (++connectionCount === 10) { | ||
worker1.send({ action: 'disconnect' }); | ||
worker2.send({ action: 'disconnect' }); | ||
} | ||
} | ||
} | ||
|
||
if (cluster.isPrimary) { | ||
cluster.schedulingPolicy = cluster.SCHED_RR; | ||
tmpdir.refresh(); | ||
worker1 = cluster.fork({ maxConnections: 1, pipePath: common.PIPE }); | ||
worker2 = cluster.fork({ maxConnections: 9, pipePath: common.PIPE }); | ||
worker1.on('message', common.mustCall((message) => { | ||
handleMessage(message); | ||
}, 2)); | ||
worker2.on('message', common.mustCall((message) => { | ||
handleMessage(message); | ||
}, 10)); | ||
} else { | ||
const server = net.createServer(common.mustCall((socket) => { | ||
process.send({ action: 'connection' }); | ||
}, +process.env.maxConnections)); | ||
|
||
server.listen(process.env.pipePath, common.mustCall(() => { | ||
process.send({ action: 'listen' }); | ||
})); | ||
|
||
server.maxConnections = +process.env.maxConnections; | ||
|
||
process.on('message', common.mustCall((message) => { | ||
assert.strictEqual(message.action, 'disconnect'); | ||
process.disconnect(); | ||
})); | ||
} |