{"action":"created","comment":{"url":"https://api.github.com/repos/netty/netty/pulls/comments/33726857","id":33726857,"diff_hunk":"@@ -0,0 +1,225 @@\n+/*\n+ * Copyright 2015 The Netty Project\n+ *\n+ * The Netty Project licenses this file to you under the Apache License, version 2.0 (the\n+ * \"License\"); you may not use this file except in compliance with the License. You may obtain a\n+ * 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 distributed under the License\n+ * is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\n+ * or implied. See the License for the specific language governing permissions and limitations under\n+ * the License.\n+ */\n+package io.netty.channel;\n+\n+import io.netty.buffer.ByteBuf;\n+import io.netty.buffer.ByteBufAllocator;\n+import io.netty.buffer.CompositeByteBuf;\n+import io.netty.buffer.Unpooled;\n+import io.netty.util.ReferenceCountUtil;\n+import io.netty.util.concurrent.ImmediateEventExecutor;\n+import io.netty.util.internal.ObjectUtil;\n+import io.netty.util.internal.logging.InternalLogger;\n+import io.netty.util.internal.logging.InternalLoggerFactory;\n+\n+import java.util.ArrayDeque;\n+\n+/**\n+ * A FIFO queue of bytes where producers add bytes by repeatedly adding {@link ByteBuf} and consumers take bytes in\n+ * arbitrary lengths. This allows producers to add lots of small buffers and the consumer to take all the bytes\n+ * out in a single buffer. Conversely the producer may add larger buffers and the consumer could take the bytes in\n+ * many small buffers.\n+ *\n+ * <p>Bytes are added and removed with promises. If the last byte of a buffer added with a promise is removed then\n+ * that promise will complete when the promise passed to {@link #remove} completes.\n+ *\n+ * <p>This functionality is useful for aggregating or partitioning writes into fixed size buffers for framing protocols\n+ * such as HTTP2.\n+ */\n+public final class CoalescingBufferQueue {\n+\n+    private static final InternalLogger logger = InternalLoggerFactory.getInstance(CoalescingBufferQueue.class);\n+\n+    private final ByteBufAllocator alloc;\n+    private final ArrayDeque<Object> bufAndListenerPairs = new ArrayDeque<Object>();\n+    private int readableBytes;\n+\n+    public CoalescingBufferQueue(ByteBufAllocator alloc) {\n+        this.alloc = ObjectUtil.checkNotNull(alloc, \"alloc\");\n+    }\n+\n+    /**\n+     * Add a buffer to the end of the queue.\n+     */\n+    public void add(ByteBuf buf) {\n+        ObjectUtil.checkNotNull(buf, \"buf\");\n+        if (readableBytes > Integer.MAX_VALUE - buf.readableBytes()) {\n+            throw new IllegalStateException(\"buffer queue length overflow: \" + readableBytes\n+                    + \" + \" + buf.readableBytes());\n+        }\n+        bufAndListenerPairs.add(buf);\n+        readableBytes += buf.readableBytes();\n+    }\n+\n+    /**\n+     * Add a buffer to the end of the queue and associate a promise with it that should be completed when\n+     * all the buffers bytes have been consumed from the queue and written.\n+     * @param buf to add to the tail of the queue\n+     * @param promise to complete when all the bytes have been consumed and written, can be void.\n+     */\n+    public void add(ByteBuf buf, ChannelPromise promise) {\n+        // buffers are added before promises so that we naturally 'consume' the entire buffer during removal\n+        // before we complete it's promise.\n+        ObjectUtil.checkNotNull(promise, \"promise\");\n+        if (promise.isVoid()) {\n+            add(buf);\n+        } else {\n+            add(buf, new ChannelPromiseNotifier(promise));\n+        }\n+    }\n+\n+    /**\n+     * Add a buffer to the end of the queue and associate a listener with it that should be completed when\n+     * all the buffers  bytes have been consumed from the queue and written.\n+     * @param buf to add to the tail of the queue\n+     * @param listener to notify when all the bytes have been consumed and written.\n+     */\n+    public void add(ByteBuf buf, ChannelFutureListener listener) {\n+        // buffers are added before promises so that we naturally 'consume' the entire buffer during removal\n+        // before we complete it's promise.\n+        ObjectUtil.checkNotNull(buf, \"buf\");\n+        ObjectUtil.checkNotNull(listener, \"listener\");\n+        if (readableBytes > Integer.MAX_VALUE - buf.readableBytes()) {\n+            throw new IllegalStateException(\"buffer queue length overflow: \" + readableBytes\n+                    + \" + \" + buf.readableBytes());\n+        }\n+        bufAndListenerPairs.add(buf);\n+        bufAndListenerPairs.add(listener);\n+        readableBytes += buf.readableBytes();\n+    }\n+\n+\n+    /**\n+     * Remove a {@link ByteBuf} from the queue with the specified number of bytes. Any added buffer who's bytes are\n+     * fully consumed during removal will have it's promise completed when the passed aggregate {@link ChannelPromise}\n+     * completes.\n+     *\n+     * @param bytes the maximum number of readable bytes in the returned {@link ByteBuf}, if {@code bytes} is greater\n+     *              than {@link #readableBytes} then a buffer of length {@link #readableBytes} is returned.\n+     * @param aggregatePromise used to aggregate the promises and listeners for the constituent buffers.\n+     * @return a {@link ByteBuf} composed of the enqueued buffers.\n+     */\n+    public ByteBuf remove(int bytes, ChannelPromise aggregatePromise) {","path":"transport/src/main/java/io/netty/channel/CoalescingBufferQueue.java","position":114,"original_position":114,"commit_id":"6969ec183f147cc9c3a5f7ff16a069496d8ee093","original_commit_id":"6969ec183f147cc9c3a5f7ff16a069496d8ee093","user":{"login":"Scottmitch","id":7562868,"avatar_url":"https://avatars.githubusercontent.com/u/7562868?v=3","gravatar_id":"","url":"https://api.github.com/users/Scottmitch","html_url":"https://github.com/Scottmitch","followers_url":"https://api.github.com/users/Scottmitch/followers","following_url":"https://api.github.com/users/Scottmitch/following{/other_user}","gists_url":"https://api.github.com/users/Scottmitch/gists{/gist_id}","starred_url":"https://api.github.com/users/Scottmitch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Scottmitch/subscriptions","organizations_url":"https://api.github.com/users/Scottmitch/orgs","repos_url":"https://api.github.com/users/Scottmitch/repos","events_url":"https://api.github.com/users/Scottmitch/events{/privacy}","received_events_url":"https://api.github.com/users/Scottmitch/received_events","type":"User","site_admin":false},"body":"@nmittler - True. Currently each `ChannelPromise` will likely be wrapped in a `ChannelPromiseNotifier` (which allocates an array of `Promise<? super V>`) (so `n` additional allocations for each `ChannelPromise` + 1 allocation for each `remove` call).  Using something like `SimpleChannelPromiseAggregator` would just be 1 additional promise allocation per `remove` call.  The API suggestion for `remove` seems a bit more straightforward too (remove returns everything you need, instead of having to worry about input/output parameters).","created_at":"2015-07-01T21:29:44Z","updated_at":"2015-07-01T21:29:44Z","html_url":"https://github.com/netty/netty/pull/3926#discussion_r33726857","pull_request_url":"https://api.github.com/repos/netty/netty/pulls/3926","_links":{"self":{"href":"https://api.github.com/repos/netty/netty/pulls/comments/33726857"},"html":{"href":"https://github.com/netty/netty/pull/3926#discussion_r33726857"},"pull_request":{"href":"https://api.github.com/repos/netty/netty/pulls/3926"}}},"pull_request":{"url":"https://api.github.com/repos/netty/netty/pulls/3926","id":38916605,"html_url":"https://github.com/netty/netty/pull/3926","diff_url":"https://github.com/netty/netty/pull/3926.diff","patch_url":"https://github.com/netty/netty/pull/3926.patch","issue_url":"https://api.github.com/repos/netty/netty/issues/3926","number":3926,"state":"open","locked":false,"title":"Utility for enqueuing buffers with promises and dequeueing them in arbitrary chunks","user":{"login":"louiscryan","id":9046588,"avatar_url":"https://avatars.githubusercontent.com/u/9046588?v=3","gravatar_id":"","url":"https://api.github.com/users/louiscryan","html_url":"https://github.com/louiscryan","followers_url":"https://api.github.com/users/louiscryan/followers","following_url":"https://api.github.com/users/louiscryan/following{/other_user}","gists_url":"https://api.github.com/users/louiscryan/gists{/gist_id}","starred_url":"https://api.github.com/users/louiscryan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/louiscryan/subscriptions","organizations_url":"https://api.github.com/users/louiscryan/orgs","repos_url":"https://api.github.com/users/louiscryan/repos","events_url":"https://api.github.com/users/louiscryan/events{/privacy}","received_events_url":"https://api.github.com/users/louiscryan/received_events","type":"User","site_admin":false},"body":"@Scottmitch @nmittler @normanmaurer \r\n\r\nUtility for enqueuing buffers with promises and dequeueing them in arbitrary chunks such that when the last byte of a buffer is dequeued its enqueued promise will complete when the promise used to dequeue it completes.","created_at":"2015-06-30T19:46:32Z","updated_at":"2015-07-01T21:29:44Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6f5471c0ad3471a4239b1fff920b548d0a242e56","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/netty/netty/pulls/3926/commits","review_comments_url":"https://api.github.com/repos/netty/netty/pulls/3926/comments","review_comment_url":"https://api.github.com/repos/netty/netty/pulls/comments{/number}","comments_url":"https://api.github.com/repos/netty/netty/issues/3926/comments","statuses_url":"https://api.github.com/repos/netty/netty/statuses/6969ec183f147cc9c3a5f7ff16a069496d8ee093","head":{"label":"louiscryan:AggregatingWriteQueue","ref":"AggregatingWriteQueue","sha":"6969ec183f147cc9c3a5f7ff16a069496d8ee093","user":{"login":"louiscryan","id":9046588,"avatar_url":"https://avatars.githubusercontent.com/u/9046588?v=3","gravatar_id":"","url":"https://api.github.com/users/louiscryan","html_url":"https://github.com/louiscryan","followers_url":"https://api.github.com/users/louiscryan/followers","following_url":"https://api.github.com/users/louiscryan/following{/other_user}","gists_url":"https://api.github.com/users/louiscryan/gists{/gist_id}","starred_url":"https://api.github.com/users/louiscryan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/louiscryan/subscriptions","organizations_url":"https://api.github.com/users/louiscryan/orgs","repos_url":"https://api.github.com/users/louiscryan/repos","events_url":"https://api.github.com/users/louiscryan/events{/privacy}","received_events_url":"https://api.github.com/users/louiscryan/received_events","type":"User","site_admin":false},"repo":{"id":29745959,"name":"netty","full_name":"louiscryan/netty","owner":{"login":"louiscryan","id":9046588,"avatar_url":"https://avatars.githubusercontent.com/u/9046588?v=3","gravatar_id":"","url":"https://api.github.com/users/louiscryan","html_url":"https://github.com/louiscryan","followers_url":"https://api.github.com/users/louiscryan/followers","following_url":"https://api.github.com/users/louiscryan/following{/other_user}","gists_url":"https://api.github.com/users/louiscryan/gists{/gist_id}","starred_url":"https://api.github.com/users/louiscryan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/louiscryan/subscriptions","organizations_url":"https://api.github.com/users/louiscryan/orgs","repos_url":"https://api.github.com/users/louiscryan/repos","events_url":"https://api.github.com/users/louiscryan/events{/privacy}","received_events_url":"https://api.github.com/users/louiscryan/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/louiscryan/netty","description":"Netty project - an event-driven asynchronous network application framework","fork":true,"url":"https://api.github.com/repos/louiscryan/netty","forks_url":"https://api.github.com/repos/louiscryan/netty/forks","keys_url":"https://api.github.com/repos/louiscryan/netty/keys{/key_id}","collaborators_url":"https://api.github.com/repos/louiscryan/netty/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/louiscryan/netty/teams","hooks_url":"https://api.github.com/repos/louiscryan/netty/hooks","issue_events_url":"https://api.github.com/repos/louiscryan/netty/issues/events{/number}","events_url":"https://api.github.com/repos/louiscryan/netty/events","assignees_url":"https://api.github.com/repos/louiscryan/netty/assignees{/user}","branches_url":"https://api.github.com/repos/louiscryan/netty/branches{/branch}","tags_url":"https://api.github.com/repos/louiscryan/netty/tags","blobs_url":"https://api.github.com/repos/louiscryan/netty/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/louiscryan/netty/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/louiscryan/netty/git/refs{/sha}","trees_url":"https://api.github.com/repos/louiscryan/netty/git/trees{/sha}","statuses_url":"https://api.github.com/repos/louiscryan/netty/statuses/{sha}","languages_url":"https://api.github.com/repos/louiscryan/netty/languages","stargazers_url":"https://api.github.com/repos/louiscryan/netty/stargazers","contributors_url":"https://api.github.com/repos/louiscryan/netty/contributors","subscribers_url":"https://api.github.com/repos/louiscryan/netty/subscribers","subscription_url":"https://api.github.com/repos/louiscryan/netty/subscription","commits_url":"https://api.github.com/repos/louiscryan/netty/commits{/sha}","git_commits_url":"https://api.github.com/repos/louiscryan/netty/git/commits{/sha}","comments_url":"https://api.github.com/repos/louiscryan/netty/comments{/number}","issue_comment_url":"https://api.github.com/repos/louiscryan/netty/issues/comments{/number}","contents_url":"https://api.github.com/repos/louiscryan/netty/contents/{+path}","compare_url":"https://api.github.com/repos/louiscryan/netty/compare/{base}...{head}","merges_url":"https://api.github.com/repos/louiscryan/netty/merges","archive_url":"https://api.github.com/repos/louiscryan/netty/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/louiscryan/netty/downloads","issues_url":"https://api.github.com/repos/louiscryan/netty/issues{/number}","pulls_url":"https://api.github.com/repos/louiscryan/netty/pulls{/number}","milestones_url":"https://api.github.com/repos/louiscryan/netty/milestones{/number}","notifications_url":"https://api.github.com/repos/louiscryan/netty/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/louiscryan/netty/labels{/name}","releases_url":"https://api.github.com/repos/louiscryan/netty/releases{/id}","created_at":"2015-01-23T18:13:14Z","updated_at":"2015-05-04T20:10:55Z","pushed_at":"2015-07-01T19:30:10Z","git_url":"git://github.com/louiscryan/netty.git","ssh_url":"git@github.com:louiscryan/netty.git","clone_url":"https://github.com/louiscryan/netty.git","svn_url":"https://github.com/louiscryan/netty","homepage":"http://netty.io/","size":42217,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"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":"netty:master","ref":"master","sha":"c15aa42d3201f9183ee9f2d89b32f6e808dc953e","user":{"login":"netty","id":473791,"avatar_url":"https://avatars.githubusercontent.com/u/473791?v=3","gravatar_id":"","url":"https://api.github.com/users/netty","html_url":"https://github.com/netty","followers_url":"https://api.github.com/users/netty/followers","following_url":"https://api.github.com/users/netty/following{/other_user}","gists_url":"https://api.github.com/users/netty/gists{/gist_id}","starred_url":"https://api.github.com/users/netty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/netty/subscriptions","organizations_url":"https://api.github.com/users/netty/orgs","repos_url":"https://api.github.com/users/netty/repos","events_url":"https://api.github.com/users/netty/events{/privacy}","received_events_url":"https://api.github.com/users/netty/received_events","type":"Organization","site_admin":false},"repo":{"id":1064563,"name":"netty","full_name":"netty/netty","owner":{"login":"netty","id":473791,"avatar_url":"https://avatars.githubusercontent.com/u/473791?v=3","gravatar_id":"","url":"https://api.github.com/users/netty","html_url":"https://github.com/netty","followers_url":"https://api.github.com/users/netty/followers","following_url":"https://api.github.com/users/netty/following{/other_user}","gists_url":"https://api.github.com/users/netty/gists{/gist_id}","starred_url":"https://api.github.com/users/netty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/netty/subscriptions","organizations_url":"https://api.github.com/users/netty/orgs","repos_url":"https://api.github.com/users/netty/repos","events_url":"https://api.github.com/users/netty/events{/privacy}","received_events_url":"https://api.github.com/users/netty/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/netty/netty","description":"Netty project - an event-driven asynchronous network application framework","fork":false,"url":"https://api.github.com/repos/netty/netty","forks_url":"https://api.github.com/repos/netty/netty/forks","keys_url":"https://api.github.com/repos/netty/netty/keys{/key_id}","collaborators_url":"https://api.github.com/repos/netty/netty/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/netty/netty/teams","hooks_url":"https://api.github.com/repos/netty/netty/hooks","issue_events_url":"https://api.github.com/repos/netty/netty/issues/events{/number}","events_url":"https://api.github.com/repos/netty/netty/events","assignees_url":"https://api.github.com/repos/netty/netty/assignees{/user}","branches_url":"https://api.github.com/repos/netty/netty/branches{/branch}","tags_url":"https://api.github.com/repos/netty/netty/tags","blobs_url":"https://api.github.com/repos/netty/netty/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/netty/netty/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/netty/netty/git/refs{/sha}","trees_url":"https://api.github.com/repos/netty/netty/git/trees{/sha}","statuses_url":"https://api.github.com/repos/netty/netty/statuses/{sha}","languages_url":"https://api.github.com/repos/netty/netty/languages","stargazers_url":"https://api.github.com/repos/netty/netty/stargazers","contributors_url":"https://api.github.com/repos/netty/netty/contributors","subscribers_url":"https://api.github.com/repos/netty/netty/subscribers","subscription_url":"https://api.github.com/repos/netty/netty/subscription","commits_url":"https://api.github.com/repos/netty/netty/commits{/sha}","git_commits_url":"https://api.github.com/repos/netty/netty/git/commits{/sha}","comments_url":"https://api.github.com/repos/netty/netty/comments{/number}","issue_comment_url":"https://api.github.com/repos/netty/netty/issues/comments{/number}","contents_url":"https://api.github.com/repos/netty/netty/contents/{+path}","compare_url":"https://api.github.com/repos/netty/netty/compare/{base}...{head}","merges_url":"https://api.github.com/repos/netty/netty/merges","archive_url":"https://api.github.com/repos/netty/netty/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/netty/netty/downloads","issues_url":"https://api.github.com/repos/netty/netty/issues{/number}","pulls_url":"https://api.github.com/repos/netty/netty/pulls{/number}","milestones_url":"https://api.github.com/repos/netty/netty/milestones{/number}","notifications_url":"https://api.github.com/repos/netty/netty/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/netty/netty/labels{/name}","releases_url":"https://api.github.com/repos/netty/netty/releases{/id}","created_at":"2010-11-09T09:22:21Z","updated_at":"2015-07-01T18:25:06Z","pushed_at":"2015-07-01T19:30:12Z","git_url":"git://github.com/netty/netty.git","ssh_url":"git@github.com:netty/netty.git","clone_url":"https://github.com/netty/netty.git","svn_url":"https://github.com/netty/netty","homepage":"http://netty.io/","size":129191,"stargazers_count":4849,"watchers_count":4849,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2408,"mirror_url":null,"open_issues_count":291,"forks":2408,"open_issues":291,"watchers":4849,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/netty/netty/pulls/3926"},"html":{"href":"https://github.com/netty/netty/pull/3926"},"issue":{"href":"https://api.github.com/repos/netty/netty/issues/3926"},"comments":{"href":"https://api.github.com/repos/netty/netty/issues/3926/comments"},"review_comments":{"href":"https://api.github.com/repos/netty/netty/pulls/3926/comments"},"review_comment":{"href":"https://api.github.com/repos/netty/netty/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/netty/netty/pulls/3926/commits"},"statuses":{"href":"https://api.github.com/repos/netty/netty/statuses/6969ec183f147cc9c3a5f7ff16a069496d8ee093"}}}}