GitHub PullRequestReviewCommentEvent at 2014-11-12 19:59:46
Repository: push-server-limit
Actor: jerryorr
Payload: {"action":"created","comment":{"url":"https://api.github.com/repos/faiq/push-server-limit/pulls/comments/20246233","id":20246233,"diff_hunk":"@@ -1,51 +1,54 @@\n-var redis = require('redis').createClient()\n-  , path = require('path') \n-  , redback = require('redback')\n+//a thing that does middleware logic\n+var path = require('path') \n+  , Ratelimit = require('./model') //logic for the structure we're putting in things\n \n-module.exports = Ratelimit \n+module.exports = Middleware\n \n-function Ratelimit (options) { \n-  if (!options) {\n-    this.bucket_interval = 1 \n-    this.bucket_span = 3600  \n-    this.subject_expiry = 3600 //expire the next hour, no sooner\n-    this.routes =  \"*\" //work for all routes\n-    this.key = 'remoteAddress' //special key you identify user with defaults to thier remote address\n-    this.limit = 3000 \n-  }\n-  var _this = this\n-  redis.on('error', function (error) { \n-    return function (req, res, next) { \n-      next(error) \n-    } //return somemiddleware that just throws an error object \n-  })\n-  \n-  if (!this.rateLimit) { \n-    redback.use(redis) \n-    this.rateLimit = redback.createRateLimit('requests', {\n-      bucket_interval: _this.bucket_interval,\n-      bucket_span: _this.bucket_span,\n-      subject_expiry: _this.subject_expiry \n-    })\n-  }\n-  return this.add()\n-} \n+function Middleware (options) {\n+  this.key = options.key || 'ip'\n+  this.window = options.window ||  3600\n+  if (!options.total_limit && !options.limit_object) throw Error(\"you need to pass in either a total limit or limit object for each route you want to limit\") \n+  if (options.total_limit && !options.limit_object) {\n+    this.total_limit = options.total_limit\n+    this.rateLimitList = new Ratelimit(this.window) \n+    return this.checkLimit()  \n+  } else if (options.limit_object && !options.total_limit) { \n+    this.limit_object = options.limit_object\n+    this.rateLimitList = new Ratelimit(this.window) \n+    return this.checkRouteLimit()\n+  } else {\n+    //the hard case, i haven't figured out how to do yet \n+    return function (req,res,next) { \n+      next()\n+    }\n+  } \n+}\n \n-Ratelimit.prototype.add = function () { \n-  var _this = this\n-  return function (req, res, next) { \n-    var key = _this.makeKey(path.basename(req.originalUrl))\n-    rateLimit.add(ip) \n-    rateLimit.count(ip, 20, function (err, count) { \n-      if (count > 1) { \n-        console.log('more dan 1')\n-        res.end('tooo many') \n-      } else next() \n+Middleware.prototype.checkLimit = function () { \n+  var _this = this \n+  \n+  return function (req, res, next) {\n+    var key\n+    if (_this.key === 'ip') { \n+      key = req.headers['x-forwarded-for'] || req.connection.remoteAddress\n+      _this.RateLimitList.add(req.headers['x-forwarded-for'] || req.connection.remoteAddress)\n+    } else { \n+      key = req.session[_this.key]\n+    } \n+    //how do i make sure these two don't race/is this a race condition?\n+    _this.RateLimitList.add(key) \n+    _this.rateLimitList.getCountTime(key, function (err, count, firstRequest) { ","path":"lib/middleware.js","position":80,"original_position":80,"commit_id":"2cbb68c6bfa0b3a01030cd6c8de698d9e7e2a33b","original_commit_id":"2cbb68c6bfa0b3a01030cd6c8de698d9e7e2a33b","user":{"login":"jerryorr","id":1598167,"avatar_url":"https://avatars.githubusercontent.com/u/1598167?v=3","gravatar_id":"","url":"https://api.github.com/users/jerryorr","html_url":"https://github.com/jerryorr","followers_url":"https://api.github.com/users/jerryorr/followers","following_url":"https://api.github.com/users/jerryorr/following{/other_user}","gists_url":"https://api.github.com/users/jerryorr/gists{/gist_id}","starred_url":"https://api.github.com/users/jerryorr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jerryorr/subscriptions","organizations_url":"https://api.github.com/users/jerryorr/orgs","repos_url":"https://api.github.com/users/jerryorr/repos","events_url":"https://api.github.com/users/jerryorr/events{/privacy}","received_events_url":"https://api.github.com/users/jerryorr/received_events","type":"User","site_admin":false},"body":"As for the race condition: I'm not sure that it totally matters. Your retrieval of the count could come back before the current request was incremented, but is that really a big deal? Maybe an extra request or two get through. Just make sure you handle the null/undefined/whatever that would presumably come back if the very first add completes before your retrieval.\r\n\r\nHowever, if you still want to avoid that race condition, couldn't you send a callback to the add call, and make your retrieval in that callback? Then they'd be guaranteed to happen sequentially. The response time would be a little longer, though, so I still think it'd make more sense to have things be marginally faster and risk an occasional request exceeding the rate limit.","created_at":"2014-11-12T19:59:46Z","updated_at":"2014-11-12T19:59:46Z","html_url":"https://github.com/faiq/push-server-limit/pull/1#discussion_r20246233","pull_request_url":"https://api.github.com/repos/faiq/push-server-limit/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/faiq/push-server-limit/pulls/comments/20246233"},"html":{"href":"https://github.com/faiq/push-server-limit/pull/1#discussion_r20246233"},"pull_request":{"href":"https://api.github.com/repos/faiq/push-server-limit/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/faiq/push-server-limit/pulls/1","id":24267716,"html_url":"https://github.com/faiq/push-server-limit/pull/1","diff_url":"https://github.com/faiq/push-server-limit/pull/1.diff","patch_url":"https://github.com/faiq/push-server-limit/pull/1.patch","issue_url":"https://api.github.com/repos/faiq/push-server-limit/issues/1","number":1,"state":"open","locked":false,"title":"Make own model","user":{"login":"faiq","id":5673456,"avatar_url":"https://avatars.githubusercontent.com/u/5673456?v=3","gravatar_id":"","url":"https://api.github.com/users/faiq","html_url":"https://github.com/faiq","followers_url":"https://api.github.com/users/faiq/followers","following_url":"https://api.github.com/users/faiq/following{/other_user}","gists_url":"https://api.github.com/users/faiq/gists{/gist_id}","starred_url":"https://api.github.com/users/faiq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/faiq/subscriptions","organizations_url":"https://api.github.com/users/faiq/orgs","repos_url":"https://api.github.com/users/faiq/repos","events_url":"https://api.github.com/users/faiq/events{/privacy}","received_events_url":"https://api.github.com/users/faiq/received_events","type":"User","site_admin":false},"body":"Instead of using redback I went with my own approach to store the data, there's seemed like overkill.\r\n\r\nTell me how its looking so far\r\n","created_at":"2014-11-12T03:09:36Z","updated_at":"2014-11-12T19:59:46Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c918cb39a30041d5d46d6045892ded5bb6b6b5bd","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/faiq/push-server-limit/pulls/1/commits","review_comments_url":"https://api.github.com/repos/faiq/push-server-limit/pulls/1/comments","review_comment_url":"https://api.github.com/repos/faiq/push-server-limit/pulls/comments/{number}","comments_url":"https://api.github.com/repos/faiq/push-server-limit/issues/1/comments","statuses_url":"https://api.github.com/repos/faiq/push-server-limit/statuses/2cbb68c6bfa0b3a01030cd6c8de698d9e7e2a33b","head":{"label":"faiq:make-own-model","ref":"make-own-model","sha":"2cbb68c6bfa0b3a01030cd6c8de698d9e7e2a33b","user":{"login":"faiq","id":5673456,"avatar_url":"https://avatars.githubusercontent.com/u/5673456?v=3","gravatar_id":"","url":"https://api.github.com/users/faiq","html_url":"https://github.com/faiq","followers_url":"https://api.github.com/users/faiq/followers","following_url":"https://api.github.com/users/faiq/following{/other_user}","gists_url":"https://api.github.com/users/faiq/gists{/gist_id}","starred_url":"https://api.github.com/users/faiq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/faiq/subscriptions","organizations_url":"https://api.github.com/users/faiq/orgs","repos_url":"https://api.github.com/users/faiq/repos","events_url":"https://api.github.com/users/faiq/events{/privacy}","received_events_url":"https://api.github.com/users/faiq/received_events","type":"User","site_admin":false},"repo":{"id":26188004,"name":"push-server-limit","full_name":"faiq/push-server-limit","owner":{"login":"faiq","id":5673456,"avatar_url":"https://avatars.githubusercontent.com/u/5673456?v=3","gravatar_id":"","url":"https://api.github.com/users/faiq","html_url":"https://github.com/faiq","followers_url":"https://api.github.com/users/faiq/followers","following_url":"https://api.github.com/users/faiq/following{/other_user}","gists_url":"https://api.github.com/users/faiq/gists{/gist_id}","starred_url":"https://api.github.com/users/faiq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/faiq/subscriptions","organizations_url":"https://api.github.com/users/faiq/orgs","repos_url":"https://api.github.com/users/faiq/repos","events_url":"https://api.github.com/users/faiq/events{/privacy}","received_events_url":"https://api.github.com/users/faiq/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/faiq/push-server-limit","description":"potential express middleware to do rate limiting on top of redback","fork":false,"url":"https://api.github.com/repos/faiq/push-server-limit","forks_url":"https://api.github.com/repos/faiq/push-server-limit/forks","keys_url":"https://api.github.com/repos/faiq/push-server-limit/keys{/key_id}","collaborators_url":"https://api.github.com/repos/faiq/push-server-limit/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/faiq/push-server-limit/teams","hooks_url":"https://api.github.com/repos/faiq/push-server-limit/hooks","issue_events_url":"https://api.github.com/repos/faiq/push-server-limit/issues/events{/number}","events_url":"https://api.github.com/repos/faiq/push-server-limit/events","assignees_url":"https://api.github.com/repos/faiq/push-server-limit/assignees{/user}","branches_url":"https://api.github.com/repos/faiq/push-server-limit/branches{/branch}","tags_url":"https://api.github.com/repos/faiq/push-server-limit/tags","blobs_url":"https://api.github.com/repos/faiq/push-server-limit/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/faiq/push-server-limit/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/faiq/push-server-limit/git/refs{/sha}","trees_url":"https://api.github.com/repos/faiq/push-server-limit/git/trees{/sha}","statuses_url":"https://api.github.com/repos/faiq/push-server-limit/statuses/{sha}","languages_url":"https://api.github.com/repos/faiq/push-server-limit/languages","stargazers_url":"https://api.github.com/repos/faiq/push-server-limit/stargazers","contributors_url":"https://api.github.com/repos/faiq/push-server-limit/contributors","subscribers_url":"https://api.github.com/repos/faiq/push-server-limit/subscribers","subscription_url":"https://api.github.com/repos/faiq/push-server-limit/subscription","commits_url":"https://api.github.com/repos/faiq/push-server-limit/commits{/sha}","git_commits_url":"https://api.github.com/repos/faiq/push-server-limit/git/commits{/sha}","comments_url":"https://api.github.com/repos/faiq/push-server-limit/comments{/number}","issue_comment_url":"https://api.github.com/repos/faiq/push-server-limit/issues/comments/{number}","contents_url":"https://api.github.com/repos/faiq/push-server-limit/contents/{+path}","compare_url":"https://api.github.com/repos/faiq/push-server-limit/compare/{base}...{head}","merges_url":"https://api.github.com/repos/faiq/push-server-limit/merges","archive_url":"https://api.github.com/repos/faiq/push-server-limit/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/faiq/push-server-limit/downloads","issues_url":"https://api.github.com/repos/faiq/push-server-limit/issues{/number}","pulls_url":"https://api.github.com/repos/faiq/push-server-limit/pulls{/number}","milestones_url":"https://api.github.com/repos/faiq/push-server-limit/milestones{/number}","notifications_url":"https://api.github.com/repos/faiq/push-server-limit/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/faiq/push-server-limit/labels{/name}","releases_url":"https://api.github.com/repos/faiq/push-server-limit/releases{/id}","created_at":"2014-11-04T20:47:43Z","updated_at":"2014-11-04T21:39:48Z","pushed_at":"2014-11-12T08:11:06Z","git_url":"git://github.com/faiq/push-server-limit.git","ssh_url":"git@github.com:faiq/push-server-limit.git","clone_url":"https://github.com/faiq/push-server-limit.git","svn_url":"https://github.com/faiq/push-server-limit","homepage":null,"size":116,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"faiq:master","ref":"master","sha":"e5a419b8bf46322852ff74bb00dc5a052ea8fb66","user":{"login":"faiq","id":5673456,"avatar_url":"https://avatars.githubusercontent.com/u/5673456?v=3","gravatar_id":"","url":"https://api.github.com/users/faiq","html_url":"https://github.com/faiq","followers_url":"https://api.github.com/users/faiq/followers","following_url":"https://api.github.com/users/faiq/following{/other_user}","gists_url":"https://api.github.com/users/faiq/gists{/gist_id}","starred_url":"https://api.github.com/users/faiq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/faiq/subscriptions","organizations_url":"https://api.github.com/users/faiq/orgs","repos_url":"https://api.github.com/users/faiq/repos","events_url":"https://api.github.com/users/faiq/events{/privacy}","received_events_url":"https://api.github.com/users/faiq/received_events","type":"User","site_admin":false},"repo":{"id":26188004,"name":"push-server-limit","full_name":"faiq/push-server-limit","owner":{"login":"faiq","id":5673456,"avatar_url":"https://avatars.githubusercontent.com/u/5673456?v=3","gravatar_id":"","url":"https://api.github.com/users/faiq","html_url":"https://github.com/faiq","followers_url":"https://api.github.com/users/faiq/followers","following_url":"https://api.github.com/users/faiq/following{/other_user}","gists_url":"https://api.github.com/users/faiq/gists{/gist_id}","starred_url":"https://api.github.com/users/faiq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/faiq/subscriptions","organizations_url":"https://api.github.com/users/faiq/orgs","repos_url":"https://api.github.com/users/faiq/repos","events_url":"https://api.github.com/users/faiq/events{/privacy}","received_events_url":"https://api.github.com/users/faiq/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/faiq/push-server-limit","description":"potential express middleware to do rate limiting on top of redback","fork":false,"url":"https://api.github.com/repos/faiq/push-server-limit","forks_url":"https://api.github.com/repos/faiq/push-server-limit/forks","keys_url":"https://api.github.com/repos/faiq/push-server-limit/keys{/key_id}","collaborators_url":"https://api.github.com/repos/faiq/push-server-limit/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/faiq/push-server-limit/teams","hooks_url":"https://api.github.com/repos/faiq/push-server-limit/hooks","issue_events_url":"https://api.github.com/repos/faiq/push-server-limit/issues/events{/number}","events_url":"https://api.github.com/repos/faiq/push-server-limit/events","assignees_url":"https://api.github.com/repos/faiq/push-server-limit/assignees{/user}","branches_url":"https://api.github.com/repos/faiq/push-server-limit/branches{/branch}","tags_url":"https://api.github.com/repos/faiq/push-server-limit/tags","blobs_url":"https://api.github.com/repos/faiq/push-server-limit/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/faiq/push-server-limit/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/faiq/push-server-limit/git/refs{/sha}","trees_url":"https://api.github.com/repos/faiq/push-server-limit/git/trees{/sha}","statuses_url":"https://api.github.com/repos/faiq/push-server-limit/statuses/{sha}","languages_url":"https://api.github.com/repos/faiq/push-server-limit/languages","stargazers_url":"https://api.github.com/repos/faiq/push-server-limit/stargazers","contributors_url":"https://api.github.com/repos/faiq/push-server-limit/contributors","subscribers_url":"https://api.github.com/repos/faiq/push-server-limit/subscribers","subscription_url":"https://api.github.com/repos/faiq/push-server-limit/subscription","commits_url":"https://api.github.com/repos/faiq/push-server-limit/commits{/sha}","git_commits_url":"https://api.github.com/repos/faiq/push-server-limit/git/commits{/sha}","comments_url":"https://api.github.com/repos/faiq/push-server-limit/comments{/number}","issue_comment_url":"https://api.github.com/repos/faiq/push-server-limit/issues/comments/{number}","contents_url":"https://api.github.com/repos/faiq/push-server-limit/contents/{+path}","compare_url":"https://api.github.com/repos/faiq/push-server-limit/compare/{base}...{head}","merges_url":"https://api.github.com/repos/faiq/push-server-limit/merges","archive_url":"https://api.github.com/repos/faiq/push-server-limit/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/faiq/push-server-limit/downloads","issues_url":"https://api.github.com/repos/faiq/push-server-limit/issues{/number}","pulls_url":"https://api.github.com/repos/faiq/push-server-limit/pulls{/number}","milestones_url":"https://api.github.com/repos/faiq/push-server-limit/milestones{/number}","notifications_url":"https://api.github.com/repos/faiq/push-server-limit/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/faiq/push-server-limit/labels{/name}","releases_url":"https://api.github.com/repos/faiq/push-server-limit/releases{/id}","created_at":"2014-11-04T20:47:43Z","updated_at":"2014-11-04T21:39:48Z","pushed_at":"2014-11-12T08:11:06Z","git_url":"git://github.com/faiq/push-server-limit.git","ssh_url":"git@github.com:faiq/push-server-limit.git","clone_url":"https://github.com/faiq/push-server-limit.git","svn_url":"https://github.com/faiq/push-server-limit","homepage":null,"size":116,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/faiq/push-server-limit/pulls/1"},"html":{"href":"https://github.com/faiq/push-server-limit/pull/1"},"issue":{"href":"https://api.github.com/repos/faiq/push-server-limit/issues/1"},"comments":{"href":"https://api.github.com/repos/faiq/push-server-limit/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/faiq/push-server-limit/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/faiq/push-server-limit/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/faiq/push-server-limit/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/faiq/push-server-limit/statuses/2cbb68c6bfa0b3a01030cd6c8de698d9e7e2a33b"}}}}