Created
August 15, 2012 12:51
-
-
Save spion/3359887 to your computer and use it in GitHub Desktop.
hacky request limiter example for connect/express
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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