mirror of
https://github.com/kikobar/mastodon.git
synced 2024-12-24 23:04:14 +00:00
Improve streaming server with cluster (#1970)
This commit is contained in:
parent
7a5086729a
commit
64e1d51025
|
@ -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
|
||||
|
|
|
@ -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}`)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue