Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var cluster = require('cluster'),
https = require('https'),
fs = require('fs'),
Buffer = require('buffer').Buffer;
function start() {
var options = {
key: fs.readFileSync('./test/fixtures/test_key.pem'),
cert: fs.readFileSync('./test/fixtures/test_cert.pem')
};
var server = https.createServer(options, function (req, res) {
if (req.url === '/abc') process.exit();
res.end('hello');
});
server.listen(44300, function() {
console.log('listening!');
});
var callbacks = {};
process.on('message', function(msg) {
if (msg.type === 'response') {
(callbacks[msg.key] || []).forEach(function(callback) {
callback(msg.value);
});
}
});
server.on('resumeTlsSession', function(key, callback) {
var skey = key.toString('base64');
(callbacks[skey] = callbacks[skey] || []).push(function(session) {
if (!session) return callback(null, null);
callback(null, new Buffer(session, 'base64'));
});
process.send({
type: 'get',
key: key.toString('base64')
});
});
server.on('newTlsSession', function(key, session) {
process.send({
type: 'set',
key: key.toString('base64'),
value: session.toString('base64')
});
});
}
if (cluster.isMaster) {
var hash = {};
function fork() {
var instance = cluster.fork();
instance.on('message', function(msg) {
if (msg.type === 'set') {
hash[msg.key] = msg.value;
} else if (msg.type === 'get') {
instance.send({ type: 'response', key: msg.key, value: hash[msg.key] });
}
});
}
for (var i = 0; i < 4; i++) {
fork();
}
cluster.on('exit', fork);
} else {
start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.