@@ -3,25 +3,59 @@ import logger from '@/utils/logger';
3
3
import { getLocalhostAddress } from '@/utils/common-utils' ;
4
4
import { config } from '@/config' ;
5
5
import app from '@/app' ;
6
+ import os from 'node:os' ;
7
+ import cluster from 'node:cluster' ;
8
+ import process from 'node:process' ;
6
9
7
10
const port = config . connect . port ;
8
11
const hostIPList = getLocalhostAddress ( ) ;
9
12
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
+ }
15
49
}
16
- }
17
50
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
+ }
26
60
27
61
export default server ;
0 commit comments