Improve streaming server with cluster (#1970)

This commit is contained in:
geta6 2017-04-17 11:32:30 +09:00 committed by Eugen
parent 7a5086729a
commit 64e1d51025
2 changed files with 272 additions and 248 deletions

View file

@ -81,3 +81,7 @@ SMTP_FROM_ADDRESS=notifications@example.com
# Advanced settings
# If you need to use pgBouncer, you need to disable prepared statements:
# PREPARED_STATEMENTS=false
# Cluster number setting for streaming API server.
# If you comment out following line, cluster number will be `numOfCpuCores - 1`.
STREAMING_CLUSTER_NUM=1

View file

@ -1,3 +1,5 @@
import os from 'os';
import cluster from 'cluster';
import dotenv from 'dotenv'
import express from 'express'
import http from 'http'
@ -14,6 +16,23 @@ dotenv.config({
path: env === 'production' ? '.env.production' : '.env'
})
if (cluster.isMaster) {
// cluster master
const core = +process.env.STREAMING_CLUSTER_NUM || (env === 'development' ? 1 : os.cpus().length - 1)
const fork = () => {
const worker = cluster.fork();
worker.on('exit', (code, signal) => {
log.error(`Worker died with exit code ${code}, signal ${signal} received.`);
setTimeout(() => fork(), 0);
});
};
for (let i = 0; i < core; i++) fork();
log.info(`Starting streaming API server master with ${core} workers`)
} else {
// cluster worker
const pgConfigs = {
development: {
database: 'mastodon_development',
@ -309,5 +328,6 @@ wss.on('connection', ws => {
server.listen(process.env.PORT || 4000, () => {
log.level = process.env.LOG_LEVEL || 'verbose'
log.info(`Starting streaming API server on port ${server.address().port}`)
log.info(`Starting streaming API server worker on port ${server.address().port}`)
})
}