{"action":"created","comment":{"url":"https://api.github.com/repos/metabrainz/listenbrainz-server/pulls/comments/42881897","id":42881897,"diff_hunk":"@@ -174,28 +174,47 @@ function reportScrobbles(struct) {\n     xhr.setRequestHeader(\"Content-Type\", \"application/json;charset=UTF-8\");\n     xhr.timeout = 10 * 1000; // 10 seconds\n     xhr.onload = function(content) {\n-        if (this.status >= 200 && this.status < 300) {\n-            console.log(\"successfully reported page\");\n-            pageDone();\n-        } else if (this.status >= 400 && this.status < 500) {\n-            times4Error++;\n-            // We mark 4xx errors as completed because we don't\n-            // retry them\n-            console.log(\"4xx error, skipping\");\n-            pageDone();\n-        } else if (this.status >= 500) {\n-            console.log(\"received http error \" + this.status + \" req'ing\");\n-            times5Error++;\n-            reportScrobbles(struct);\n-        } else {\n-            console.log(\"received http status \" + this.status + \", skipping\");\n-            pageDone();\n-        }\n-        if (numCompleted >= numberOfPages) {\n-            updateMessage(\"<i class='fa fa-check'></i> Import finished<br><span style='font-size:8pt'>Thank you for using ListenBrainz</span>\");\n-        } else {\n-            updateMessage(\"<i class='fa fa-cog fa-spin'></i> Sending page \" + numCompleted + \" of \" + numberOfPages + \" to ListenBrainz<br><span style='font-size:8pt'>Please don't navigate while this is running</span>\");\n-        }\n+        remain = parseInt(xhr.getResponseHeader(\"X-RateLimit-Remaining\"));\n+        reset = parseInt(xhr.getResponseHeader(\"X-RateLimit-Reset\"));\n+        current = new Date().getTime() / 1000;\n+\n+        if (remain)\n+            rate_limit_delay = Math.max(0, Math.floor((reset - current) * 1000 / remain));\n+        else\n+            rate_limit_delay = Math.max(0, (reset - current) * 1000);","path":"webserver/templates/user/scraper.js","position":42,"original_position":42,"commit_id":"a83b4de0701b369557c88c8a6a4fd853cf30a408","original_commit_id":"a83b4de0701b369557c88c8a6a4fd853cf30a408","user":{"login":"gentlecat","id":460525,"avatar_url":"https://avatars.githubusercontent.com/u/460525?v=3","gravatar_id":"","url":"https://api.github.com/users/gentlecat","html_url":"https://github.com/gentlecat","followers_url":"https://api.github.com/users/gentlecat/followers","following_url":"https://api.github.com/users/gentlecat/following{/other_user}","gists_url":"https://api.github.com/users/gentlecat/gists{/gist_id}","starred_url":"https://api.github.com/users/gentlecat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gentlecat/subscriptions","organizations_url":"https://api.github.com/users/gentlecat/orgs","repos_url":"https://api.github.com/users/gentlecat/repos","events_url":"https://api.github.com/users/gentlecat/events{/privacy}","received_events_url":"https://api.github.com/users/gentlecat/received_events","type":"User","site_admin":false},"body":"```javascript\r\nif (remain) {\r\n    rate_limit_delay = Math.max(0, Math.floor((reset - current) * 1000 / remain));\r\n} else {\r\n    rate_limit_delay = Math.max(0, (reset - current) * 1000);\r\n}\r\n```","created_at":"2015-10-23T15:57:42Z","updated_at":"2015-10-23T15:57:42Z","html_url":"https://github.com/metabrainz/listenbrainz-server/pull/65#discussion_r42881897","pull_request_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/pulls/65","_links":{"self":{"href":"https://api.github.com/repos/metabrainz/listenbrainz-server/pulls/comments/42881897"},"html":{"href":"https://github.com/metabrainz/listenbrainz-server/pull/65#discussion_r42881897"},"pull_request":{"href":"https://api.github.com/repos/metabrainz/listenbrainz-server/pulls/65"}}},"pull_request":{"url":"https://api.github.com/repos/metabrainz/listenbrainz-server/pulls/65","id":48351046,"html_url":"https://github.com/metabrainz/listenbrainz-server/pull/65","diff_url":"https://github.com/metabrainz/listenbrainz-server/pull/65.diff","patch_url":"https://github.com/metabrainz/listenbrainz-server/pull/65.patch","issue_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/issues/65","number":65,"state":"open","locked":false,"title":"[WIP] [KTHA] rate limiting amidointhisrite?","user":{"login":"mayhem","id":49745,"avatar_url":"https://avatars.githubusercontent.com/u/49745?v=3","gravatar_id":"","url":"https://api.github.com/users/mayhem","html_url":"https://github.com/mayhem","followers_url":"https://api.github.com/users/mayhem/followers","following_url":"https://api.github.com/users/mayhem/following{/other_user}","gists_url":"https://api.github.com/users/mayhem/gists{/gist_id}","starred_url":"https://api.github.com/users/mayhem/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mayhem/subscriptions","organizations_url":"https://api.github.com/users/mayhem/orgs","repos_url":"https://api.github.com/users/mayhem/repos","events_url":"https://api.github.com/users/mayhem/events{/privacy}","received_events_url":"https://api.github.com/users/mayhem/received_events","type":"User","site_admin":false},"body":"First cut of the rate limiting stuff seems to be working. The general idea:\r\n\r\n1. Rate limiting is done on a per IP or per token basis. If the caller provides the Authorization header with their token, the user gets a better rate limit. If not, the user is rate limited based on IP.\r\n2. Rate limiting is done on a windowing basis, were X requests are allowed in a given window of time. This information is returned in the X-RateLimit-* headers.\r\n2.b. There is no 2.b\r\n3. Rate limits are dynamic and stored in Redis. They can be set with the set_rate_limits.py program. \r\n4. If you want to try this PR, install redis and python redis by hand for now.\r\n5. I've added a dummy endpoint for easy testing. Use with:\r\n\r\n  curl -D - --header \"Authorization: token [your token here]\" http://10.1.2.3:8080/1/rate-limit-test\r\n\r\n","created_at":"2015-10-21T15:55:42Z","updated_at":"2015-10-23T15:57:42Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/pulls/65/commits","review_comments_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/pulls/65/comments","review_comment_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/pulls/comments{/number}","comments_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/issues/65/comments","statuses_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/statuses/a83b4de0701b369557c88c8a6a4fd853cf30a408","head":{"label":"metabrainz:rate-limiting","ref":"rate-limiting","sha":"a83b4de0701b369557c88c8a6a4fd853cf30a408","user":{"login":"metabrainz","id":293421,"avatar_url":"https://avatars.githubusercontent.com/u/293421?v=3","gravatar_id":"","url":"https://api.github.com/users/metabrainz","html_url":"https://github.com/metabrainz","followers_url":"https://api.github.com/users/metabrainz/followers","following_url":"https://api.github.com/users/metabrainz/following{/other_user}","gists_url":"https://api.github.com/users/metabrainz/gists{/gist_id}","starred_url":"https://api.github.com/users/metabrainz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/metabrainz/subscriptions","organizations_url":"https://api.github.com/users/metabrainz/orgs","repos_url":"https://api.github.com/users/metabrainz/repos","events_url":"https://api.github.com/users/metabrainz/events{/privacy}","received_events_url":"https://api.github.com/users/metabrainz/received_events","type":"Organization","site_admin":false},"repo":{"id":41960013,"name":"listenbrainz-server","full_name":"metabrainz/listenbrainz-server","owner":{"login":"metabrainz","id":293421,"avatar_url":"https://avatars.githubusercontent.com/u/293421?v=3","gravatar_id":"","url":"https://api.github.com/users/metabrainz","html_url":"https://github.com/metabrainz","followers_url":"https://api.github.com/users/metabrainz/followers","following_url":"https://api.github.com/users/metabrainz/following{/other_user}","gists_url":"https://api.github.com/users/metabrainz/gists{/gist_id}","starred_url":"https://api.github.com/users/metabrainz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/metabrainz/subscriptions","organizations_url":"https://api.github.com/users/metabrainz/orgs","repos_url":"https://api.github.com/users/metabrainz/repos","events_url":"https://api.github.com/users/metabrainz/events{/privacy}","received_events_url":"https://api.github.com/users/metabrainz/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/metabrainz/listenbrainz-server","description":"Server for the ListenBrainz project","fork":false,"url":"https://api.github.com/repos/metabrainz/listenbrainz-server","forks_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/forks","keys_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/keys{/key_id}","collaborators_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/teams","hooks_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/hooks","issue_events_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/issues/events{/number}","events_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/events","assignees_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/assignees{/user}","branches_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/branches{/branch}","tags_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/tags","blobs_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/git/refs{/sha}","trees_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/git/trees{/sha}","statuses_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/statuses/{sha}","languages_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/languages","stargazers_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/stargazers","contributors_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/contributors","subscribers_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/subscribers","subscription_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/subscription","commits_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/commits{/sha}","git_commits_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/git/commits{/sha}","comments_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/comments{/number}","issue_comment_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/issues/comments{/number}","contents_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/contents/{+path}","compare_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/compare/{base}...{head}","merges_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/merges","archive_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/downloads","issues_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/issues{/number}","pulls_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/pulls{/number}","milestones_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/milestones{/number}","notifications_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/labels{/name}","releases_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/releases{/id}","created_at":"2015-09-05T12:30:49Z","updated_at":"2015-10-23T11:54:12Z","pushed_at":"2015-10-23T14:48:47Z","git_url":"git://github.com/metabrainz/listenbrainz-server.git","ssh_url":"git@github.com:metabrainz/listenbrainz-server.git","clone_url":"https://github.com/metabrainz/listenbrainz-server.git","svn_url":"https://github.com/metabrainz/listenbrainz-server","homepage":null,"size":2082,"stargazers_count":35,"watchers_count":35,"language":"CSS","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":6,"mirror_url":null,"open_issues_count":6,"forks":6,"open_issues":6,"watchers":35,"default_branch":"master"}},"base":{"label":"metabrainz:master","ref":"master","sha":"0c22def320811a69ea8b063990e09d7008207a6c","user":{"login":"metabrainz","id":293421,"avatar_url":"https://avatars.githubusercontent.com/u/293421?v=3","gravatar_id":"","url":"https://api.github.com/users/metabrainz","html_url":"https://github.com/metabrainz","followers_url":"https://api.github.com/users/metabrainz/followers","following_url":"https://api.github.com/users/metabrainz/following{/other_user}","gists_url":"https://api.github.com/users/metabrainz/gists{/gist_id}","starred_url":"https://api.github.com/users/metabrainz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/metabrainz/subscriptions","organizations_url":"https://api.github.com/users/metabrainz/orgs","repos_url":"https://api.github.com/users/metabrainz/repos","events_url":"https://api.github.com/users/metabrainz/events{/privacy}","received_events_url":"https://api.github.com/users/metabrainz/received_events","type":"Organization","site_admin":false},"repo":{"id":41960013,"name":"listenbrainz-server","full_name":"metabrainz/listenbrainz-server","owner":{"login":"metabrainz","id":293421,"avatar_url":"https://avatars.githubusercontent.com/u/293421?v=3","gravatar_id":"","url":"https://api.github.com/users/metabrainz","html_url":"https://github.com/metabrainz","followers_url":"https://api.github.com/users/metabrainz/followers","following_url":"https://api.github.com/users/metabrainz/following{/other_user}","gists_url":"https://api.github.com/users/metabrainz/gists{/gist_id}","starred_url":"https://api.github.com/users/metabrainz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/metabrainz/subscriptions","organizations_url":"https://api.github.com/users/metabrainz/orgs","repos_url":"https://api.github.com/users/metabrainz/repos","events_url":"https://api.github.com/users/metabrainz/events{/privacy}","received_events_url":"https://api.github.com/users/metabrainz/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/metabrainz/listenbrainz-server","description":"Server for the ListenBrainz project","fork":false,"url":"https://api.github.com/repos/metabrainz/listenbrainz-server","forks_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/forks","keys_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/keys{/key_id}","collaborators_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/teams","hooks_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/hooks","issue_events_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/issues/events{/number}","events_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/events","assignees_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/assignees{/user}","branches_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/branches{/branch}","tags_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/tags","blobs_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/git/refs{/sha}","trees_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/git/trees{/sha}","statuses_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/statuses/{sha}","languages_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/languages","stargazers_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/stargazers","contributors_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/contributors","subscribers_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/subscribers","subscription_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/subscription","commits_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/commits{/sha}","git_commits_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/git/commits{/sha}","comments_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/comments{/number}","issue_comment_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/issues/comments{/number}","contents_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/contents/{+path}","compare_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/compare/{base}...{head}","merges_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/merges","archive_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/downloads","issues_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/issues{/number}","pulls_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/pulls{/number}","milestones_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/milestones{/number}","notifications_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/labels{/name}","releases_url":"https://api.github.com/repos/metabrainz/listenbrainz-server/releases{/id}","created_at":"2015-09-05T12:30:49Z","updated_at":"2015-10-23T11:54:12Z","pushed_at":"2015-10-23T14:48:47Z","git_url":"git://github.com/metabrainz/listenbrainz-server.git","ssh_url":"git@github.com:metabrainz/listenbrainz-server.git","clone_url":"https://github.com/metabrainz/listenbrainz-server.git","svn_url":"https://github.com/metabrainz/listenbrainz-server","homepage":null,"size":2082,"stargazers_count":35,"watchers_count":35,"language":"CSS","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":6,"mirror_url":null,"open_issues_count":6,"forks":6,"open_issues":6,"watchers":35,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/metabrainz/listenbrainz-server/pulls/65"},"html":{"href":"https://github.com/metabrainz/listenbrainz-server/pull/65"},"issue":{"href":"https://api.github.com/repos/metabrainz/listenbrainz-server/issues/65"},"comments":{"href":"https://api.github.com/repos/metabrainz/listenbrainz-server/issues/65/comments"},"review_comments":{"href":"https://api.github.com/repos/metabrainz/listenbrainz-server/pulls/65/comments"},"review_comment":{"href":"https://api.github.com/repos/metabrainz/listenbrainz-server/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/metabrainz/listenbrainz-server/pulls/65/commits"},"statuses":{"href":"https://api.github.com/repos/metabrainz/listenbrainz-server/statuses/a83b4de0701b369557c88c8a6a4fd853cf30a408"}}}}