{"action":"created","comment":{"url":"https://api.github.com/repos/grpc/grpc-java/pulls/comments/56925535","id":56925535,"diff_hunk":"@@ -204,4 +218,195 @@ public final void start(Listener<RespT> responseListener, Metadata headers) {\n       }\n     }\n   }\n+\n+\n+  public static class RetryingInterceptor implements ClientInterceptor {\n+    // TODO(lukaszx0) have default logger for this one to give more visibility into when/what gets\n+    // retried?\n+    // private final Logger logger = Logger.getLogger(RetryingInterceptor.class.getName());\n+    private final RetryPolicy retryPolicy;\n+\n+    public RetryingInterceptor(RetryPolicy retryPolicy) {\n+      this.retryPolicy = retryPolicy;\n+    }\n+\n+    public RetryingInterceptor() {\n+      this.retryPolicy = new IdempotentRetryPolicy();\n+    }\n+\n+    @Override\n+    public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(\n+            MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {\n+      return new RetryingCall<ReqT, RespT>(method, callOptions, next, retryPolicy);\n+    }\n+\n+    private class RetryingCall<ReqT, RespT> extends ClientCall<ReqT, RespT> {\n+      private final MethodDescriptor<ReqT, RespT> method;\n+      private final CallOptions callOptions;\n+      private final Channel channel;\n+      private final ScheduledExecutorService scheduledExecutor;\n+      private Listener<RespT> responseListener;\n+      private Metadata requestHeaders;\n+      private ReqT requestMessage;\n+      private boolean compressionEnabled;\n+      private final Queue<AttemptListener> attemptListeners =\n+              new ConcurrentLinkedQueue<AttemptListener>();\n+      private volatile AttemptListener latestResponse;\n+      private final RetryPolicy retryPolicy;\n+\n+      RetryingCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel channel,\n+                   RetryPolicy retryPolicy) {\n+        this.method = method;\n+        this.callOptions = callOptions;\n+        this.channel = channel;\n+        this.retryPolicy = retryPolicy;\n+        this.scheduledExecutor = SharedResourceHolder.get(TIMER_SERVICE);\n+      }\n+\n+      @Override\n+      public void start(Listener<RespT> listener, Metadata headers) {\n+        checkState(attemptListeners.isEmpty());\n+        checkState(responseListener == null);\n+        checkState(requestHeaders == null);\n+        responseListener = listener;\n+        requestHeaders = headers;\n+        ClientCall<ReqT, RespT> firstCall = channel.newCall(method, callOptions);\n+        AttemptListener attemptListener = new AttemptListener(firstCall);\n+        attemptListeners.add(attemptListener);\n+        firstCall.start(attemptListener, headers);\n+      }\n+\n+      @Override\n+      public void request(int numMessages) {\n+        lastCall().request(numMessages);\n+      }\n+\n+      @Override\n+      public void cancel() {\n+        for (AttemptListener attempt : attemptListeners) {\n+          attempt.call.cancel();\n+        }\n+      }\n+\n+      @Override\n+      public void halfClose() {\n+        lastCall().halfClose();\n+      }\n+\n+      @Override\n+      public void sendMessage(ReqT message) {\n+        checkState(requestMessage == null);\n+        requestMessage = message;\n+        lastCall().sendMessage(message);\n+      }\n+\n+      @Override\n+      public boolean isReady() {\n+        return lastCall().isReady();\n+      }\n+\n+      @Override\n+      public void setMessageCompression(boolean enabled) {\n+        compressionEnabled = enabled;\n+        lastCall().setMessageCompression(enabled);\n+      }\n+\n+      private void maybeRetry(AttemptListener attempt) {\n+        Status status = attempt.responseStatus;\n+        if (!retryPolicy.isRetryable(status, method, callOptions)) {\n+          return;","path":"core/src/main/java/io/grpc/ClientInterceptors.java","position":126,"original_position":126,"commit_id":"585dda3f9e818bd7b70805d36b91be01cdadd9ae","original_commit_id":"585dda3f9e818bd7b70805d36b91be01cdadd9ae","user":{"login":"jhump","id":2035234,"avatar_url":"https://avatars.githubusercontent.com/u/2035234?v=3","gravatar_id":"","url":"https://api.github.com/users/jhump","html_url":"https://github.com/jhump","followers_url":"https://api.github.com/users/jhump/followers","following_url":"https://api.github.com/users/jhump/following{/other_user}","gists_url":"https://api.github.com/users/jhump/gists{/gist_id}","starred_url":"https://api.github.com/users/jhump/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jhump/subscriptions","organizations_url":"https://api.github.com/users/jhump/orgs","repos_url":"https://api.github.com/users/jhump/repos","events_url":"https://api.github.com/users/jhump/events{/privacy}","received_events_url":"https://api.github.com/users/jhump/received_events","type":"User","site_admin":false},"body":"Need to call `useResponse(attempt)` here.\r\n\r\nMaybe this method should be called `maybeFinish(...)`? It either finishes the call or initiates a retry. But you don't want to leave without doing either.","created_at":"2016-03-22T01:31:05Z","updated_at":"2016-03-22T01:31:05Z","html_url":"https://github.com/grpc/grpc-java/pull/1570#discussion_r56925535","pull_request_url":"https://api.github.com/repos/grpc/grpc-java/pulls/1570","_links":{"self":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/comments/56925535"},"html":{"href":"https://github.com/grpc/grpc-java/pull/1570#discussion_r56925535"},"pull_request":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/1570"}}},"pull_request":{"url":"https://api.github.com/repos/grpc/grpc-java/pulls/1570","id":63633831,"html_url":"https://github.com/grpc/grpc-java/pull/1570","diff_url":"https://github.com/grpc/grpc-java/pull/1570.diff","patch_url":"https://github.com/grpc/grpc-java/pull/1570.patch","issue_url":"https://api.github.com/repos/grpc/grpc-java/issues/1570","number":1570,"state":"open","locked":false,"title":"Support for retrying logic on client side","user":{"login":"lukaszx0","id":11562,"avatar_url":"https://avatars.githubusercontent.com/u/11562?v=3","gravatar_id":"","url":"https://api.github.com/users/lukaszx0","html_url":"https://github.com/lukaszx0","followers_url":"https://api.github.com/users/lukaszx0/followers","following_url":"https://api.github.com/users/lukaszx0/following{/other_user}","gists_url":"https://api.github.com/users/lukaszx0/gists{/gist_id}","starred_url":"https://api.github.com/users/lukaszx0/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lukaszx0/subscriptions","organizations_url":"https://api.github.com/users/lukaszx0/orgs","repos_url":"https://api.github.com/users/lukaszx0/repos","events_url":"https://api.github.com/users/lukaszx0/events{/privacy}","received_events_url":"https://api.github.com/users/lukaszx0/received_events","type":"User","site_admin":false},"body":"This PR provides support for client side retries using interceptors. This is work which I mentioned in https://github.com/grpc/grpc-java/pull/1566 and in current form it depends on this PR being merged (see `IdempotentRetryPolicy:42`).\r\n\r\nI think that grpc should ship with retry support of some kind hence I'm pushing this up for review and vetting. It lacks tests, documentations and polishing but I'm happy to work on it further if we decide to include it in core.\r\n\r\nBig thanks for @jhump who worked with me on this.\r\n\r\ncc @ericzundel\r\n","created_at":"2016-03-21T21:17:29Z","updated_at":"2016-03-22T01:31:06Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ef53aaa5aff50c079dfa3bd56aa63bf4a85ae11a","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/grpc/grpc-java/pulls/1570/commits","review_comments_url":"https://api.github.com/repos/grpc/grpc-java/pulls/1570/comments","review_comment_url":"https://api.github.com/repos/grpc/grpc-java/pulls/comments{/number}","comments_url":"https://api.github.com/repos/grpc/grpc-java/issues/1570/comments","statuses_url":"https://api.github.com/repos/grpc/grpc-java/statuses/585dda3f9e818bd7b70805d36b91be01cdadd9ae","head":{"label":"lukaszx0:client-retries","ref":"client-retries","sha":"585dda3f9e818bd7b70805d36b91be01cdadd9ae","user":{"login":"lukaszx0","id":11562,"avatar_url":"https://avatars.githubusercontent.com/u/11562?v=3","gravatar_id":"","url":"https://api.github.com/users/lukaszx0","html_url":"https://github.com/lukaszx0","followers_url":"https://api.github.com/users/lukaszx0/followers","following_url":"https://api.github.com/users/lukaszx0/following{/other_user}","gists_url":"https://api.github.com/users/lukaszx0/gists{/gist_id}","starred_url":"https://api.github.com/users/lukaszx0/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lukaszx0/subscriptions","organizations_url":"https://api.github.com/users/lukaszx0/orgs","repos_url":"https://api.github.com/users/lukaszx0/repos","events_url":"https://api.github.com/users/lukaszx0/events{/privacy}","received_events_url":"https://api.github.com/users/lukaszx0/received_events","type":"User","site_admin":false},"repo":{"id":51521701,"name":"grpc-java","full_name":"lukaszx0/grpc-java","owner":{"login":"lukaszx0","id":11562,"avatar_url":"https://avatars.githubusercontent.com/u/11562?v=3","gravatar_id":"","url":"https://api.github.com/users/lukaszx0","html_url":"https://github.com/lukaszx0","followers_url":"https://api.github.com/users/lukaszx0/followers","following_url":"https://api.github.com/users/lukaszx0/following{/other_user}","gists_url":"https://api.github.com/users/lukaszx0/gists{/gist_id}","starred_url":"https://api.github.com/users/lukaszx0/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lukaszx0/subscriptions","organizations_url":"https://api.github.com/users/lukaszx0/orgs","repos_url":"https://api.github.com/users/lukaszx0/repos","events_url":"https://api.github.com/users/lukaszx0/events{/privacy}","received_events_url":"https://api.github.com/users/lukaszx0/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/lukaszx0/grpc-java","description":"The Java gRPC implementation. HTTP/2 based RPC","fork":true,"url":"https://api.github.com/repos/lukaszx0/grpc-java","forks_url":"https://api.github.com/repos/lukaszx0/grpc-java/forks","keys_url":"https://api.github.com/repos/lukaszx0/grpc-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/lukaszx0/grpc-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/lukaszx0/grpc-java/teams","hooks_url":"https://api.github.com/repos/lukaszx0/grpc-java/hooks","issue_events_url":"https://api.github.com/repos/lukaszx0/grpc-java/issues/events{/number}","events_url":"https://api.github.com/repos/lukaszx0/grpc-java/events","assignees_url":"https://api.github.com/repos/lukaszx0/grpc-java/assignees{/user}","branches_url":"https://api.github.com/repos/lukaszx0/grpc-java/branches{/branch}","tags_url":"https://api.github.com/repos/lukaszx0/grpc-java/tags","blobs_url":"https://api.github.com/repos/lukaszx0/grpc-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/lukaszx0/grpc-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/lukaszx0/grpc-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/lukaszx0/grpc-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/lukaszx0/grpc-java/statuses/{sha}","languages_url":"https://api.github.com/repos/lukaszx0/grpc-java/languages","stargazers_url":"https://api.github.com/repos/lukaszx0/grpc-java/stargazers","contributors_url":"https://api.github.com/repos/lukaszx0/grpc-java/contributors","subscribers_url":"https://api.github.com/repos/lukaszx0/grpc-java/subscribers","subscription_url":"https://api.github.com/repos/lukaszx0/grpc-java/subscription","commits_url":"https://api.github.com/repos/lukaszx0/grpc-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/lukaszx0/grpc-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/lukaszx0/grpc-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/lukaszx0/grpc-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/lukaszx0/grpc-java/contents/{+path}","compare_url":"https://api.github.com/repos/lukaszx0/grpc-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/lukaszx0/grpc-java/merges","archive_url":"https://api.github.com/repos/lukaszx0/grpc-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/lukaszx0/grpc-java/downloads","issues_url":"https://api.github.com/repos/lukaszx0/grpc-java/issues{/number}","pulls_url":"https://api.github.com/repos/lukaszx0/grpc-java/pulls{/number}","milestones_url":"https://api.github.com/repos/lukaszx0/grpc-java/milestones{/number}","notifications_url":"https://api.github.com/repos/lukaszx0/grpc-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/lukaszx0/grpc-java/labels{/name}","releases_url":"https://api.github.com/repos/lukaszx0/grpc-java/releases{/id}","deployments_url":"https://api.github.com/repos/lukaszx0/grpc-java/deployments","created_at":"2016-02-11T14:56:45Z","updated_at":"2016-02-11T14:56:46Z","pushed_at":"2016-03-21T23:22:35Z","git_url":"git://github.com/lukaszx0/grpc-java.git","ssh_url":"git@github.com:lukaszx0/grpc-java.git","clone_url":"https://github.com/lukaszx0/grpc-java.git","svn_url":"https://github.com/lukaszx0/grpc-java","homepage":"http://grpc.io","size":6518,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"grpc:master","ref":"master","sha":"37f45e332fac1f7d9e8bbeeba4a71945eb32c492","user":{"login":"grpc","id":7802525,"avatar_url":"https://avatars.githubusercontent.com/u/7802525?v=3","gravatar_id":"","url":"https://api.github.com/users/grpc","html_url":"https://github.com/grpc","followers_url":"https://api.github.com/users/grpc/followers","following_url":"https://api.github.com/users/grpc/following{/other_user}","gists_url":"https://api.github.com/users/grpc/gists{/gist_id}","starred_url":"https://api.github.com/users/grpc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/grpc/subscriptions","organizations_url":"https://api.github.com/users/grpc/orgs","repos_url":"https://api.github.com/users/grpc/repos","events_url":"https://api.github.com/users/grpc/events{/privacy}","received_events_url":"https://api.github.com/users/grpc/received_events","type":"Organization","site_admin":false},"repo":{"id":27729926,"name":"grpc-java","full_name":"grpc/grpc-java","owner":{"login":"grpc","id":7802525,"avatar_url":"https://avatars.githubusercontent.com/u/7802525?v=3","gravatar_id":"","url":"https://api.github.com/users/grpc","html_url":"https://github.com/grpc","followers_url":"https://api.github.com/users/grpc/followers","following_url":"https://api.github.com/users/grpc/following{/other_user}","gists_url":"https://api.github.com/users/grpc/gists{/gist_id}","starred_url":"https://api.github.com/users/grpc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/grpc/subscriptions","organizations_url":"https://api.github.com/users/grpc/orgs","repos_url":"https://api.github.com/users/grpc/repos","events_url":"https://api.github.com/users/grpc/events{/privacy}","received_events_url":"https://api.github.com/users/grpc/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/grpc/grpc-java","description":"The Java gRPC implementation. HTTP/2 based RPC","fork":false,"url":"https://api.github.com/repos/grpc/grpc-java","forks_url":"https://api.github.com/repos/grpc/grpc-java/forks","keys_url":"https://api.github.com/repos/grpc/grpc-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/grpc/grpc-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/grpc/grpc-java/teams","hooks_url":"https://api.github.com/repos/grpc/grpc-java/hooks","issue_events_url":"https://api.github.com/repos/grpc/grpc-java/issues/events{/number}","events_url":"https://api.github.com/repos/grpc/grpc-java/events","assignees_url":"https://api.github.com/repos/grpc/grpc-java/assignees{/user}","branches_url":"https://api.github.com/repos/grpc/grpc-java/branches{/branch}","tags_url":"https://api.github.com/repos/grpc/grpc-java/tags","blobs_url":"https://api.github.com/repos/grpc/grpc-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/grpc/grpc-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/grpc/grpc-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/grpc/grpc-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/grpc/grpc-java/statuses/{sha}","languages_url":"https://api.github.com/repos/grpc/grpc-java/languages","stargazers_url":"https://api.github.com/repos/grpc/grpc-java/stargazers","contributors_url":"https://api.github.com/repos/grpc/grpc-java/contributors","subscribers_url":"https://api.github.com/repos/grpc/grpc-java/subscribers","subscription_url":"https://api.github.com/repos/grpc/grpc-java/subscription","commits_url":"https://api.github.com/repos/grpc/grpc-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/grpc/grpc-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/grpc/grpc-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/grpc/grpc-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/grpc/grpc-java/contents/{+path}","compare_url":"https://api.github.com/repos/grpc/grpc-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/grpc/grpc-java/merges","archive_url":"https://api.github.com/repos/grpc/grpc-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/grpc/grpc-java/downloads","issues_url":"https://api.github.com/repos/grpc/grpc-java/issues{/number}","pulls_url":"https://api.github.com/repos/grpc/grpc-java/pulls{/number}","milestones_url":"https://api.github.com/repos/grpc/grpc-java/milestones{/number}","notifications_url":"https://api.github.com/repos/grpc/grpc-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/grpc/grpc-java/labels{/name}","releases_url":"https://api.github.com/repos/grpc/grpc-java/releases{/id}","deployments_url":"https://api.github.com/repos/grpc/grpc-java/deployments","created_at":"2014-12-08T19:00:00Z","updated_at":"2016-03-21T11:16:23Z","pushed_at":"2016-03-21T23:22:35Z","git_url":"git://github.com/grpc/grpc-java.git","ssh_url":"git@github.com:grpc/grpc-java.git","clone_url":"https://github.com/grpc/grpc-java.git","svn_url":"https://github.com/grpc/grpc-java","homepage":"http://grpc.io","size":21675,"stargazers_count":1209,"watchers_count":1209,"language":"Java","has_issues":true,"has_downloads":false,"has_wiki":false,"has_pages":true,"forks_count":313,"mirror_url":null,"open_issues_count":135,"forks":313,"open_issues":135,"watchers":1209,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/1570"},"html":{"href":"https://github.com/grpc/grpc-java/pull/1570"},"issue":{"href":"https://api.github.com/repos/grpc/grpc-java/issues/1570"},"comments":{"href":"https://api.github.com/repos/grpc/grpc-java/issues/1570/comments"},"review_comments":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/1570/comments"},"review_comment":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/1570/commits"},"statuses":{"href":"https://api.github.com/repos/grpc/grpc-java/statuses/585dda3f9e818bd7b70805d36b91be01cdadd9ae"}}}}