File tree Expand file tree Collapse file tree 3 files changed +13
-12
lines changed
frameworks/JavaScript/nodejs Expand file tree Collapse file tree 3 files changed +13
-12
lines changed Original file line number Diff line number Diff line change 1
- const cluster = require ( 'cluster' ) ;
2
- const numCPUs = require ( 'os' ) . cpus ( ) . length ;
1
+ const cluster = require ( 'node:cluster' ) ;
2
+ const { availableParallelism } = require ( 'node:os' ) ;
3
+ const numCPUs = availableParallelism ( ) ;
3
4
4
5
process . env . NODE_HANDLER = 'postgres' ;
5
6
@@ -13,18 +14,18 @@ if (process.env.TFB_TEST_NAME === 'nodejs-mongodb') {
13
14
process . env . NODE_HANDLER = 'mysql-raw' ;
14
15
} else if ( process . env . TFB_TEST_NAME === 'nodejs-postgres' ) {
15
16
process . env . NODE_HANDLER = 'sequelize-postgres' ;
16
- } else if ( process . env . TFB_TEST_NAME === 'nodejs-postgresjs-raw' ) {
17
+ } else if ( process . env . TFB_TEST_NAME === 'nodejs-postgresjs-raw' ) {
17
18
process . env . NODE_HANDLER = 'postgres' ;
18
19
}
19
20
20
- if ( cluster . isPrimary ) {
21
+ if ( numCPUs > 1 && cluster . isPrimary ) {
21
22
console . log ( `Primary ${ process . pid } is running` ) ;
22
23
23
24
// Fork workers.
24
25
for ( let i = 0 ; i < numCPUs ; i ++ ) {
25
26
cluster . fork ( ) ;
26
27
}
27
-
28
+
28
29
cluster . on ( 'exit' , ( worker , code , signal ) => {
29
30
console . log ( `worker ${ worker . process . pid } died` ) ;
30
31
process . exit ( 1 ) ;
Original file line number Diff line number Diff line change 1
1
// Forked workers will run this code when found to not be
2
2
// the master of the cluster.
3
3
4
- const http = require ( 'http' ) ;
4
+ const http = require ( 'node: http' ) ;
5
5
const parseurl = require ( 'parseurl' ) ; // faster than native nodejs url package
6
6
7
7
// Initialize routes & their handlers (once)
Original file line number Diff line number Diff line change @@ -17,14 +17,14 @@ const dbfind = async (id) =>
17
17
( arr ) => arr [ 0 ]
18
18
) ;
19
19
20
- const dbbulkUpdate = async ( worlds ) =>
20
+ const dbbulkUpdate = async ( worlds ) => {
21
+ const sorted = sql ( worlds
22
+ . map ( ( world ) => [ world . id , world . randomNumber ] )
23
+ . sort ( ( a , b ) => ( a [ 0 ] < b [ 0 ] ? - 1 : 1 ) ) ) ;
21
24
await sql `UPDATE world SET randomNumber = (update_data.randomNumber)::int
22
- FROM (VALUES ${ sql (
23
- worlds
24
- . map ( ( world ) => [ world . id , world . randomNumber ] )
25
- . sort ( ( a , b ) => ( a [ 0 ] < b [ 0 ] ? - 1 : 1 ) )
26
- ) } ) AS update_data (id, randomNumber)
25
+ FROM (VALUES ${ sorted } ) AS update_data (id, randomNumber)
27
26
WHERE world.id = (update_data.id)::int` ;
27
+ } ;
28
28
29
29
const dbgetAllWorlds = async ( ) => sql `SELECT id, randomNumber FROM world` ;
30
30
You can’t perform that action at this time.
0 commit comments