Improve streaming server with cluster (#1970)

feature-private-federation
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 # Advanced settings
# If you need to use pgBouncer, you need to disable prepared statements: # If you need to use pgBouncer, you need to disable prepared statements:
# PREPARED_STATEMENTS=false # 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 dotenv from 'dotenv'
import express from 'express' import express from 'express'
import http from 'http' import http from 'http'
@ -14,6 +16,23 @@ dotenv.config({
path: env === 'production' ? '.env.production' : '.env' 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 = { const pgConfigs = {
development: { development: {
database: 'mastodon_development', database: 'mastodon_development',
@ -309,5 +328,6 @@ wss.on('connection', ws => {
server.listen(process.env.PORT || 4000, () => { server.listen(process.env.PORT || 4000, () => {
log.level = process.env.LOG_LEVEL || 'verbose' 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}`)
}) })
}