1
0
Fork 0
forked from svrjs/svrjs

Disabled checking for hung up server processes, while SVR.JS is not listening yet. Also improved minimum thread count calculation algorithm.

This commit is contained in:
Dorian Niemiec 2023-08-09 22:37:53 +02:00
parent 6515a54471
commit 889207e6a2
2 changed files with 23 additions and 19 deletions

View file

@ -3,7 +3,7 @@
"port": 80, "port": 80,
"pubport": 80, "pubport": 80,
"page404": "404.html", "page404": "404.html",
"timestamp": 1691607311790, "timestamp": 1691613309162,
"blacklist": [], "blacklist": [],
"nonStandardCodes": [], "nonStandardCodes": [],
"enableCompression": true, "enableCompression": true,

38
svr.js
View file

@ -1801,7 +1801,7 @@ if (!cluster.isPrimary) {
} }
}); });
server2.once("listening", function () { server2.on("listening", function () {
listeningMessage(); listeningMessage();
}); });
@ -4556,11 +4556,13 @@ if (!cluster.isPrimary) {
} }
}); });
server.once("listening", function () { server.on("listening", function () {
listeningMessage(); listeningMessage();
}); });
} }
var closedMaster = true;
function listenConnListener(msg) { function listenConnListener(msg) {
if (msg == "\x12LISTEN") { if (msg == "\x12LISTEN") {
listeningMessage(); listeningMessage();
@ -4620,8 +4622,6 @@ function msgListener(msg) {
serverconsole.locmessage("Configuration saved."); serverconsole.locmessage("Configuration saved.");
} else if (msg.indexOf("\x12SAVEERR") == 0) { } else if (msg.indexOf("\x12SAVEERR") == 0) {
serverconsole.locwarnmessage("There was a problem, while saving configuration file. Reason: " + msg.substr(8)); serverconsole.locwarnmessage("There was a problem, while saving configuration file. Reason: " + msg.substr(8));
} else if (msg == "\x12OPEN") {
closedMaster = false;
} else if (msg == "\x12END") { } else if (msg == "\x12END") {
cluster.workers[Object.keys(cluster.workers)[0]].on("message", function (msg) { cluster.workers[Object.keys(cluster.workers)[0]].on("message", function (msg) {
if (msg.length > 9 && msg.indexOf("\x12ERRLIST") == 0) { if (msg.length > 9 && msg.indexOf("\x12ERRLIST") == 0) {
@ -4675,12 +4675,13 @@ function msgListener(msg) {
var messageTransmitted = false; var messageTransmitted = false;
function listeningMessage() { function listeningMessage() {
if (messageTransmitted) return; if(typeof closedMaster !== "undefined") closedMaster = false;
messageTransmitted = true;
if (!cluster.isPrimary && cluster.isPrimary !== undefined) { if (!cluster.isPrimary && cluster.isPrimary !== undefined) {
process.send("\x12LISTEN"); process.send("\x12LISTEN");
return; return;
} }
if (messageTransmitted) return;
messageTransmitted = true;
serverconsole.locmessage("Started server at: "); serverconsole.locmessage("Started server at: ");
if (secure) { if (secure) {
if (typeof sport === "number") { if (typeof sport === "number") {
@ -4713,8 +4714,6 @@ function listeningMessage() {
}); });
} }
var closedMaster = false;
function start(init) { function start(init) {
init = Boolean(init); init = Boolean(init);
if (cluster.isPrimary || cluster.isPrimary === undefined) { if (cluster.isPrimary || cluster.isPrimary === undefined) {
@ -4798,7 +4797,6 @@ function start(init) {
if (cluster.isPrimary === undefined) serverconsole.climessage("Server opened."); if (cluster.isPrimary === undefined) serverconsole.climessage("Server opened.");
else { else {
process.send("Server opened."); process.send("Server opened.");
process.send("\x12OPEN");
} }
} catch (err) { } catch (err) {
if (cluster.isPrimary === undefined) serverconsole.climessage("Cannot open server! Reason: " + err.message); if (cluster.isPrimary === undefined) serverconsole.climessage("Cannot open server! Reason: " + err.message);
@ -4972,6 +4970,7 @@ function start(init) {
} }
if (stopError) serverconsole.climessage("Some SVR.JS workers might not be stopped."); if (stopError) serverconsole.climessage("Some SVR.JS workers might not be stopped.");
SVRJSInitialized = false; SVRJSInitialized = false;
closedMaster = true;
var cpus = os.cpus().length; var cpus = os.cpus().length;
if (cpus > 16) cpus = 16; if (cpus > 16) cpus = 16;
try { try {
@ -5121,7 +5120,7 @@ function start(init) {
"X-SVR-JS-From-Main-Thread": "true", "X-SVR-JS-From-Main-Thread": "true",
"User-Agent": (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS") "User-Agent": (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS")
}, },
timeout: 2000, timeout: 1625,
rejectUnauthorized: false rejectUnauthorized: false
}, function (res) { }, function (res) {
chksocket.removeAllListeners("timeout"); chksocket.removeAllListeners("timeout");
@ -5147,7 +5146,7 @@ function start(init) {
else crashed = false; else crashed = false;
} }
}); });
connection.setTimeout(2000, function () { connection.setTimeout(1625, function () {
if (!exiting) SVRJSFork(); if (!exiting) SVRJSFork();
crashed = true; crashed = true;
}); });
@ -5174,7 +5173,7 @@ function start(init) {
"X-SVR-JS-From-Main-Thread": "true", "X-SVR-JS-From-Main-Thread": "true",
"User-Agent": (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS") "User-Agent": (exposeServerVersion ? "SVR.JS/" + version + " (" + getOS() + "; " + (process.isBun ? ("Bun/v" + process.versions.bun + "; like Node.JS/" + process.version) : ("Node.JS/" + process.version)) + ")" : "SVR.JS")
}, },
timeout: 2000 timeout: 1625
}, function (res) { }, function (res) {
chksocket.removeAllListeners("timeout"); chksocket.removeAllListeners("timeout");
res.destroy(); res.destroy();
@ -5192,15 +5191,20 @@ function start(init) {
}); });
} }
} }
}, 5000); }, 4500);
// Termination of unused good workers // Termination of unused good workers
if(cluster.isMaster !== undefined) { if(cluster.isPrimary !== undefined) {
setTimeout(function () { setTimeout(function () {
setInterval(function () { setInterval(function () {
if (!closedMaster && !exiting) { if (!closedMaster && !exiting) {
var allClusters = Object.keys(cluster.workers); var allClusters = Object.keys(cluster.workers);
var minClusters = Math.ceil(cpus / 2); var minClusters = 0;
if(cpus < 8) {
minClusters = Math.ceil(cpus * 0.65);
} else {
minClusters = Math.ceil(cpus * 0.55);
}
if(minClusters < 2) minClusters = 2; if(minClusters < 2) minClusters = 2;
var goodWorkers = []; var goodWorkers = [];
function checkWorker(callback, _id) { function checkWorker(callback, _id) {
@ -5255,8 +5259,8 @@ function start(init) {
} }
}); });
} }
}, 120000); }, 150000);
}, 2500); }, 2000);
} }
} }
} }