Permalink
Browse files

Add RateLimit

  • Loading branch information...
1 parent 40fb495 commit e73085124c1c1bee82cd6e810db190e90519761b @tlrobinson committed Oct 8, 2012
Showing with 66 additions and 3 deletions.
  1. +3 −3 README.md
  2. +2 −0 jack2.js
  3. +29 −0 middleware/ratelimit.js
  4. +32 −0 package.json
View
6 README.md
@@ -1,4 +1,4 @@
-jack2
-=====
+Jack 2
+======
-Jack HTTP middleware for Node.js/Q-JSGI/JSGI2
+Work in Progress.
View
2 jack2.js
@@ -0,0 +1,2 @@
+
+exports.RateLimit = require("./middleware/ratelimit");
View
29 middleware/ratelimit.js
@@ -0,0 +1,29 @@
+
+var Q = require("q");
+var Q_HTTP = require("q-http");
+
+module.exports = function RateLimit(app, interval) {
+ var pending = [];
+ var inProgress = false;
+ function next() {
+ if (!inProgress) {
+ var p = pending.shift();
+ if (p) {
+ inProgress = true;
+ Q.when(app(p.request), function(response) {
+ p.deferred.resolve(response);
+ setTimeout(function() {
+ inProgress = false;
+ next();
+ }, interval);
+ });
+ }
+ }
+ }
+ return function(request) {
+ var deferred = Q.defer();
+ pending.push({ request: request, deferred: deferred });
+ next();
+ return deferred.promise;
+ }
+}
View
32 package.json
@@ -0,0 +1,32 @@
+{
+ "name": "jack2",
+ "version": "0.0.0",
+ "description": "HTTP middleware for Node.js/Q-JSGI/JSGI2",
+ "main": "jack2.js",
+ "dependencies": {
+ "q": "~0.8.9",
+ "q-http": "~0.1.14"
+ },
+ "scripts": {
+ "test": "echo \"Error: no test specified\" && exit 1"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/tlrobinson/jack2.git"
+ },
+ "keywords": [
+ "jack",
+ "jsgi",
+ "jsgi2",
+ "q-jsgi",
+ "q",
+ "q-http",
+ "http",
+ "middleware",
+ "rack",
+ "wsgi",
+ "connect"
+ ],
+ "author": "Tom Robinson <tlrobinson@gmail.com> (http://tlrobinson.net/)",
+ "license": "BSD"
+}

0 comments on commit e730851

Please sign in to comment.