Problem
I have a single server that can process 10 HTTP requests in a minute. If I get more than 10 requests, then I would like to save them somewhere to respond to them later.
Latency is not a problem here, clients can wait
What I did
import express from "express";
const app = express();
const Q = [];
async function scheduler() {
// if Q is empty then wait for request to come in
if (!Q.length) {
setTimeout(scheduler, 1000);
return;
}
// send response to all currently available requests
let currentLength = Q.length;
for (let i = 0; i < currentLength; i++) Q[i].res.send("Q" + Q[i].id);
// remove the requests whose response is sent
Q.slice(0, currentLength);
scheduler();
}
scheduler();
app.get("/", (req, res) => {
// store HTTP requests
Q.push({ req, res, id: req.query.id });
});
app.listen(process.env.PORT || 5000, () => console.log("Server is running..."));
I tried to store the requests in the array, then send a response by another function.
This is just a simulation
I tried the following script to test the script
import axios from "axios";
const promises = [];
for (let i = 0; i < 25; i++) {
promises.push(axios.get(`http://localhost:5000?id=${i}`));
}
console.time();
Promise.allSettled(promises).then((res) => {
console.timeEnd();
console.log(res.map((httpRes) => httpRes?.value?.data));
});
I am getting successful response for each request
[
'Q0', 'Q1', 'Q2', 'Q3',
'Q4', 'Q5', 'Q6', 'Q7',
'Q8', 'Q9', 'Q10', 'Q11',
'Q12', 'Q13', 'Q14', 'Q15',
'Q16', 'Q17', 'Q18', 'Q19',
'Q20', 'Q21', 'Q22', 'Q23',
'Q24'
]
But, still I am getting the below error on server terminal
(node:26071) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:561:11)
at ServerResponse.header (/home/rahul/Desktop/projects/temp/puppeteer/node_modules/express/lib/response.js:776:10)
at ServerResponse.send (/home/rahul/Desktop/projects/temp/puppeteer/node_modules/express/lib/response.js:170:12)
at scheduler (file:///home/rahul/Desktop/projects/temp/puppeteer/index.js:43:52)
at Timeout.scheduler [as _onTimeout] (file:///home/rahul/Desktop/projects/temp/puppeteer/index.js:47:3)
at listOnTimeout (internal/timers.js:557:17)
at processTimers (internal/timers.js:500:7)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:26071) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:26071) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.