{"action":"created","comment":{"url":"https://api.github.com/repos/netty/netty/pulls/comments/45655225","id":45655225,"diff_hunk":"@@ -121,35 +123,84 @@ public void close() { }\n     @Override\n     public ChannelFuture writeData(ChannelHandlerContext ctx, int streamId, ByteBuf data,\n             int padding, boolean endStream, ChannelPromise promise) {\n-        boolean releaseData = true;\n+        boolean lastHeaderWritten = false;\n+        boolean lastFrameWritten = false;\n         SimpleChannelPromiseAggregator promiseAggregator =\n                 new SimpleChannelPromiseAggregator(promise, ctx.channel(), ctx.executor());\n+\n+        // All padding will be put at the end, so in the worst case we need 3 headers:\n+        // a repeated no-padding frame of maxFrameSize, a frame that has part data and part\n+        // padding, and a frame that has the remainder of the padding.\n+        ByteBuf headerBuf = ctx.alloc().buffer(3 * DATA_FRAME_HEADER_LENGTH);\n         try {\n             verifyStreamId(streamId, STREAM_ID);\n             verifyPadding(padding);\n \n-            Http2Flags flags = new Http2Flags().paddingPresent(padding > 0).endOfStream(endStream);\n+            int remainingData = data.readableBytes();\n+            int headerIndex = 0;\n+            int prevFrameDataBytes = 0;\n+            int prevFramePaddingBytes = 0;\n+            ByteBuf frameHeader = null;\n \n-            int payloadLength = data.readableBytes() + padding + flags.getPaddingPresenceFieldLength();\n-            verifyPayloadLength(payloadLength);\n+            do {\n+                // Determine how much data and padding to write in this frame.\n+                int frameDataBytes = min(remainingData, maxFrameSize);\n+                int framePaddingBytes = min(padding, max(0, (maxFrameSize - 1) - frameDataBytes));\n+\n+                // Decrement the remaining counters.\n+                padding -= framePaddingBytes;\n+                remainingData -= frameDataBytes;\n+                boolean lastFrame = remainingData == 0 && padding == 0;\n+\n+                if (frameDataBytes != prevFrameDataBytes || framePaddingBytes != prevFramePaddingBytes\n+                        || frameHeader == null) {\n+                    // Go to the next unused header frame and write it.\n+                    frameHeader = headerBuf.slice(headerIndex++ * DATA_FRAME_HEADER_LENGTH,\n+                            DATA_FRAME_HEADER_LENGTH).writerIndex(0);\n+                    prevFrameDataBytes = frameDataBytes;\n+                    prevFramePaddingBytes = framePaddingBytes;\n+                    Http2Flags flags = new Http2Flags().paddingPresent(framePaddingBytes > 0);\n+                    if (endStream && lastFrame) {\n+                        // This is the last frame to be sent for this stream.\n+                        flags.endOfStream(true);\n+                    }\n+\n+                    int payloadLength = frameDataBytes + framePaddingBytes + flags.getPaddingPresenceFieldLength();\n+                    verifyPayloadLength(payloadLength);","path":"codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2FrameWriter.java","position":81,"original_position":81,"commit_id":"99fcaad263a28f3c86c704b9851339f959806e9a","original_commit_id":"99fcaad263a28f3c86c704b9851339f959806e9a","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":"Is this call necessary now that we're enforcing the frame length restriction above anyway?","created_at":"2015-11-23T20:21:30Z","updated_at":"2015-11-23T20:21:30Z","html_url":"https://github.com/netty/netty/pull/4488#discussion_r45655225","pull_request_url":"https://api.github.com/repos/netty/netty/pulls/4488","_links":{"self":{"href":"https://api.github.com/repos/netty/netty/pulls/comments/45655225"},"html":{"href":"https://github.com/netty/netty/pull/4488#discussion_r45655225"},"pull_request":{"href":"https://api.github.com/repos/netty/netty/pulls/4488"}}},"pull_request":{"url":"https://api.github.com/repos/netty/netty/pulls/4488","id":51287587,"html_url":"https://github.com/netty/netty/pull/4488","diff_url":"https://github.com/netty/netty/pull/4488.diff","patch_url":"https://github.com/netty/netty/pull/4488.patch","issue_url":"https://api.github.com/repos/netty/netty/issues/4488","number":4488,"state":"open","locked":false,"title":"Allow HTTP2 frame writer to accept arbitrarily large frames","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":"Motivation:\r\n\r\nThe encoder is currently responsible for chunking frames when writing in order to conform to max frame size. The frame writer would be a better place for this since it could perform a reuse the same promise aggregator for all the write and could also perform a single allocation for all of the frame headers.\r\n\r\nModifications:\r\n\r\nModified `DefaultHttp2FrameWriter` to perform the chunking and modified the contract in the `Http2FrameWriter` interface. Modified `DefaultHttp2ConnectionEncoder` to send give all allocated bytes to the writer.\r\n\r\nResult:\r\n\r\nFixes #3966","created_at":"2015-11-19T22:01:28Z","updated_at":"2015-11-23T20:21:30Z","closed_at":null,"merged_at":null,"merge_commit_sha":"645d2cf3d9586e6b1f991f75e64ed7a11adb854a","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/netty/netty/pulls/4488/commits","review_comments_url":"https://api.github.com/repos/netty/netty/pulls/4488/comments","review_comment_url":"https://api.github.com/repos/netty/netty/pulls/comments{/number}","comments_url":"https://api.github.com/repos/netty/netty/issues/4488/comments","statuses_url":"https://api.github.com/repos/netty/netty/statuses/99fcaad263a28f3c86c704b9851339f959806e9a","head":{"label":"nmittler:frame_writer_big_messages","ref":"frame_writer_big_messages","sha":"99fcaad263a28f3c86c704b9851339f959806e9a","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":18468502,"name":"netty","full_name":"nmittler/netty","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/netty","description":"Netty project - an event-driven asynchronous network application framework","fork":true,"url":"https://api.github.com/repos/nmittler/netty","forks_url":"https://api.github.com/repos/nmittler/netty/forks","keys_url":"https://api.github.com/repos/nmittler/netty/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nmittler/netty/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nmittler/netty/teams","hooks_url":"https://api.github.com/repos/nmittler/netty/hooks","issue_events_url":"https://api.github.com/repos/nmittler/netty/issues/events{/number}","events_url":"https://api.github.com/repos/nmittler/netty/events","assignees_url":"https://api.github.com/repos/nmittler/netty/assignees{/user}","branches_url":"https://api.github.com/repos/nmittler/netty/branches{/branch}","tags_url":"https://api.github.com/repos/nmittler/netty/tags","blobs_url":"https://api.github.com/repos/nmittler/netty/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nmittler/netty/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nmittler/netty/git/refs{/sha}","trees_url":"https://api.github.com/repos/nmittler/netty/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nmittler/netty/statuses/{sha}","languages_url":"https://api.github.com/repos/nmittler/netty/languages","stargazers_url":"https://api.github.com/repos/nmittler/netty/stargazers","contributors_url":"https://api.github.com/repos/nmittler/netty/contributors","subscribers_url":"https://api.github.com/repos/nmittler/netty/subscribers","subscription_url":"https://api.github.com/repos/nmittler/netty/subscription","commits_url":"https://api.github.com/repos/nmittler/netty/commits{/sha}","git_commits_url":"https://api.github.com/repos/nmittler/netty/git/commits{/sha}","comments_url":"https://api.github.com/repos/nmittler/netty/comments{/number}","issue_comment_url":"https://api.github.com/repos/nmittler/netty/issues/comments{/number}","contents_url":"https://api.github.com/repos/nmittler/netty/contents/{+path}","compare_url":"https://api.github.com/repos/nmittler/netty/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nmittler/netty/merges","archive_url":"https://api.github.com/repos/nmittler/netty/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nmittler/netty/downloads","issues_url":"https://api.github.com/repos/nmittler/netty/issues{/number}","pulls_url":"https://api.github.com/repos/nmittler/netty/pulls{/number}","milestones_url":"https://api.github.com/repos/nmittler/netty/milestones{/number}","notifications_url":"https://api.github.com/repos/nmittler/netty/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nmittler/netty/labels{/name}","releases_url":"https://api.github.com/repos/nmittler/netty/releases{/id}","created_at":"2014-04-05T14:33:03Z","updated_at":"2015-02-02T15:50:48Z","pushed_at":"2015-11-23T15:49:16Z","git_url":"git://github.com/nmittler/netty.git","ssh_url":"git@github.com:nmittler/netty.git","clone_url":"https://github.com/nmittler/netty.git","svn_url":"https://github.com/nmittler/netty","homepage":"http://netty.io/","size":43468,"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":"1a096d4ce62c7fc728862355504322786c0ca6dd","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-11-23T16:44:17Z","pushed_at":"2015-11-23T19:42:38Z","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":58336,"stargazers_count":5630,"watchers_count":5630,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2859,"mirror_url":null,"open_issues_count":338,"forks":2859,"open_issues":338,"watchers":5630,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/netty/netty/pulls/4488"},"html":{"href":"https://github.com/netty/netty/pull/4488"},"issue":{"href":"https://api.github.com/repos/netty/netty/issues/4488"},"comments":{"href":"https://api.github.com/repos/netty/netty/issues/4488/comments"},"review_comments":{"href":"https://api.github.com/repos/netty/netty/pulls/4488/comments"},"review_comment":{"href":"https://api.github.com/repos/netty/netty/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/netty/netty/pulls/4488/commits"},"statuses":{"href":"https://api.github.com/repos/netty/netty/statuses/99fcaad263a28f3c86c704b9851339f959806e9a"}}}}