{"action":"created","comment":{"url":"https://api.github.com/repos/grpc/grpc-java/pulls/comments/42900104","id":42900104,"diff_hunk":"@@ -0,0 +1,294 @@\n+/*\n+ * Copyright 2015, Google Inc. All rights reserved.\n+ *\n+ * Redistribution and use in source and binary forms, with or without\n+ * modification, are permitted provided that the following conditions are\n+ * met:\n+ *\n+ *    * Redistributions of source code must retain the above copyright\n+ * notice, this list of conditions and the following disclaimer.\n+ *    * Redistributions in binary form must reproduce the above\n+ * copyright notice, this list of conditions and the following disclaimer\n+ * in the documentation and/or other materials provided with the\n+ * distribution.\n+ *\n+ *    * Neither the name of Google Inc. nor the names of its\n+ * contributors may be used to endorse or promote products derived from\n+ * this software without specific prior written permission.\n+ *\n+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n+ * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n+ */\n+\n+package io.grpc.internal;\n+\n+import static com.google.common.base.Preconditions.checkNotNull;\n+\n+import io.grpc.Compressor;\n+import io.grpc.Metadata;\n+import io.grpc.Status;\n+\n+import java.io.InputStream;\n+\n+import javax.annotation.Nullable;\n+import javax.annotation.concurrent.GuardedBy;\n+\n+/**\n+ * A class that manages the outbound state on behalf of a stream. Manages the sending of headers,\n+ * messages, and trailers, as well as outbound flow control.\n+ */\n+final class OutboundState {\n+  /**\n+   * The default number of queued bytes for a given stream, below which {@link\n+   * StreamListener#onReady()} will be called.\n+   */\n+  private static final int ONREADY_THRESHOLD = 32 * 1024;\n+\n+  private enum Phase {\n+    HEADERS, MESSAGE, STATUS\n+  }\n+\n+  /**\n+   * The handler for this state.\n+   */\n+  interface Handler {\n+    /**\n+     * Sends the given headers to the remote endpoint.\n+     */\n+    void sendHeaders(Metadata headers);\n+\n+    /**\n+     * Sends the given message to the remote endpoint.\n+     *\n+     * @param frame the message to be sent.\n+     * @param endOfStream if {@code true}, indicates that the frame should be marked as the end of stream.\n+     * @param flush if {@code true}, indicates that any buffered IO should be flushed to the remote endpoint.\n+     */\n+    void sendMessage(@Nullable WritableBuffer frame, boolean endOfStream, boolean flush);\n+\n+    /**\n+     * Sends the specified trailers to the remote endpoint. This is only called for server-side\n+     * handlers.\n+     *\n+     * @param trailers the trailers to be sent\n+     * @param headersSent if {@code true}, indicates that headers were previously sent.\n+     */\n+    void sendTrailers(Metadata trailers, boolean headersSent);\n+\n+    /**\n+     * This indicates that the transport is now capable of sending additional messages\n+     * without requiring excessive buffering internally. This event is\n+     * just a suggestion and the application is free to ignore it, however doing so may\n+     * result in excessive buffering within the transport.\n+     */\n+    void onReady();\n+  }\n+\n+  private final boolean server;\n+  private final Handler handler;\n+  private final MessageFramer framer;\n+  private Phase phase = Phase.HEADERS;\n+  private boolean headersSent;\n+  private Metadata stashedTrailers;\n+\n+  /**\n+   * The number of bytes currently queued, waiting to be sent. When this falls below\n+   * onReadyThreshold, {@link StreamListener#onReady()} will be called.\n+   */\n+  @GuardedBy(\"onReadyLock\")\n+  private int numSentBytesQueued;\n+\n+  private final Object onReadyLock = new Object();\n+\n+  OutboundState(WritableBufferAllocator bufferAllocator, Handler handler, boolean server) {\n+    this.handler = checkNotNull(handler, \"handler\");\n+    this.server = server;\n+    MessageFramer.Sink frameHandler = new MessageFramer.Sink() {\n+      @Override\n+      public void deliverFrame(WritableBuffer frame, boolean endOfStream, boolean flush) {\n+        onMessageFrame(frame, endOfStream, flush);\n+      }\n+    };\n+    framer = new MessageFramer(frameHandler, bufferAllocator);\n+  }\n+\n+  void setCompressor(Compressor c) {\n+    // TODO(carl-mastrangelo): check that headers haven't already been sent.  I can't find where\n+    // the client stream changes outbound phase correctly, so I am ignoring it.\n+    framer.setCompressor(c);\n+  }\n+\n+  /**\n+   * Request the sending of headers. Must be called from the channel thread context.\n+   */\n+  void sendHeaders(Metadata headers) {\n+    checkNotNull(headers, \"headers\");\n+    phase(Phase.HEADERS);\n+    headersSent = true;\n+    handler.sendHeaders(headers);\n+    phase(Phase.MESSAGE);\n+  }\n+\n+  /**\n+   * Request that a message be sent. Must be called from the channel thread context.\n+   */\n+  void sendMessage(InputStream message) {","path":"core/src/main/java/io/grpc/internal/OutboundState.java","position":144,"original_position":144,"commit_id":"a95ee8bf09ca8ed42a4c45b28d0320b514007d35","original_commit_id":"a95ee8bf09ca8ed42a4c45b28d0320b514007d35","user":{"login":"nmittler","id":6493296,"avatar_url":"https://avatars.githubusercontent.com/u/6493296?v=3","gravatar_id":"","url":"https://api.github.com/users/nmittler","html_url":"https://github.com/nmittler","followers_url":"https://api.github.com/users/nmittler/followers","following_url":"https://api.github.com/users/nmittler/following{/other_user}","gists_url":"https://api.github.com/users/nmittler/gists{/gist_id}","starred_url":"https://api.github.com/users/nmittler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nmittler/subscriptions","organizations_url":"https://api.github.com/users/nmittler/orgs","repos_url":"https://api.github.com/users/nmittler/repos","events_url":"https://api.github.com/users/nmittler/events{/privacy}","received_events_url":"https://api.github.com/users/nmittler/received_events","type":"User","site_admin":false},"body":"@carl-mastrangelo I just realized that this will already throw ... `phase(Phase.MESSAGE)` will throw if `phase == STATUS`.","created_at":"2015-10-23T18:45:30Z","updated_at":"2015-10-23T18:45:30Z","html_url":"https://github.com/grpc/grpc-java/pull/1135#discussion_r42900104","pull_request_url":"https://api.github.com/repos/grpc/grpc-java/pulls/1135","_links":{"self":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/comments/42900104"},"html":{"href":"https://github.com/grpc/grpc-java/pull/1135#discussion_r42900104"},"pull_request":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/1135"}}},"pull_request":{"url":"https://api.github.com/repos/grpc/grpc-java/pulls/1135","id":48522224,"html_url":"https://github.com/grpc/grpc-java/pull/1135","diff_url":"https://github.com/grpc/grpc-java/pull/1135.diff","patch_url":"https://github.com/grpc/grpc-java/pull/1135.patch","issue_url":"https://api.github.com/repos/grpc/grpc-java/issues/1135","number":1135,"state":"open","locked":false,"title":"POC: Separating outbound/inbound stream state","user":{"login":"nmittler","id":6493296,"avatar_url":"https://avatars.githubusercontent.com/u/6493296?v=3","gravatar_id":"","url":"https://api.github.com/users/nmittler","html_url":"https://github.com/nmittler","followers_url":"https://api.github.com/users/nmittler/followers","following_url":"https://api.github.com/users/nmittler/following{/other_user}","gists_url":"https://api.github.com/users/nmittler/gists{/gist_id}","starred_url":"https://api.github.com/users/nmittler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nmittler/subscriptions","organizations_url":"https://api.github.com/users/nmittler/orgs","repos_url":"https://api.github.com/users/nmittler/repos","events_url":"https://api.github.com/users/nmittler/events{/privacy}","received_events_url":"https://api.github.com/users/nmittler/received_events","type":"User","site_admin":false},"body":"The stream class structure has become a bit complicated and hard to understand. Much of this is due to the fact that we have so many special cases for client/server, okhttp/netty, etc.\r\n\r\nI think that we can begin to simplify the code a bit by pulling out the core logic that compose the inbound and outbound state of the streams into specialized classes.  This PR is a first-cut at what this might look like.","created_at":"2015-10-22T22:34:47Z","updated_at":"2015-10-23T18:45:30Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a0102843756c842db576cb889b7db014576b76c7","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/grpc/grpc-java/pulls/1135/commits","review_comments_url":"https://api.github.com/repos/grpc/grpc-java/pulls/1135/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/1135/comments","statuses_url":"https://api.github.com/repos/grpc/grpc-java/statuses/a95ee8bf09ca8ed42a4c45b28d0320b514007d35","head":{"label":"nmittler:stream_redesign","ref":"stream_redesign","sha":"a95ee8bf09ca8ed42a4c45b28d0320b514007d35","user":{"login":"nmittler","id":6493296,"avatar_url":"https://avatars.githubusercontent.com/u/6493296?v=3","gravatar_id":"","url":"https://api.github.com/users/nmittler","html_url":"https://github.com/nmittler","followers_url":"https://api.github.com/users/nmittler/followers","following_url":"https://api.github.com/users/nmittler/following{/other_user}","gists_url":"https://api.github.com/users/nmittler/gists{/gist_id}","starred_url":"https://api.github.com/users/nmittler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nmittler/subscriptions","organizations_url":"https://api.github.com/users/nmittler/orgs","repos_url":"https://api.github.com/users/nmittler/repos","events_url":"https://api.github.com/users/nmittler/events{/privacy}","received_events_url":"https://api.github.com/users/nmittler/received_events","type":"User","site_admin":false},"repo":{"id":31379300,"name":"grpc-java","full_name":"nmittler/grpc-java","owner":{"login":"nmittler","id":6493296,"avatar_url":"https://avatars.githubusercontent.com/u/6493296?v=3","gravatar_id":"","url":"https://api.github.com/users/nmittler","html_url":"https://github.com/nmittler","followers_url":"https://api.github.com/users/nmittler/followers","following_url":"https://api.github.com/users/nmittler/following{/other_user}","gists_url":"https://api.github.com/users/nmittler/gists{/gist_id}","starred_url":"https://api.github.com/users/nmittler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nmittler/subscriptions","organizations_url":"https://api.github.com/users/nmittler/orgs","repos_url":"https://api.github.com/users/nmittler/repos","events_url":"https://api.github.com/users/nmittler/events{/privacy}","received_events_url":"https://api.github.com/users/nmittler/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/nmittler/grpc-java","description":"The Java gRPC implementation. HTTP/2 based RPC","fork":true,"url":"https://api.github.com/repos/nmittler/grpc-java","forks_url":"https://api.github.com/repos/nmittler/grpc-java/forks","keys_url":"https://api.github.com/repos/nmittler/grpc-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nmittler/grpc-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nmittler/grpc-java/teams","hooks_url":"https://api.github.com/repos/nmittler/grpc-java/hooks","issue_events_url":"https://api.github.com/repos/nmittler/grpc-java/issues/events{/number}","events_url":"https://api.github.com/repos/nmittler/grpc-java/events","assignees_url":"https://api.github.com/repos/nmittler/grpc-java/assignees{/user}","branches_url":"https://api.github.com/repos/nmittler/grpc-java/branches{/branch}","tags_url":"https://api.github.com/repos/nmittler/grpc-java/tags","blobs_url":"https://api.github.com/repos/nmittler/grpc-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nmittler/grpc-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nmittler/grpc-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/nmittler/grpc-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nmittler/grpc-java/statuses/{sha}","languages_url":"https://api.github.com/repos/nmittler/grpc-java/languages","stargazers_url":"https://api.github.com/repos/nmittler/grpc-java/stargazers","contributors_url":"https://api.github.com/repos/nmittler/grpc-java/contributors","subscribers_url":"https://api.github.com/repos/nmittler/grpc-java/subscribers","subscription_url":"https://api.github.com/repos/nmittler/grpc-java/subscription","commits_url":"https://api.github.com/repos/nmittler/grpc-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/nmittler/grpc-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/nmittler/grpc-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/nmittler/grpc-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/nmittler/grpc-java/contents/{+path}","compare_url":"https://api.github.com/repos/nmittler/grpc-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nmittler/grpc-java/merges","archive_url":"https://api.github.com/repos/nmittler/grpc-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nmittler/grpc-java/downloads","issues_url":"https://api.github.com/repos/nmittler/grpc-java/issues{/number}","pulls_url":"https://api.github.com/repos/nmittler/grpc-java/pulls{/number}","milestones_url":"https://api.github.com/repos/nmittler/grpc-java/milestones{/number}","notifications_url":"https://api.github.com/repos/nmittler/grpc-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nmittler/grpc-java/labels{/name}","releases_url":"https://api.github.com/repos/nmittler/grpc-java/releases{/id}","created_at":"2015-02-26T17:29:20Z","updated_at":"2015-03-03T21:47:37Z","pushed_at":"2015-10-23T18:36:58Z","git_url":"git://github.com/nmittler/grpc-java.git","ssh_url":"git@github.com:nmittler/grpc-java.git","clone_url":"https://github.com/nmittler/grpc-java.git","svn_url":"https://github.com/nmittler/grpc-java","homepage":"","size":5060,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":0,"forks":1,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"grpc:master","ref":"master","sha":"74088c4fb4bcb8b62fef05a9f43d550180b1cba3","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}","created_at":"2014-12-08T19:00:00Z","updated_at":"2015-10-23T11:37:16Z","pushed_at":"2015-10-23T18:44:55Z","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":"","size":24493,"stargazers_count":931,"watchers_count":931,"language":"Java","has_issues":true,"has_downloads":false,"has_wiki":false,"has_pages":true,"forks_count":235,"mirror_url":null,"open_issues_count":117,"forks":235,"open_issues":117,"watchers":931,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/1135"},"html":{"href":"https://github.com/grpc/grpc-java/pull/1135"},"issue":{"href":"https://api.github.com/repos/grpc/grpc-java/issues/1135"},"comments":{"href":"https://api.github.com/repos/grpc/grpc-java/issues/1135/comments"},"review_comments":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/1135/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/1135/commits"},"statuses":{"href":"https://api.github.com/repos/grpc/grpc-java/statuses/a95ee8bf09ca8ed42a4c45b28d0320b514007d35"}}}}