by mjpuser · 8y ago · 0 comments
const http = require('http');
let count = 0;
const THRESHOLD = 1;
const server = http.createServer((req, res) => {
if (count++ < THRESHOLD) {
// do work
res.write('success');
res.end();
setTimeout(() => count--, 10 * 1000);
} else {
res.write('limit');
res.end();
count--;
}
});
server.listen(10000);Second, your solution does not limit the acceptance of new incoming client connections, it only limits the frequency with which "work" is done. That solves some possible problems, true, but it does not solve the problem that the post is about.
On the other hand, I see no evidence in this blog post that callbacks are the inherent cause of the flow-control problem; it's rather that high-level languages that abstract the connection event are the inherent cause. Callbacks or no callbacks, the problem is the level of abstraction.