Merge pull request #288 from zoff-music/test/cluster-mode

Test/cluster mode
This commit is contained in:
Kasper Rynning-Tønnesen
2018-02-05 14:12:33 +01:00
committed by GitHub
4 changed files with 218 additions and 47 deletions

61
package-lock.json generated
View File

@@ -442,6 +442,11 @@
"version": "https://registry.npmjs.org/dom-walk/-/dom-walk-0.1.1.tgz",
"integrity": "sha1-ZyIm3HTI95mtNTB9+TaroRrNYBg="
},
"double-ended-queue": {
"version": "2.1.0-0",
"resolved": "https://registry.npmjs.org/double-ended-queue/-/double-ended-queue-2.1.0-0.tgz",
"integrity": "sha1-ED01J/0xUo9AGIEwyEHv3XgmTlw="
},
"duplexer2": {
"version": "https://registry.npmjs.org/duplexer2/-/duplexer2-0.0.2.tgz",
"integrity": "sha1-xhTc9n4vsUmVqRcR5aYX6KYKMds=",
@@ -1207,6 +1212,11 @@
"version": "https://registry.npmjs.org/interpret/-/interpret-1.0.4.tgz",
"integrity": "sha1-ggzdWIuGj/sZGoCVBtbJyPISsbA="
},
"ip": {
"version": "1.1.5",
"resolved": "https://registry.npmjs.org/ip/-/ip-1.1.5.tgz",
"integrity": "sha1-vd7XARQpCCjAoDnnLvJfWq7ENUo="
},
"ip-regex": {
"version": "https://registry.npmjs.org/ip-regex/-/ip-regex-1.0.3.tgz",
"integrity": "sha1-3FiQdvZZ9BnCIgOaMzFvHHOH7/0="
@@ -1760,6 +1770,11 @@
"remove-trailing-separator": "https://registry.npmjs.org/remove-trailing-separator/-/remove-trailing-separator-1.1.0.tgz"
}
},
"notepack.io": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/notepack.io/-/notepack.io-2.1.2.tgz",
"integrity": "sha1-lKR/XkzkrOHz1xsnxvg8cWM5PFI="
},
"oauth-sign": {
"version": "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.8.2.tgz",
"integrity": "sha1-Rqarfwrq2N6unsBWV4C31O/rnUM="
@@ -2088,6 +2103,26 @@
"resolve": "https://registry.npmjs.org/resolve/-/resolve-1.4.0.tgz"
}
},
"redis": {
"version": "2.8.0",
"resolved": "https://registry.npmjs.org/redis/-/redis-2.8.0.tgz",
"integrity": "sha512-M1OkonEQwtRmZv4tEWF2VgpG0JWJ8Fv1PhlgT5+B+uNq2cA3Rt1Yt/ryoR+vQNOQcIEgdCdfH0jr3bDpihAw1A==",
"requires": {
"double-ended-queue": "2.1.0-0",
"redis-commands": "1.3.1",
"redis-parser": "2.6.0"
}
},
"redis-commands": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.3.1.tgz",
"integrity": "sha1-gdgm9F+pyLIBH0zXoP5ZfSQdRCs="
},
"redis-parser": {
"version": "2.6.0",
"resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-2.6.0.tgz",
"integrity": "sha1-Uu0J2srBCPGmMcB+m2mUHnoZUEs="
},
"regex-cache": {
"version": "https://registry.npmjs.org/regex-cache/-/regex-cache-0.4.4.tgz",
"integrity": "sha1-db3FiioUls7EihKDW8VMjVYjNt0=",
@@ -2300,6 +2335,18 @@
}
}
},
"socket.io-redis": {
"version": "5.2.0",
"resolved": "https://registry.npmjs.org/socket.io-redis/-/socket.io-redis-5.2.0.tgz",
"integrity": "sha1-j+KtlEX8UIhvtwq8dZ1nQD1Ymd8=",
"requires": {
"debug": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"notepack.io": "2.1.2",
"redis": "2.8.0",
"socket.io-adapter": "https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-1.1.1.tgz",
"uid2": "0.0.3"
}
},
"source-map": {
"version": "https://registry.npmjs.org/source-map/-/source-map-0.5.7.tgz",
"integrity": "sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w="
@@ -2326,6 +2373,15 @@
"version": "https://registry.npmjs.org/statuses/-/statuses-1.3.1.tgz",
"integrity": "sha1-+vUbnrdKrvOzrPStX2Gr8ky3uT4="
},
"sticky-session": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/sticky-session/-/sticky-session-1.1.2.tgz",
"integrity": "sha1-cWznOEUrvp7t7FBjEYlaPWSAPeE=",
"requires": {
"debug": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"ip": "1.1.5"
}
},
"stream-consume": {
"version": "https://registry.npmjs.org/stream-consume/-/stream-consume-0.1.0.tgz",
"integrity": "sha1-pB6tGm1ggc63n2WwYZAbbY89HQ8="
@@ -2478,6 +2534,11 @@
"integrity": "sha1-bgkk1r2mta/jSeOabWMoUKD4grc=",
"optional": true
},
"uid2": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/uid2/-/uid2-0.0.3.tgz",
"integrity": "sha1-SDEm4Rd03y9xuLY53NeZw3YWK4I="
},
"ultron": {
"version": "https://registry.npmjs.org/ultron/-/ultron-1.1.0.tgz",
"integrity": "sha1-sHoualQagV/Go0zNRTO67DB8qGQ="

View File

@@ -48,6 +48,8 @@
"nodemailer": "^4.0.1",
"request": "^2.72.0",
"socket.io": "^2.0.3",
"socket.io-redis": "^5.2.0",
"sticky-session": "^1.1.2",
"uniqid": "^4.1.1"
}
}

147
server/app.js Normal file
View File

@@ -0,0 +1,147 @@
var express = require('express'),
cluster = require('cluster'),
net = require('net'),
socketio = require('socket.io'),
socket_redis = require('socket.io-redis');
var path = require('path');
var publicPath = path.join(__dirname, 'public');
var http = require('http');
var port = 8080,
num_processes = require('os').cpus().length;
if (cluster.isMaster) {
// This stores our workers. We need to keep them to be able to reference
// them based on source IP address. It's also useful for auto-restart,
// for example.
var workers = [];
// Helper function for spawning worker at index 'i'.
var spawn = function(i) {
workers[i] = cluster.fork();
// Optional: Restart worker on exit
workers[i].on('exit', function(code, signal) {
console.log('respawning worker', i);
spawn(i);
});
};
// Spawn workers.
for (var i = 0; i < num_processes; i++) {
spawn(i);
}
// Helper function for getting a worker index based on IP address.
// This is a hot path so it should be really fast. The way it works
// is by converting the IP address to a number by removing non numeric
// characters, then compressing it to the number of slots we have.
//
// Compared against "real" hashing (from the sticky-session code) and
// "real" IP number conversion, this function is on par in terms of
// worker index distribution only much faster.
var worker_index = function(ip, len) {
var s = '';
for (var i = 0, _len = ip.length; i < _len; i++) {
if (!isNaN(ip[i])) {
s += ip[i];
}
}
return Number(s) % len;
};
// Create the outside facing server listening on our port.
var server = net.createServer({ pauseOnConnect: true }, function(connection) {
// We received a connection and need to pass it to the appropriate
// worker. Get the worker for this connection's source IP and pass
// it the connection.
var worker = workers[worker_index(connection.remoteAddress, num_processes)];
worker.send('sticky-session:connection', connection);
}).listen(port);
} else {
// Note we don't use a port here because the master listens on it for us.
var app = require('./index.js');
// Here you might use middleware, attach routes, etc
// Don't expose our internal server to the outside.
try {
var cert_config = require(path.join(path.join(__dirname, 'config'), 'cert_config.js'));
var fs = require('fs');
var privateKey = fs.readFileSync(cert_config.privateKey).toString();
var certificate = fs.readFileSync(cert_config.certificate).toString();
var ca = fs.readFileSync(cert_config.ca).toString();
var credentials = {
key: privateKey,
cert: certificate,
ca: ca
};
var https = require('https');
server = https.Server(credentials, app);
var cors_proxy = require('cors-anywhere');
cors_proxy.createServer({
requireHeader: ['origin', 'x-requested-with'],
removeHeaders: ['cookie', 'cookie2'],
httpsOptions: credentials
}).listen(8081, function() {
console.log('Running CORS Anywhere on :' + 8081);
});
} catch(err){
console.log("Starting without https (probably on localhost)");
var cors_proxy = require('cors-anywhere');
cors_proxy.createServer({
requireHeader: ['origin', 'x-requested-with'],
removeHeaders: ['cookie', 'cookie2'],
}).listen(8081, function() {
console.log('Running CORS Anywhere on :' + 8081);
});
var http = require('http');
server = http.Server(app);
//add = ",http://localhost:80*,http://localhost:8080*,localhost:8080*, localhost:8082*,http://zoff.dev:80*,http://zoff.dev:8080*,zoff.dev:8080*, zoff.dev:8082*";
}
server.listen();
var socketIO = app.socketIO;
var redis = require('socket.io-redis');
socketIO.adapter(redis({ host: 'localhost', port: 6379 }));
//socketIO.set( 'origins', '*localhost:8080' );
socketIO.listen(server);
// Tell Socket.IO to use the redis adapter. By default, the redis
// server is assumed to be on localhost:6379. You don't have to
// specify them explicitly unless you want to change them.
//io.adapter(socket_redis({ host: 'localhost', port: 6379 }));
// Here you might use Socket.IO middleware for authorization etc.
/*io.on('connection', function(socket) {
console.log('New client connection detected on process ' + process.pid);
socket.emit('welcome', {message: 'Welcome to BlueFrog Chat Room'});
socket.on('new.message', function(message) {
socket.emit('new.message', message);
})
});*/
// Listen to messages sent from the master. Ignore everything else.
process.on('message', function(message, connection) {
if (message !== 'sticky-session:connection') {
return;
}
// Emulate a connection event on the server by emitting the
// event with the connection the master sent us.
server.emit('connection', connection);
connection.resume();
});
}

View File

@@ -1,6 +1,5 @@
VERSION = require('./VERSION.js');
var server;
var add = "";
var path = require('path');
var publicPath = path.join(__dirname, 'public');
@@ -22,45 +21,6 @@ app.set('view engine', 'handlebars');
app.enable('view cache');
app.set('views', publicPath);
try{
var cert_config = require(path.join(path.join(__dirname, 'config'), 'cert_config.js'));
var fs = require('fs');
var privateKey = fs.readFileSync(cert_config.privateKey).toString();
var certificate = fs.readFileSync(cert_config.certificate).toString();
var ca = fs.readFileSync(cert_config.ca).toString();
var credentials = {
key: privateKey,
cert: certificate,
ca: ca
};
var https = require('https');
server = https.Server(credentials, app);
var cors_proxy = require('cors-anywhere');
cors_proxy.createServer({
requireHeader: ['origin', 'x-requested-with'],
removeHeaders: ['cookie', 'cookie2'],
httpsOptions: credentials
}).listen(8081, function() {
console.log('Running CORS Anywhere on :' + 8081);
});
}
catch(err){
console.log("Starting without https (probably on localhost)");
var cors_proxy = require('cors-anywhere');
cors_proxy.createServer({
requireHeader: ['origin', 'x-requested-with'],
removeHeaders: ['cookie', 'cookie2'],
}).listen(8081, function() {
console.log('Running CORS Anywhere on :' + 8081);
});
var http = require('http');
server = http.Server(app);
add = ",http://localhost:80*,http://localhost:8080*,localhost:8080*, localhost:8082*,http://zoff.dev:80*,http://zoff.dev:8080*,zoff.dev:8080*, zoff.dev:8082*";
}
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser')
app.use( bodyParser.json() ); // to support JSON-encoded bodies
@@ -70,13 +30,17 @@ app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
app.use(cookieParser());
/* Starting DB and socketio */
io = require('socket.io')(server, {
io = require('socket.io')({
pingTimeout: 25000,
}); //, "origins": ("https://zoff.me:443*,https://zoff.me:8080*,zoff.me:8080*,https://remote.zoff.me:443*,https://remote.zoff.me:8080*,https://fb.zoff.me:443*,https://fb.zoff.me:8080*,https://admin.zoff.me:443*,https://admin.zoff.me:8080*" + add)});
//path: '/zoff',
//"origins": ("https://zoff.me:443*,https://zoff.me:8080*,zoff.me:8080*,https://remote.zoff.me:443*,https://remote.zoff.me:8080*,https://fb.zoff.me:443*,https://fb.zoff.me:8080*,https://admin.zoff.me:443*,https://admin.zoff.me:8080*, http://localhost:8080*")});
});
db = require('./handlers/db.js');
var socketIO = require('./handlers/io.js');
socketIO();
app.socketIO = io;
request = require('request');
/* Globally needed "libraries" and files */
@@ -99,11 +63,6 @@ filter = new Filter({ placeHolder: 'x'});
var router = require('./routing/router.js');
var api = require('./routing/api.js');
var ico_router = require('./routing/icons_routing.js');
var port = 8080;
server.listen(port, function () {
console.log('Server listening at port %d', port);
});
app.get('/robots.txt', function (req, res) {
res.type('text/plain');
@@ -147,3 +106,5 @@ db.collection("user_names").update({"_id": "all_names"}, {$set: {names: []}}, {m
db.collection("connected_users").update({users: {$exists: true}}, {$set: {users: []}}, {multi: true, upsert: true}, function(err, docs){});
db.collection("connected_users").update({"_id": "total_users"}, {$set: {total_users: []}}, {multi: true, upsert: true}, function(err, docs) {});
db.collection("frontpage_lists").update({viewers: {$ne: 0}}, {$set: {"viewers": 0}}, {multi: true, upsert: true}, function(err, docs) {});
module.exports = app;