-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cluster: wait on servers closing before disconnect
Before this, cluster behaves not the way it is documented. When disconnect is triggered, worker must wait for every server is closed before doing disconnect actually. Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> PR-URL: #1400 Fixes: #1305
- Loading branch information
1 parent
d9ddd7d
commit 9c0a1b8
Showing
2 changed files
with
58 additions
and
2 deletions.
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,42 @@ | ||
'use strict'; | ||
|
||
var common = require('../common'); | ||
var assert = require('assert'); | ||
var cluster = require('cluster'); | ||
var net = require('net'); | ||
|
||
if (cluster.isWorker) { | ||
net.createServer(function(socket) { | ||
// Wait for any data, then close connection | ||
socket.on('data', socket.end.bind(socket)); | ||
}).listen(common.PORT, common.localhostIPv4); | ||
} else if (cluster.isMaster) { | ||
|
||
var connectionDone; | ||
var ok; | ||
|
||
// start worker | ||
var worker = cluster.fork(); | ||
|
||
// Disconnect worker when it is ready | ||
worker.once('listening', function() { | ||
net.createConnection(common.PORT, common.localhostIPv4, function() { | ||
var socket = this; | ||
worker.disconnect(); | ||
setTimeout(function() { | ||
socket.write('.'); | ||
connectionDone = true; | ||
}, 1000); | ||
}); | ||
}); | ||
|
||
// Check worker events and properties | ||
worker.once('disconnect', function() { | ||
assert.ok(connectionDone, 'disconnect should occur after socket close'); | ||
ok = true; | ||
}); | ||
|
||
process.once('exit', function() { | ||
assert.ok(ok); | ||
}); | ||
} |