add sticky session with cluster

This commit is contained in:
2023-10-05 10:57:04 +07:00
parent 0f34326da4
commit a8e0376496
3 changed files with 43 additions and 19 deletions
+20 -19
View File
@@ -1,9 +1,10 @@
require('dotenv').config();
// const cluster = require('node:cluster');
// const totalCPUs = require('node:os').cpus().length;
const cluster = require('node:cluster');
const totalCPUs = require('node:os').cpus().length;
const process = require('node:process');
const winston = require('./application/helpers/winston.logger');
const server = require('./server');
const sticky = require('sticky-session');
// set port, listen for requests
const PORT = process.env.PORT || 3000;
@@ -31,31 +32,31 @@ if (process.env.CHANGE_TO_DRC == 'YES' || process.env.CHANGE_TO_DRC == 'yes') {
// jika mau di cluster gunakan script berikut:
// if (cluster.isMaster) {
// console.log(`Number of CPUs is ${totalCPUs}`);
// console.log(`Master ${process.pid} is running`);
if (cluster.isMaster) {
console.log(`Number of CPUs is ${totalCPUs}`);
console.log(`Master ${process.pid} is running`);
// // Fork workers.
// for (let i = 0; i < totalCPUs; i++) {
// cluster.fork();
// }
// Fork workers.
for (let i = 0; i < totalCPUs; i++) {
cluster.fork();
}
// cluster.on('exit', (worker, code, signal) => {
// console.log(`worker ${worker.process.pid} died`);
// console.log("Let's fork another worker!");
// cluster.fork();
// });
// } else {
// startExpress();
// }
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
console.log("Let's fork another worker!");
cluster.fork();
});
} else {
startExpress();
}
// console.log(`worker pid=${process.pid}`);
// jika tidak mau di cluster langsung call function startExpress
startExpress();
// startExpress();
function startExpress() {
server.listen(PORT, () => {
sticky.listen(server, PORT, () => {
winston.logger.info(
`Server is running on version ${
process.env.VERSION
+22
View File
@@ -28,6 +28,7 @@
"prom-client": "^14.2.0",
"redis": "^4.6.5",
"response-time": "^2.3.2",
"sticky-session": "^1.1.2",
"winston": "^3.7.2",
"winston-daily-rotate-file": "^4.7.1",
"winston-loki": "^6.0.6",
@@ -8301,6 +8302,18 @@
"node": ">= 0.8"
}
},
"node_modules/sticky-session": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/sticky-session/-/sticky-session-1.1.2.tgz",
"integrity": "sha512-1PPeJYu5SqjYcBNp93On4cQJuN7q+M+RbVjmYW1KSCKSv/rN1cVVyBBB0kCexsg0qJpQXgfHOt6bCneJTYRafQ==",
"dependencies": {
"debug": "^2.2.0",
"ip": "^1.0.0"
},
"engines": {
"node": ">= 0.12.0"
}
},
"node_modules/string_decoder": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz",
@@ -15338,6 +15351,15 @@
"resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.1.tgz",
"integrity": "sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ=="
},
"sticky-session": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/sticky-session/-/sticky-session-1.1.2.tgz",
"integrity": "sha512-1PPeJYu5SqjYcBNp93On4cQJuN7q+M+RbVjmYW1KSCKSv/rN1cVVyBBB0kCexsg0qJpQXgfHOt6bCneJTYRafQ==",
"requires": {
"debug": "^2.2.0",
"ip": "^1.0.0"
}
},
"string_decoder": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz",
+1
View File
@@ -41,6 +41,7 @@
"prom-client": "^14.2.0",
"redis": "^4.6.5",
"response-time": "^2.3.2",
"sticky-session": "^1.1.2",
"winston": "^3.7.2",
"winston-daily-rotate-file": "^4.7.1",
"winston-loki": "^6.0.6",