{"action":"created","comment":{"url":"https://api.github.com/repos/rackerlabs/arsenal/pulls/comments/30543204","id":30543204,"diff_hunk":"@@ -0,0 +1,101 @@\n+# -*- encoding: utf-8 -*-\n+#\n+# Copyright 2015 Rackspace\n+# All Rights Reserved.\n+#\n+#    Licensed under the Apache License, Version 2.0 (the \"License\"); you may\n+#    not use this file except in compliance with the License. You may obtain\n+#    a copy of the License at\n+#\n+#         http://www.apache.org/licenses/LICENSE-2.0\n+#\n+#    Unless required by applicable law or agreed to in writing, software\n+#    distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n+#    License for the specific language governing permissions and limitations\n+#    under the License.\n+\n+import datetime\n+\n+ZERO_DELTA = datetime.timedelta()\n+\n+\n+# NOTE(ClifHouck): Wrapper function for datetime.now() to make it mockable.\n+def now():\n+    return datetime.datetime.now()\n+\n+\n+class RateLimiter(object):\n+    \"\"\"This class provides rate-limiting behavior.\"\"\"\n+\n+    def __init__(self, limit, limit_period):\n+        \"\"\"Constructs a RateLimiter object.\n+\n+            :param: limit - An integral limit to observe. Once the limit is\n+                hit, no more items will be returned until the current\n+                limit_period expires.\n+            :param: limit_period - Integral time in seconds. The period in\n+                which withdrawing items counts towards the current period's\n+                rate limit.\n+            :returns: A RateLimiter object.\n+        \"\"\"\n+        if not isinstance(limit, int):\n+            raise TypeError(\"limit must be an integer, wrong type received\")\n+        if not isinstance(limit_period, int):\n+            raise TypeError(\"limit_period must be an integer, \"\n+                            \"wrong type received\")\n+        if limit <= 0:\n+            raise ValueError(\"limit must be a positive integer\")\n+        if limit_period <= 0:\n+            raise ValueError(\"limit_period must be a positive integer\")\n+\n+        self.limit = limit\n+        self.limit_period = limit_period\n+        self.limit_period_delta = datetime.timedelta(days=0,\n+                                                     seconds=limit_period)\n+        self.current_limit_period_start = now()\n+        self.current_count = 0\n+        self.items = []","path":"arsenal/common/rate_limiter.py","position":58,"original_position":58,"commit_id":"ea0d2dd198415d2ba3feea1693255fe39a92bc92","original_commit_id":"ea0d2dd198415d2ba3feea1693255fe39a92bc92","user":{"login":"supermari0","id":993860,"avatar_url":"https://avatars.githubusercontent.com/u/993860?v=3","gravatar_id":"","url":"https://api.github.com/users/supermari0","html_url":"https://github.com/supermari0","followers_url":"https://api.github.com/users/supermari0/followers","following_url":"https://api.github.com/users/supermari0/following{/other_user}","gists_url":"https://api.github.com/users/supermari0/gists{/gist_id}","starred_url":"https://api.github.com/users/supermari0/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/supermari0/subscriptions","organizations_url":"https://api.github.com/users/supermari0/orgs","repos_url":"https://api.github.com/users/supermari0/repos","events_url":"https://api.github.com/users/supermari0/events{/privacy}","received_events_url":"https://api.github.com/users/supermari0/received_events","type":"User","site_admin":false},"body":"I don't think any race conditions could happen, but I'm not that familiar with arsenal. Seems like this could be a good case for a Queue if any of this is threaded? https://docs.python.org/2/library/queue.html","created_at":"2015-05-18T20:17:20Z","updated_at":"2015-05-18T20:17:20Z","html_url":"https://github.com/rackerlabs/arsenal/pull/30#discussion_r30543204","pull_request_url":"https://api.github.com/repos/rackerlabs/arsenal/pulls/30","_links":{"self":{"href":"https://api.github.com/repos/rackerlabs/arsenal/pulls/comments/30543204"},"html":{"href":"https://github.com/rackerlabs/arsenal/pull/30#discussion_r30543204"},"pull_request":{"href":"https://api.github.com/repos/rackerlabs/arsenal/pulls/30"}}},"pull_request":{"url":"https://api.github.com/repos/rackerlabs/arsenal/pulls/30","id":35444802,"html_url":"https://github.com/rackerlabs/arsenal/pull/30","diff_url":"https://github.com/rackerlabs/arsenal/pull/30.diff","patch_url":"https://github.com/rackerlabs/arsenal/pull/30.patch","issue_url":"https://api.github.com/repos/rackerlabs/arsenal/issues/30","number":30,"state":"open","locked":false,"title":"Add cache directive rate limiting","user":{"login":"ClifHouck","id":1920475,"avatar_url":"https://avatars.githubusercontent.com/u/1920475?v=3","gravatar_id":"","url":"https://api.github.com/users/ClifHouck","html_url":"https://github.com/ClifHouck","followers_url":"https://api.github.com/users/ClifHouck/followers","following_url":"https://api.github.com/users/ClifHouck/following{/other_user}","gists_url":"https://api.github.com/users/ClifHouck/gists{/gist_id}","starred_url":"https://api.github.com/users/ClifHouck/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ClifHouck/subscriptions","organizations_url":"https://api.github.com/users/ClifHouck/orgs","repos_url":"https://api.github.com/users/ClifHouck/repos","events_url":"https://api.github.com/users/ClifHouck/events{/privacy}","received_events_url":"https://api.github.com/users/ClifHouck/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-05-14T17:55:51Z","updated_at":"2015-05-18T20:17:20Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a93766dc850646c2019d123d114c3019e4f1df93","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/rackerlabs/arsenal/pulls/30/commits","review_comments_url":"https://api.github.com/repos/rackerlabs/arsenal/pulls/30/comments","review_comment_url":"https://api.github.com/repos/rackerlabs/arsenal/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rackerlabs/arsenal/issues/30/comments","statuses_url":"https://api.github.com/repos/rackerlabs/arsenal/statuses/ea0d2dd198415d2ba3feea1693255fe39a92bc92","head":{"label":"rackerlabs:add_cache_directive_rate_limiting","ref":"add_cache_directive_rate_limiting","sha":"ea0d2dd198415d2ba3feea1693255fe39a92bc92","user":{"login":"rackerlabs","id":84293,"avatar_url":"https://avatars.githubusercontent.com/u/84293?v=3","gravatar_id":"","url":"https://api.github.com/users/rackerlabs","html_url":"https://github.com/rackerlabs","followers_url":"https://api.github.com/users/rackerlabs/followers","following_url":"https://api.github.com/users/rackerlabs/following{/other_user}","gists_url":"https://api.github.com/users/rackerlabs/gists{/gist_id}","starred_url":"https://api.github.com/users/rackerlabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rackerlabs/subscriptions","organizations_url":"https://api.github.com/users/rackerlabs/orgs","repos_url":"https://api.github.com/users/rackerlabs/repos","events_url":"https://api.github.com/users/rackerlabs/events{/privacy}","received_events_url":"https://api.github.com/users/rackerlabs/received_events","type":"Organization","site_admin":false},"repo":{"id":29627379,"name":"arsenal","full_name":"rackerlabs/arsenal","owner":{"login":"rackerlabs","id":84293,"avatar_url":"https://avatars.githubusercontent.com/u/84293?v=3","gravatar_id":"","url":"https://api.github.com/users/rackerlabs","html_url":"https://github.com/rackerlabs","followers_url":"https://api.github.com/users/rackerlabs/followers","following_url":"https://api.github.com/users/rackerlabs/following{/other_user}","gists_url":"https://api.github.com/users/rackerlabs/gists{/gist_id}","starred_url":"https://api.github.com/users/rackerlabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rackerlabs/subscriptions","organizations_url":"https://api.github.com/users/rackerlabs/orgs","repos_url":"https://api.github.com/users/rackerlabs/repos","events_url":"https://api.github.com/users/rackerlabs/events{/privacy}","received_events_url":"https://api.github.com/users/rackerlabs/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rackerlabs/arsenal","description":"","fork":false,"url":"https://api.github.com/repos/rackerlabs/arsenal","forks_url":"https://api.github.com/repos/rackerlabs/arsenal/forks","keys_url":"https://api.github.com/repos/rackerlabs/arsenal/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rackerlabs/arsenal/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rackerlabs/arsenal/teams","hooks_url":"https://api.github.com/repos/rackerlabs/arsenal/hooks","issue_events_url":"https://api.github.com/repos/rackerlabs/arsenal/issues/events{/number}","events_url":"https://api.github.com/repos/rackerlabs/arsenal/events","assignees_url":"https://api.github.com/repos/rackerlabs/arsenal/assignees{/user}","branches_url":"https://api.github.com/repos/rackerlabs/arsenal/branches{/branch}","tags_url":"https://api.github.com/repos/rackerlabs/arsenal/tags","blobs_url":"https://api.github.com/repos/rackerlabs/arsenal/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rackerlabs/arsenal/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rackerlabs/arsenal/git/refs{/sha}","trees_url":"https://api.github.com/repos/rackerlabs/arsenal/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rackerlabs/arsenal/statuses/{sha}","languages_url":"https://api.github.com/repos/rackerlabs/arsenal/languages","stargazers_url":"https://api.github.com/repos/rackerlabs/arsenal/stargazers","contributors_url":"https://api.github.com/repos/rackerlabs/arsenal/contributors","subscribers_url":"https://api.github.com/repos/rackerlabs/arsenal/subscribers","subscription_url":"https://api.github.com/repos/rackerlabs/arsenal/subscription","commits_url":"https://api.github.com/repos/rackerlabs/arsenal/commits{/sha}","git_commits_url":"https://api.github.com/repos/rackerlabs/arsenal/git/commits{/sha}","comments_url":"https://api.github.com/repos/rackerlabs/arsenal/comments{/number}","issue_comment_url":"https://api.github.com/repos/rackerlabs/arsenal/issues/comments{/number}","contents_url":"https://api.github.com/repos/rackerlabs/arsenal/contents/{+path}","compare_url":"https://api.github.com/repos/rackerlabs/arsenal/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rackerlabs/arsenal/merges","archive_url":"https://api.github.com/repos/rackerlabs/arsenal/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rackerlabs/arsenal/downloads","issues_url":"https://api.github.com/repos/rackerlabs/arsenal/issues{/number}","pulls_url":"https://api.github.com/repos/rackerlabs/arsenal/pulls{/number}","milestones_url":"https://api.github.com/repos/rackerlabs/arsenal/milestones{/number}","notifications_url":"https://api.github.com/repos/rackerlabs/arsenal/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rackerlabs/arsenal/labels{/name}","releases_url":"https://api.github.com/repos/rackerlabs/arsenal/releases{/id}","created_at":"2015-01-21T23:22:13Z","updated_at":"2015-05-07T20:48:23Z","pushed_at":"2015-05-18T18:31:27Z","git_url":"git://github.com/rackerlabs/arsenal.git","ssh_url":"git@github.com:rackerlabs/arsenal.git","clone_url":"https://github.com/rackerlabs/arsenal.git","svn_url":"https://github.com/rackerlabs/arsenal","homepage":null,"size":776,"stargazers_count":3,"watchers_count":3,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":8,"forks":0,"open_issues":8,"watchers":3,"default_branch":"master"}},"base":{"label":"rackerlabs:master","ref":"master","sha":"b2e608e16972bf6d6107ce5b646f250664970468","user":{"login":"rackerlabs","id":84293,"avatar_url":"https://avatars.githubusercontent.com/u/84293?v=3","gravatar_id":"","url":"https://api.github.com/users/rackerlabs","html_url":"https://github.com/rackerlabs","followers_url":"https://api.github.com/users/rackerlabs/followers","following_url":"https://api.github.com/users/rackerlabs/following{/other_user}","gists_url":"https://api.github.com/users/rackerlabs/gists{/gist_id}","starred_url":"https://api.github.com/users/rackerlabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rackerlabs/subscriptions","organizations_url":"https://api.github.com/users/rackerlabs/orgs","repos_url":"https://api.github.com/users/rackerlabs/repos","events_url":"https://api.github.com/users/rackerlabs/events{/privacy}","received_events_url":"https://api.github.com/users/rackerlabs/received_events","type":"Organization","site_admin":false},"repo":{"id":29627379,"name":"arsenal","full_name":"rackerlabs/arsenal","owner":{"login":"rackerlabs","id":84293,"avatar_url":"https://avatars.githubusercontent.com/u/84293?v=3","gravatar_id":"","url":"https://api.github.com/users/rackerlabs","html_url":"https://github.com/rackerlabs","followers_url":"https://api.github.com/users/rackerlabs/followers","following_url":"https://api.github.com/users/rackerlabs/following{/other_user}","gists_url":"https://api.github.com/users/rackerlabs/gists{/gist_id}","starred_url":"https://api.github.com/users/rackerlabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rackerlabs/subscriptions","organizations_url":"https://api.github.com/users/rackerlabs/orgs","repos_url":"https://api.github.com/users/rackerlabs/repos","events_url":"https://api.github.com/users/rackerlabs/events{/privacy}","received_events_url":"https://api.github.com/users/rackerlabs/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rackerlabs/arsenal","description":"","fork":false,"url":"https://api.github.com/repos/rackerlabs/arsenal","forks_url":"https://api.github.com/repos/rackerlabs/arsenal/forks","keys_url":"https://api.github.com/repos/rackerlabs/arsenal/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rackerlabs/arsenal/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rackerlabs/arsenal/teams","hooks_url":"https://api.github.com/repos/rackerlabs/arsenal/hooks","issue_events_url":"https://api.github.com/repos/rackerlabs/arsenal/issues/events{/number}","events_url":"https://api.github.com/repos/rackerlabs/arsenal/events","assignees_url":"https://api.github.com/repos/rackerlabs/arsenal/assignees{/user}","branches_url":"https://api.github.com/repos/rackerlabs/arsenal/branches{/branch}","tags_url":"https://api.github.com/repos/rackerlabs/arsenal/tags","blobs_url":"https://api.github.com/repos/rackerlabs/arsenal/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rackerlabs/arsenal/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rackerlabs/arsenal/git/refs{/sha}","trees_url":"https://api.github.com/repos/rackerlabs/arsenal/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rackerlabs/arsenal/statuses/{sha}","languages_url":"https://api.github.com/repos/rackerlabs/arsenal/languages","stargazers_url":"https://api.github.com/repos/rackerlabs/arsenal/stargazers","contributors_url":"https://api.github.com/repos/rackerlabs/arsenal/contributors","subscribers_url":"https://api.github.com/repos/rackerlabs/arsenal/subscribers","subscription_url":"https://api.github.com/repos/rackerlabs/arsenal/subscription","commits_url":"https://api.github.com/repos/rackerlabs/arsenal/commits{/sha}","git_commits_url":"https://api.github.com/repos/rackerlabs/arsenal/git/commits{/sha}","comments_url":"https://api.github.com/repos/rackerlabs/arsenal/comments{/number}","issue_comment_url":"https://api.github.com/repos/rackerlabs/arsenal/issues/comments{/number}","contents_url":"https://api.github.com/repos/rackerlabs/arsenal/contents/{+path}","compare_url":"https://api.github.com/repos/rackerlabs/arsenal/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rackerlabs/arsenal/merges","archive_url":"https://api.github.com/repos/rackerlabs/arsenal/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rackerlabs/arsenal/downloads","issues_url":"https://api.github.com/repos/rackerlabs/arsenal/issues{/number}","pulls_url":"https://api.github.com/repos/rackerlabs/arsenal/pulls{/number}","milestones_url":"https://api.github.com/repos/rackerlabs/arsenal/milestones{/number}","notifications_url":"https://api.github.com/repos/rackerlabs/arsenal/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rackerlabs/arsenal/labels{/name}","releases_url":"https://api.github.com/repos/rackerlabs/arsenal/releases{/id}","created_at":"2015-01-21T23:22:13Z","updated_at":"2015-05-07T20:48:23Z","pushed_at":"2015-05-18T18:31:27Z","git_url":"git://github.com/rackerlabs/arsenal.git","ssh_url":"git@github.com:rackerlabs/arsenal.git","clone_url":"https://github.com/rackerlabs/arsenal.git","svn_url":"https://github.com/rackerlabs/arsenal","homepage":null,"size":776,"stargazers_count":3,"watchers_count":3,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":8,"forks":0,"open_issues":8,"watchers":3,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rackerlabs/arsenal/pulls/30"},"html":{"href":"https://github.com/rackerlabs/arsenal/pull/30"},"issue":{"href":"https://api.github.com/repos/rackerlabs/arsenal/issues/30"},"comments":{"href":"https://api.github.com/repos/rackerlabs/arsenal/issues/30/comments"},"review_comments":{"href":"https://api.github.com/repos/rackerlabs/arsenal/pulls/30/comments"},"review_comment":{"href":"https://api.github.com/repos/rackerlabs/arsenal/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rackerlabs/arsenal/pulls/30/commits"},"statuses":{"href":"https://api.github.com/repos/rackerlabs/arsenal/statuses/ea0d2dd198415d2ba3feea1693255fe39a92bc92"}}}}