Skip to content

Instantly share code, notes, and snippets.

@spion
Created August 15, 2012 12:51
Show Gist options
  • Save spion/3359887 to your computer and use it in GitHub Desktop.
Save spion/3359887 to your computer and use it in GitHub Desktop.
hacky request limiter example for connect/express
var requestlimit = function(num) {
var queue = [];
return function(req, res, next) {
queue.push(next);
var removeAndNext = function() {
var qindex = queue.indexOf(next);
if (qindex >= 0) delete queue[qindex];
if (queue.length) queue[0]();
}
var oldresend = res.end;
res.end = function() {
if (!oldresend.apply(this, arguments))
res.on('drain', removeAndNext)
else removeAndNext();
}
res.on('close', removeAndNext);
if (queue.length <= num) next();
}
}
app.use("/url", requestlimit(5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment