{"action":"created","comment":{"url":"https://api.github.com/repos/treasure-data/td-client-java/pulls/comments/85316638","pull_request_review_id":6038598,"id":85316638,"diff_hunk":"@@ -78,4 +93,127 @@ public void deleteMethodTest()\n             logger.warn(\"error\", e);\n         }\n     }\n+\n+    @Test\n+    public void retryOn429()\n+            throws Exception\n+    {\n+        // Configure an artificially low retry interval so we can measure with some confidence that Retry-After is respected\n+        client = TDClient.newBuilder()\n+                .setRetryMaxIntervalMillis(100)\n+                .setRetryLimit(1000)\n+                .build()\n+                .httpClient;\n+\n+        final AtomicLong firstRequestNanos = new AtomicLong();\n+        final AtomicLong secondRequestNanos = new AtomicLong();\n+        final AtomicInteger requests = new AtomicInteger();\n+\n+        final TDApiRequest req = TDApiRequest.Builder.GET(\"/v3/system/server_status\").build();\n+        final byte[] body = \"foobar\".getBytes(\"UTF-8\");\n+        final long retryAfterSeconds = 5;\n+\n+        ContentResponse resp = client.submitRequest(req, Optional.<String>absent(), new TDHttpClient.DefaultContentHandler()\n+        {\n+            @Override\n+            public ContentResponse submit(Request request)\n+                    throws InterruptedException, ExecutionException, TimeoutException\n+            {\n+                List<Response.ResponseListener> listeners = ImmutableList.of();\n+                switch (requests.incrementAndGet()) {\n+                    case 1: {\n+                        firstRequestNanos.set(System.nanoTime());\n+                        HttpResponse response = new HttpResponse(request, listeners)\n+                                .status(429);\n+                        response.getHeaders().add(\"Retry-After\", Long.toString(retryAfterSeconds));\n+                        return new HttpContentResponse(response, new byte[] {}, \"\", \"\");\n+                    }\n+                    case 2: {\n+                        secondRequestNanos.set(System.nanoTime());\n+                        HttpResponse response = new HttpResponse(request, listeners)\n+                                .status(200);\n+                        return new HttpContentResponse(response, body, \"plain/text\", \"UTF-8\");\n+                    }\n+                    default:\n+                        throw new AssertionError();\n+                }\n+            }\n+        });\n+\n+        assertThat(requests.get(), is(2));\n+        assertThat(resp.getStatus(), is(200));\n+        assertThat(resp.getContent(), is(body));\n+\n+        long delayNanos = secondRequestNanos.get() - firstRequestNanos.get();\n+        assertThat(delayNanos, Matchers.greaterThanOrEqualTo(SECONDS.toNanos(retryAfterSeconds)));\n+    }\n+\n+    @Test\n+    public void failOn429_TimeLimitExceeded()\n+            throws Exception\n+    {\n+        client = TDClient.newBuilder()\n+                .setRetryMaxIntervalMillis(1000)\n+                .setRetryLimit(3)\n+                .build()\n+                .httpClient;\n+\n+        final long retryAfterSeconds = 4711;\n+        final AtomicInteger requests = new AtomicInteger();\n+\n+        failWith429(retryAfterSeconds, requests);\n+\n+        // Verify that only one attempt was made\n+        assertThat(requests.get(), is(1));\n+    }\n+\n+    @Test\n+    public void failOn429_RetryLimitExceeded()\n+            throws Exception\n+    {\n+        client = TDClient.newBuilder()\n+                .setRetryMaxIntervalMillis(Integer.MAX_VALUE)\n+                .setRetryLimit(3)\n+                .build()\n+                .httpClient;\n+\n+        final long retryAfterSeconds = 1;\n+        final AtomicInteger requests = new AtomicInteger();\n+\n+        failWith429(retryAfterSeconds, requests);\n+\n+        // Verify that 4 attempts were made (original request + three retries)\n+        assertThat(requests.get(), is(4));\n+    }\n+\n+    private void failWith429(final long retryAfterSeconds, final AtomicInteger requests)\n+    {\n+        final TDApiRequest req = TDApiRequest.Builder.GET(\"/v3/system/server_status\").build();\n+\n+        try {\n+            ContentResponse resp = client.submitRequest(req, Optional.<String>absent(), new TDHttpClient.DefaultContentHandler()\n+            {\n+                @Override\n+                public ContentResponse submit(Request request)\n+                        throws InterruptedException, ExecutionException, TimeoutException\n+                {\n+                    requests.incrementAndGet();\n+                    List<Response.ResponseListener> listeners = ImmutableList.of();\n+                    HttpResponse response = new HttpResponse(request, listeners)\n+                            .status(429);\n+                    response.getHeaders().add(\"Retry-After\", Long.toString(retryAfterSeconds));","path":"src/test/java/com/treasuredata/client/TestTDHttpClient.java","position":145,"original_position":145,"commit_id":"35c727f4cf4b7293eea7e750b283f7bb393ebcb4","original_commit_id":"35c727f4cf4b7293eea7e750b283f7bb393ebcb4","user":{"login":"komamitsu","id":59043,"avatar_url":"https://avatars.githubusercontent.com/u/59043?v=3","gravatar_id":"","url":"https://api.github.com/users/komamitsu","html_url":"https://github.com/komamitsu","followers_url":"https://api.github.com/users/komamitsu/followers","following_url":"https://api.github.com/users/komamitsu/following{/other_user}","gists_url":"https://api.github.com/users/komamitsu/gists{/gist_id}","starred_url":"https://api.github.com/users/komamitsu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/komamitsu/subscriptions","organizations_url":"https://api.github.com/users/komamitsu/orgs","repos_url":"https://api.github.com/users/komamitsu/repos","events_url":"https://api.github.com/users/komamitsu/events{/privacy}","received_events_url":"https://api.github.com/users/komamitsu/received_events","type":"User","site_admin":false},"body":"How about adding a test that doesn't add `Retry-After` header to status code 429?","created_at":"2016-10-27T11:21:51Z","updated_at":"2016-10-27T11:21:51Z","html_url":"https://github.com/treasure-data/td-client-java/pull/75#discussion_r85316638","pull_request_url":"https://api.github.com/repos/treasure-data/td-client-java/pulls/75","_links":{"self":{"href":"https://api.github.com/repos/treasure-data/td-client-java/pulls/comments/85316638"},"html":{"href":"https://github.com/treasure-data/td-client-java/pull/75#discussion_r85316638"},"pull_request":{"href":"https://api.github.com/repos/treasure-data/td-client-java/pulls/75"}}},"pull_request":{"url":"https://api.github.com/repos/treasure-data/td-client-java/pulls/75","id":91136267,"html_url":"https://github.com/treasure-data/td-client-java/pull/75","diff_url":"https://github.com/treasure-data/td-client-java/pull/75.diff","patch_url":"https://github.com/treasure-data/td-client-java/pull/75.patch","issue_url":"https://api.github.com/repos/treasure-data/td-client-java/issues/75","number":75,"state":"open","locked":false,"title":"Handle 429 Too Many Requests","user":{"login":"danielnorberg","id":393289,"avatar_url":"https://avatars.githubusercontent.com/u/393289?v=3","gravatar_id":"","url":"https://api.github.com/users/danielnorberg","html_url":"https://github.com/danielnorberg","followers_url":"https://api.github.com/users/danielnorberg/followers","following_url":"https://api.github.com/users/danielnorberg/following{/other_user}","gists_url":"https://api.github.com/users/danielnorberg/gists{/gist_id}","starred_url":"https://api.github.com/users/danielnorberg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danielnorberg/subscriptions","organizations_url":"https://api.github.com/users/danielnorberg/orgs","repos_url":"https://api.github.com/users/danielnorberg/repos","events_url":"https://api.github.com/users/danielnorberg/events{/privacy}","received_events_url":"https://api.github.com/users/danielnorberg/received_events","type":"User","site_admin":false},"body":"The TD API returns 429 with a `Retry-After` header when rate limiting requests.\r\n\r\nRespect the `Retry-After` value when retrying and also propagate the the value to the user in a new `TDClientHttpTooManyRequestsException` when the configured retry has been exceeded.","created_at":"2016-10-27T05:41:12Z","updated_at":"2016-10-27T11:21:51Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4232f2b03a853b57ebff0ac915a3735f05db1d32","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/treasure-data/td-client-java/pulls/75/commits","review_comments_url":"https://api.github.com/repos/treasure-data/td-client-java/pulls/75/comments","review_comment_url":"https://api.github.com/repos/treasure-data/td-client-java/pulls/comments{/number}","comments_url":"https://api.github.com/repos/treasure-data/td-client-java/issues/75/comments","statuses_url":"https://api.github.com/repos/treasure-data/td-client-java/statuses/35c727f4cf4b7293eea7e750b283f7bb393ebcb4","head":{"label":"treasure-data:dano/handle-429","ref":"dano/handle-429","sha":"35c727f4cf4b7293eea7e750b283f7bb393ebcb4","user":{"login":"treasure-data","id":747746,"avatar_url":"https://avatars.githubusercontent.com/u/747746?v=3","gravatar_id":"","url":"https://api.github.com/users/treasure-data","html_url":"https://github.com/treasure-data","followers_url":"https://api.github.com/users/treasure-data/followers","following_url":"https://api.github.com/users/treasure-data/following{/other_user}","gists_url":"https://api.github.com/users/treasure-data/gists{/gist_id}","starred_url":"https://api.github.com/users/treasure-data/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/treasure-data/subscriptions","organizations_url":"https://api.github.com/users/treasure-data/orgs","repos_url":"https://api.github.com/users/treasure-data/repos","events_url":"https://api.github.com/users/treasure-data/events{/privacy}","received_events_url":"https://api.github.com/users/treasure-data/received_events","type":"Organization","site_admin":false},"repo":{"id":3625270,"name":"td-client-java","full_name":"treasure-data/td-client-java","owner":{"login":"treasure-data","id":747746,"avatar_url":"https://avatars.githubusercontent.com/u/747746?v=3","gravatar_id":"","url":"https://api.github.com/users/treasure-data","html_url":"https://github.com/treasure-data","followers_url":"https://api.github.com/users/treasure-data/followers","following_url":"https://api.github.com/users/treasure-data/following{/other_user}","gists_url":"https://api.github.com/users/treasure-data/gists{/gist_id}","starred_url":"https://api.github.com/users/treasure-data/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/treasure-data/subscriptions","organizations_url":"https://api.github.com/users/treasure-data/orgs","repos_url":"https://api.github.com/users/treasure-data/repos","events_url":"https://api.github.com/users/treasure-data/events{/privacy}","received_events_url":"https://api.github.com/users/treasure-data/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/treasure-data/td-client-java","description":"Java Client Library for Treasure Data","fork":false,"url":"https://api.github.com/repos/treasure-data/td-client-java","forks_url":"https://api.github.com/repos/treasure-data/td-client-java/forks","keys_url":"https://api.github.com/repos/treasure-data/td-client-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/treasure-data/td-client-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/treasure-data/td-client-java/teams","hooks_url":"https://api.github.com/repos/treasure-data/td-client-java/hooks","issue_events_url":"https://api.github.com/repos/treasure-data/td-client-java/issues/events{/number}","events_url":"https://api.github.com/repos/treasure-data/td-client-java/events","assignees_url":"https://api.github.com/repos/treasure-data/td-client-java/assignees{/user}","branches_url":"https://api.github.com/repos/treasure-data/td-client-java/branches{/branch}","tags_url":"https://api.github.com/repos/treasure-data/td-client-java/tags","blobs_url":"https://api.github.com/repos/treasure-data/td-client-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/treasure-data/td-client-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/treasure-data/td-client-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/treasure-data/td-client-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/treasure-data/td-client-java/statuses/{sha}","languages_url":"https://api.github.com/repos/treasure-data/td-client-java/languages","stargazers_url":"https://api.github.com/repos/treasure-data/td-client-java/stargazers","contributors_url":"https://api.github.com/repos/treasure-data/td-client-java/contributors","subscribers_url":"https://api.github.com/repos/treasure-data/td-client-java/subscribers","subscription_url":"https://api.github.com/repos/treasure-data/td-client-java/subscription","commits_url":"https://api.github.com/repos/treasure-data/td-client-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/treasure-data/td-client-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/treasure-data/td-client-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/treasure-data/td-client-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/treasure-data/td-client-java/contents/{+path}","compare_url":"https://api.github.com/repos/treasure-data/td-client-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/treasure-data/td-client-java/merges","archive_url":"https://api.github.com/repos/treasure-data/td-client-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/treasure-data/td-client-java/downloads","issues_url":"https://api.github.com/repos/treasure-data/td-client-java/issues{/number}","pulls_url":"https://api.github.com/repos/treasure-data/td-client-java/pulls{/number}","milestones_url":"https://api.github.com/repos/treasure-data/td-client-java/milestones{/number}","notifications_url":"https://api.github.com/repos/treasure-data/td-client-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/treasure-data/td-client-java/labels{/name}","releases_url":"https://api.github.com/repos/treasure-data/td-client-java/releases{/id}","deployments_url":"https://api.github.com/repos/treasure-data/td-client-java/deployments","created_at":"2012-03-05T08:45:22Z","updated_at":"2016-08-24T20:00:46Z","pushed_at":"2016-10-27T05:55:43Z","git_url":"git://github.com/treasure-data/td-client-java.git","ssh_url":"git@github.com:treasure-data/td-client-java.git","clone_url":"https://github.com/treasure-data/td-client-java.git","svn_url":"https://github.com/treasure-data/td-client-java","homepage":"http://docs.treasuredata.com/articles/java","size":1486,"stargazers_count":6,"watchers_count":6,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":10,"mirror_url":null,"open_issues_count":3,"forks":10,"open_issues":3,"watchers":6,"default_branch":"master"}},"base":{"label":"treasure-data:master","ref":"master","sha":"87c4dbf6033cdbe70f448a388a4f0204cf73653b","user":{"login":"treasure-data","id":747746,"avatar_url":"https://avatars.githubusercontent.com/u/747746?v=3","gravatar_id":"","url":"https://api.github.com/users/treasure-data","html_url":"https://github.com/treasure-data","followers_url":"https://api.github.com/users/treasure-data/followers","following_url":"https://api.github.com/users/treasure-data/following{/other_user}","gists_url":"https://api.github.com/users/treasure-data/gists{/gist_id}","starred_url":"https://api.github.com/users/treasure-data/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/treasure-data/subscriptions","organizations_url":"https://api.github.com/users/treasure-data/orgs","repos_url":"https://api.github.com/users/treasure-data/repos","events_url":"https://api.github.com/users/treasure-data/events{/privacy}","received_events_url":"https://api.github.com/users/treasure-data/received_events","type":"Organization","site_admin":false},"repo":{"id":3625270,"name":"td-client-java","full_name":"treasure-data/td-client-java","owner":{"login":"treasure-data","id":747746,"avatar_url":"https://avatars.githubusercontent.com/u/747746?v=3","gravatar_id":"","url":"https://api.github.com/users/treasure-data","html_url":"https://github.com/treasure-data","followers_url":"https://api.github.com/users/treasure-data/followers","following_url":"https://api.github.com/users/treasure-data/following{/other_user}","gists_url":"https://api.github.com/users/treasure-data/gists{/gist_id}","starred_url":"https://api.github.com/users/treasure-data/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/treasure-data/subscriptions","organizations_url":"https://api.github.com/users/treasure-data/orgs","repos_url":"https://api.github.com/users/treasure-data/repos","events_url":"https://api.github.com/users/treasure-data/events{/privacy}","received_events_url":"https://api.github.com/users/treasure-data/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/treasure-data/td-client-java","description":"Java Client Library for Treasure Data","fork":false,"url":"https://api.github.com/repos/treasure-data/td-client-java","forks_url":"https://api.github.com/repos/treasure-data/td-client-java/forks","keys_url":"https://api.github.com/repos/treasure-data/td-client-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/treasure-data/td-client-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/treasure-data/td-client-java/teams","hooks_url":"https://api.github.com/repos/treasure-data/td-client-java/hooks","issue_events_url":"https://api.github.com/repos/treasure-data/td-client-java/issues/events{/number}","events_url":"https://api.github.com/repos/treasure-data/td-client-java/events","assignees_url":"https://api.github.com/repos/treasure-data/td-client-java/assignees{/user}","branches_url":"https://api.github.com/repos/treasure-data/td-client-java/branches{/branch}","tags_url":"https://api.github.com/repos/treasure-data/td-client-java/tags","blobs_url":"https://api.github.com/repos/treasure-data/td-client-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/treasure-data/td-client-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/treasure-data/td-client-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/treasure-data/td-client-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/treasure-data/td-client-java/statuses/{sha}","languages_url":"https://api.github.com/repos/treasure-data/td-client-java/languages","stargazers_url":"https://api.github.com/repos/treasure-data/td-client-java/stargazers","contributors_url":"https://api.github.com/repos/treasure-data/td-client-java/contributors","subscribers_url":"https://api.github.com/repos/treasure-data/td-client-java/subscribers","subscription_url":"https://api.github.com/repos/treasure-data/td-client-java/subscription","commits_url":"https://api.github.com/repos/treasure-data/td-client-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/treasure-data/td-client-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/treasure-data/td-client-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/treasure-data/td-client-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/treasure-data/td-client-java/contents/{+path}","compare_url":"https://api.github.com/repos/treasure-data/td-client-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/treasure-data/td-client-java/merges","archive_url":"https://api.github.com/repos/treasure-data/td-client-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/treasure-data/td-client-java/downloads","issues_url":"https://api.github.com/repos/treasure-data/td-client-java/issues{/number}","pulls_url":"https://api.github.com/repos/treasure-data/td-client-java/pulls{/number}","milestones_url":"https://api.github.com/repos/treasure-data/td-client-java/milestones{/number}","notifications_url":"https://api.github.com/repos/treasure-data/td-client-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/treasure-data/td-client-java/labels{/name}","releases_url":"https://api.github.com/repos/treasure-data/td-client-java/releases{/id}","deployments_url":"https://api.github.com/repos/treasure-data/td-client-java/deployments","created_at":"2012-03-05T08:45:22Z","updated_at":"2016-08-24T20:00:46Z","pushed_at":"2016-10-27T05:55:43Z","git_url":"git://github.com/treasure-data/td-client-java.git","ssh_url":"git@github.com:treasure-data/td-client-java.git","clone_url":"https://github.com/treasure-data/td-client-java.git","svn_url":"https://github.com/treasure-data/td-client-java","homepage":"http://docs.treasuredata.com/articles/java","size":1486,"stargazers_count":6,"watchers_count":6,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":10,"mirror_url":null,"open_issues_count":3,"forks":10,"open_issues":3,"watchers":6,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/treasure-data/td-client-java/pulls/75"},"html":{"href":"https://github.com/treasure-data/td-client-java/pull/75"},"issue":{"href":"https://api.github.com/repos/treasure-data/td-client-java/issues/75"},"comments":{"href":"https://api.github.com/repos/treasure-data/td-client-java/issues/75/comments"},"review_comments":{"href":"https://api.github.com/repos/treasure-data/td-client-java/pulls/75/comments"},"review_comment":{"href":"https://api.github.com/repos/treasure-data/td-client-java/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/treasure-data/td-client-java/pulls/75/commits"},"statuses":{"href":"https://api.github.com/repos/treasure-data/td-client-java/statuses/35c727f4cf4b7293eea7e750b283f7bb393ebcb4"}}}}
{"actor":{"display_login":"komamitsu"}}