Skip to content

Commit 4a84b02

Browse files
committed
feat: implement enableCluster
1 parent aefd162 commit 4a84b02

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

β€Žlib/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ const calculateValue = () => {
456456
const _value = {
457457
// app config
458458
disallowRobot: toBoolean(envs.DISALLOW_ROBOT, false),
459-
enableCluster: envs.ENABLE_CLUSTER,
459+
enableCluster: toBoolean(envs.ENABLE_CLUSTER, false),
460460
isPackage: !!envs.IS_PACKAGE,
461461
nodeName: envs.NODE_NAME,
462462
puppeteerWSEndpoint: envs.PUPPETEER_WS_ENDPOINT,

β€Žlib/index.ts

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,59 @@ import logger from '@/utils/logger';
33
import { getLocalhostAddress } from '@/utils/common-utils';
44
import { config } from '@/config';
55
import app from '@/app';
6+
import os from 'node:os';
7+
import cluster from 'node:cluster';
8+
import process from 'node:process';
69

710
const port = config.connect.port;
811
const hostIPList = getLocalhostAddress();
912

10-
logger.info(`πŸŽ‰ RSSHub is running on port ${port}! Cheers!`);
11-
logger.info(`πŸ”— Local: πŸ‘‰ http://localhost:${port}`);
12-
if (config.listenInaddrAny) {
13-
for (const ip of hostIPList) {
14-
logger.info(`πŸ”— Network: πŸ‘‰ http://${ip}:${port}`);
13+
let server;
14+
if (config.enableCluster) {
15+
if (cluster.isPrimary) {
16+
logger.info(`πŸŽ‰ RSSHub is running on port ${port}! Cheers!`);
17+
logger.info(`πŸ”— Local: πŸ‘‰ http://localhost:${port}`);
18+
if (config.listenInaddrAny) {
19+
for (const ip of hostIPList) {
20+
logger.info(`πŸ”— Network: πŸ‘‰ http://${ip}:${port}`);
21+
}
22+
}
23+
24+
logger.info(`Primary ${process.pid} is running`);
25+
26+
const numCPUs = os.availableParallelism();
27+
28+
for (let i = 0; i < numCPUs; i++) {
29+
cluster.fork();
30+
}
31+
} else {
32+
logger.info(`Worker ${process.pid} is running`);
33+
serve({
34+
fetch: app.fetch,
35+
hostname: config.listenInaddrAny ? '::' : '127.0.0.1',
36+
port,
37+
serverOptions: {
38+
maxHeaderSize: 1024 * 32,
39+
},
40+
});
41+
}
42+
} else {
43+
logger.info(`πŸŽ‰ RSSHub is running on port ${port}! Cheers!`);
44+
logger.info(`πŸ”— Local: πŸ‘‰ http://localhost:${port}`);
45+
if (config.listenInaddrAny) {
46+
for (const ip of hostIPList) {
47+
logger.info(`πŸ”— Network: πŸ‘‰ http://${ip}:${port}`);
48+
}
1549
}
16-
}
1750

18-
const server = serve({
19-
fetch: app.fetch,
20-
hostname: config.listenInaddrAny ? '::' : '127.0.0.1',
21-
port,
22-
serverOptions: {
23-
maxHeaderSize: 1024 * 32,
24-
},
25-
});
51+
server = serve({
52+
fetch: app.fetch,
53+
hostname: config.listenInaddrAny ? '::' : '127.0.0.1',
54+
port,
55+
serverOptions: {
56+
maxHeaderSize: 1024 * 32,
57+
},
58+
});
59+
}
2660

2761
export default server;

0 commit comments

Comments
Β (0)