Skip to content

Instantly share code, notes, and snippets.

@mohankumar-i2i
Created February 16, 2020 03:34
Show Gist options
  • Save mohankumar-i2i/e3fe4a25fd53d9ee647322cfa93e6a72 to your computer and use it in GitHub Desktop.
Save mohankumar-i2i/e3fe4a25fd53d9ee647322cfa93e6a72 to your computer and use it in GitHub Desktop.
const cluster = require("cluster");
const express = require("express");
const numCPUs = require("os").cpus().length;
const app = require
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on("exit", (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
// Workers can share any TCP connection
const express = require("express");
const app = express();
const port = 3000;
app.get("/slow", (req, res) => {
var waitTill = new Date(new Date().getTime() + 5 * 1000);
while (waitTill > new Date()) {}
res.send("i am slow");
});
app.get("/fast", (req, res) => {
res.send("i am fast");
});
app.listen(port, () => console.log(`App listening on port ${port}!`));
}
const express = require("express");
const app = express();
const port = 3000;
app.get("/slow", (req, res) => {
var waitTill = new Date(new Date().getTime() + 5 * 1000);
while (waitTill > new Date()) {}
res.send("i am slow");
});
app.get("/fast", (req, res) => {
res.send("i am fast");
});
app.listen(port, () => console.log(`App listening on port ${port}!`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment