Skip to content

Commit

Permalink
Fix use the physical CPU count from Express (#7986)
Browse files Browse the repository at this point in the history
  • Loading branch information
JHyeok authored Mar 7, 2023
1 parent e0fa211 commit bb94cc2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
8 changes: 5 additions & 3 deletions frameworks/JavaScript/express/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

const cluster = require('cluster'),
numCPUs = require('os').cpus().length,
physicalCpuCount = require('physical-cpu-count'),
express = require('express');

const bodyParser = require('body-parser');
Expand All @@ -13,7 +13,7 @@ if (cluster.isPrimary) {
console.log(`Primary ${process.pid} is running`);

// Fork workers.
for (let i = 0; i < numCPUs; i++) {
for (let i = 0; i < physicalCpuCount; i++) {
cluster.fork();
}

Expand Down Expand Up @@ -41,5 +41,7 @@ if (cluster.isPrimary) {
app.get('/plaintext', (req, res) =>
res.header('Content-Type', 'text/plain').send('Hello, World!'));

app.listen(8080);
app.listen(8080, () => {
console.log('listening on port 8080');
});
}
8 changes: 5 additions & 3 deletions frameworks/JavaScript/express/mongodb-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

const cluster = require('cluster'),
numCPUs = require('os').cpus().length,
physicalCpuCount = require('physical-cpu-count'),
express = require('express'),
mongoose = require('mongoose'),
conn = mongoose.connect('mongodb://tfb-database/hello_world');
Expand Down Expand Up @@ -33,7 +33,7 @@ const FortuneSchema = new mongoose.Schema({

if (cluster.isPrimary) {
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
for (let i = 0; i < physicalCpuCount; i++) {
cluster.fork();
}

Expand Down Expand Up @@ -101,5 +101,7 @@ if (cluster.isPrimary) {
res.send(results);
});

app.listen(8080);
app.listen(8080, () => {
console.log('listening on port 8080');
});
}
8 changes: 5 additions & 3 deletions frameworks/JavaScript/express/mysql-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

const cluster = require('cluster'),
numCPUs = require('os').cpus().length,
physicalCpuCount = require('physical-cpu-count'),
express = require('express'),
Sequelize = require('sequelize');

Expand Down Expand Up @@ -45,7 +45,7 @@ const Fortune = sequelize.define('Fortune', {

if (cluster.isPrimary) {
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
for (let i = 0; i < physicalCpuCount; i++) {
cluster.fork();
}

Expand Down Expand Up @@ -127,5 +127,7 @@ if (cluster.isPrimary) {
res.send(results);
});

app.listen(8080);
app.listen(8080, () => {
console.log('listening on port 8080');
});
}
1 change: 1 addition & 0 deletions frameworks/JavaScript/express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"mysql2": "2.2.5",
"pg": "8.5.0",
"pg-promise": "10.7.3",
"physical-cpu-count": "^2.0.0",
"pug": "2.0.1",
"sequelize": "5.15.1"
}
Expand Down
4 changes: 2 additions & 2 deletions frameworks/JavaScript/express/postgresql-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Module dependencies.
*/
const cluster = require('cluster'),
numCPUs = require('os').cpus().length,
physicalCpuCount = require('physical-cpu-count'),
express = require('express'),
helper = require('./helper');

Expand Down Expand Up @@ -51,7 +51,7 @@ const randomWorldPromise = () => {

if (cluster.isPrimary) {
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
for (let i = 0; i < physicalCpuCount; i++) {
cluster.fork();
}

Expand Down

0 comments on commit bb94cc2

Please sign in to comment.