{"action":"created","comment":{"url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/pulls/comments/38497633","id":38497633,"diff_hunk":"@@ -0,0 +1,152 @@\n+/*\n+ * Copyright (c) 2010-2012 Sonatype, Inc. All rights reserved.\n+ *\n+ * This program is licensed to you under the Apache License Version 2.0,\n+ * and you may not use this file except in compliance with the Apache License Version 2.0.\n+ * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.\n+ *\n+ * Unless required by applicable law or agreed to in writing,\n+ * software distributed under the Apache License Version 2.0 is distributed on an\n+ * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.\n+ */\n+package org.asynchttpclient.request.body.generator;\n+\n+import java.io.IOException;\n+import java.nio.ByteBuffer;\n+import java.util.concurrent.Executor;\n+\n+import org.asynchttpclient.request.body.Body;\n+import org.reactivestreams.Publisher;\n+import org.reactivestreams.Subscriber;\n+import org.reactivestreams.Subscription;\n+import org.slf4j.Logger;\n+import org.slf4j.LoggerFactory;\n+\n+public class ReactiveStreamBodyGenerator implements IFeedableBodyGenerator, BodyGenerator {\n+    private static final ByteBuffer EMPTY = ByteBuffer.wrap(\"\".getBytes());\n+\n+    private final Executor executor;\n+    private final Publisher<ByteBuffer> publisher;\n+    private final FeedableBodyGenerator feedableBodyGenerator;\n+\n+    /** Constructor\n+     * \n+     * @param executor Executor to use to run task asynchronously when streaming the body. Can be null if {@link #createBody()} is not used.\n+     * @param publisher The body parts generator.\n+     */\n+    public ReactiveStreamBodyGenerator(Executor executor, Publisher<ByteBuffer> publisher) {\n+        this.executor = executor;\n+        this.publisher = publisher;\n+        this.feedableBodyGenerator = new FeedableBodyGenerator();\n+    }\n+\n+    public Publisher<ByteBuffer> getPublisher() {\n+        return this.publisher;\n+    }\n+\n+    @Override\n+    public void writeChunkBoundaries() {\n+        feedableBodyGenerator.writeChunkBoundaries();\n+    }\n+\n+    @Override\n+    public void setListener(FeedListener listener) {\n+        feedableBodyGenerator.setListener(listener);\n+    }\n+\n+    @Override\n+    public Body createBody() {\n+        return new StreamedBody(executor, publisher, feedableBodyGenerator);\n+    }\n+\n+    private class StreamedBody implements Body {\n+        private final SimpleSubscriber subscriber;\n+        private final Body body;\n+\n+        public StreamedBody(Executor executor, Publisher<ByteBuffer> publisher, FeedableBodyGenerator bodyGenerator) {\n+            if(executor == null)\n+                throw new IllegalStateException(\"An Executor is needed to stream the body. Hint: Create an instance of ReactiveStreamBodyGenerator with a non null Executor.\");\n+            this.body = bodyGenerator.createBody();\n+            this.subscriber = new SimpleSubscriber(executor, bodyGenerator);\n+            publisher.subscribe(subscriber);\n+        }\n+\n+        @Override\n+        public void close() throws IOException {\n+            body.close();\n+        }\n+\n+        @Override\n+        public long getContentLength() {\n+            return body.getContentLength();\n+        }\n+\n+        @Override\n+        public State read(ByteBuffer buffer) throws IOException {\n+            return body.read(buffer);\n+        }\n+    }\n+\n+    private static class SimpleSubscriber implements Subscriber<ByteBuffer> {\n+\n+        private static final Logger LOGGER = LoggerFactory.getLogger(SimpleSubscriber.class);\n+\n+        private final Executor executor;\n+        private final FeedableBodyGenerator feeder;\n+        private volatile Subscription subscription;\n+\n+        public SimpleSubscriber(Executor executor, FeedableBodyGenerator feeder) {\n+            this.executor = executor;\n+            this.feeder = feeder;\n+        }\n+\n+        @Override\n+        public void onSubscribe(Subscription s) {\n+            if (s == null) throw null;\n+\n+            // If someone has made a mistake and added this Subscriber multiple times, let's handle it gracefully\n+            if (this.subscription != null) { \n+                s.cancel(); // Cancel the additional subscription\n+            }\n+            else {\n+              subscription = s;\n+              subscription.request(1);\n+            }\n+        }\n+\n+        @Override\n+        public void onNext(ByteBuffer t) {\n+            if (t == null) throw null;\n+            try {\n+                feeder.feed(t, false);\n+                executor.execute(new Runnable() {\n+                    @Override\n+                    public void run() {\n+                      subscription.request(1);","path":"api/src/main/java/org/asynchttpclient/request/body/generator/ReactiveStreamBodyGenerator.java","position":126,"original_position":126,"commit_id":"c463b2a675d6831e3789361605da5a27a226b757","original_commit_id":"c463b2a675d6831e3789361605da5a27a226b757","user":{"login":"jroper","id":105833,"avatar_url":"https://avatars.githubusercontent.com/u/105833?v=3","gravatar_id":"","url":"https://api.github.com/users/jroper","html_url":"https://github.com/jroper","followers_url":"https://api.github.com/users/jroper/followers","following_url":"https://api.github.com/users/jroper/following{/other_user}","gists_url":"https://api.github.com/users/jroper/gists{/gist_id}","starred_url":"https://api.github.com/users/jroper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jroper/subscriptions","organizations_url":"https://api.github.com/users/jroper/orgs","repos_url":"https://api.github.com/users/jroper/repos","events_url":"https://api.github.com/users/jroper/events{/privacy}","received_events_url":"https://api.github.com/users/jroper/received_events","type":"User","site_admin":false},"body":"Considering this is a fallback implementation, I think it's fine.  Another option, seeing as the provider is going to end up buffering infinitely anyway, is you can invoke `subscription.request(Long.MAX_VALUE)` in `onSubscribe`, this is allowed according to reactive streams spec [3.17](https://github.com/reactive-streams/reactive-streams-jvm/tree/v1.0.0#3.17).","created_at":"2015-09-02T04:15:37Z","updated_at":"2015-09-02T04:15:37Z","html_url":"https://github.com/AsyncHttpClient/async-http-client/pull/963#discussion_r38497633","pull_request_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/pulls/963","_links":{"self":{"href":"https://api.github.com/repos/AsyncHttpClient/async-http-client/pulls/comments/38497633"},"html":{"href":"https://github.com/AsyncHttpClient/async-http-client/pull/963#discussion_r38497633"},"pull_request":{"href":"https://api.github.com/repos/AsyncHttpClient/async-http-client/pulls/963"}}},"pull_request":{"url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/pulls/963","id":43861081,"html_url":"https://github.com/AsyncHttpClient/async-http-client/pull/963","diff_url":"https://github.com/AsyncHttpClient/async-http-client/pull/963.diff","patch_url":"https://github.com/AsyncHttpClient/async-http-client/pull/963.patch","issue_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/issues/963","number":963,"state":"open","locked":false,"title":"Issue/544 reactive streams support","user":{"login":"dotta","id":703748,"avatar_url":"https://avatars.githubusercontent.com/u/703748?v=3","gravatar_id":"","url":"https://api.github.com/users/dotta","html_url":"https://github.com/dotta","followers_url":"https://api.github.com/users/dotta/followers","following_url":"https://api.github.com/users/dotta/following{/other_user}","gists_url":"https://api.github.com/users/dotta/gists{/gist_id}","starred_url":"https://api.github.com/users/dotta/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotta/subscriptions","organizations_url":"https://api.github.com/users/dotta/orgs","repos_url":"https://api.github.com/users/dotta/repos","events_url":"https://api.github.com/users/dotta/events{/privacy}","received_events_url":"https://api.github.com/users/dotta/received_events","type":"User","site_admin":false},"body":"Here is an initial proposal to fix #544.\r\n\r\nI'm entirely new to both Netty and AHC, so if you notice anything weird it's very likely that I haven't understood something :-)\r\n\r\nOther than that, something is puzzling me with the tests I wrote. Basically, if I change the `CHUNK_SIZE` in https://github.com/AsyncHttpClient/async-http-client/compare/AsyncHttpClient:master...dotta:issue/544-reactive-streams-support?expand=1#diff-5133f4e3fed98f7a99081a1ae643c4c0R104 to a value around `20000` (or lower), the tests start to fail because there is a mismatch between the received bytes and the expected ones. I honestly have no good explanation for this, but I thought it may have something to do with the internals of Netty (or maybe a config flag?). I'd really appreciate it if someone more experienced with Netty could have a look.\r\n\r\nThis PR also includes @jroper commit to add support for handling response bodies.","created_at":"2015-09-01T10:42:59Z","updated_at":"2015-09-02T04:15:37Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ad7c0a865aba46965be7368fb4498c4baed9cbd3","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/pulls/963/commits","review_comments_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/pulls/963/comments","review_comment_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/pulls/comments{/number}","comments_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/issues/963/comments","statuses_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/statuses/c463b2a675d6831e3789361605da5a27a226b757","head":{"label":"dotta:issue/544-reactive-streams-support","ref":"issue/544-reactive-streams-support","sha":"c463b2a675d6831e3789361605da5a27a226b757","user":{"login":"dotta","id":703748,"avatar_url":"https://avatars.githubusercontent.com/u/703748?v=3","gravatar_id":"","url":"https://api.github.com/users/dotta","html_url":"https://github.com/dotta","followers_url":"https://api.github.com/users/dotta/followers","following_url":"https://api.github.com/users/dotta/following{/other_user}","gists_url":"https://api.github.com/users/dotta/gists{/gist_id}","starred_url":"https://api.github.com/users/dotta/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotta/subscriptions","organizations_url":"https://api.github.com/users/dotta/orgs","repos_url":"https://api.github.com/users/dotta/repos","events_url":"https://api.github.com/users/dotta/events{/privacy}","received_events_url":"https://api.github.com/users/dotta/received_events","type":"User","site_admin":false},"repo":{"id":40973276,"name":"async-http-client","full_name":"dotta/async-http-client","owner":{"login":"dotta","id":703748,"avatar_url":"https://avatars.githubusercontent.com/u/703748?v=3","gravatar_id":"","url":"https://api.github.com/users/dotta","html_url":"https://github.com/dotta","followers_url":"https://api.github.com/users/dotta/followers","following_url":"https://api.github.com/users/dotta/following{/other_user}","gists_url":"https://api.github.com/users/dotta/gists{/gist_id}","starred_url":"https://api.github.com/users/dotta/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotta/subscriptions","organizations_url":"https://api.github.com/users/dotta/orgs","repos_url":"https://api.github.com/users/dotta/repos","events_url":"https://api.github.com/users/dotta/events{/privacy}","received_events_url":"https://api.github.com/users/dotta/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/dotta/async-http-client","description":"Asynchronous Http and WebSocket Client library for Java ","fork":true,"url":"https://api.github.com/repos/dotta/async-http-client","forks_url":"https://api.github.com/repos/dotta/async-http-client/forks","keys_url":"https://api.github.com/repos/dotta/async-http-client/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dotta/async-http-client/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dotta/async-http-client/teams","hooks_url":"https://api.github.com/repos/dotta/async-http-client/hooks","issue_events_url":"https://api.github.com/repos/dotta/async-http-client/issues/events{/number}","events_url":"https://api.github.com/repos/dotta/async-http-client/events","assignees_url":"https://api.github.com/repos/dotta/async-http-client/assignees{/user}","branches_url":"https://api.github.com/repos/dotta/async-http-client/branches{/branch}","tags_url":"https://api.github.com/repos/dotta/async-http-client/tags","blobs_url":"https://api.github.com/repos/dotta/async-http-client/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dotta/async-http-client/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dotta/async-http-client/git/refs{/sha}","trees_url":"https://api.github.com/repos/dotta/async-http-client/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dotta/async-http-client/statuses/{sha}","languages_url":"https://api.github.com/repos/dotta/async-http-client/languages","stargazers_url":"https://api.github.com/repos/dotta/async-http-client/stargazers","contributors_url":"https://api.github.com/repos/dotta/async-http-client/contributors","subscribers_url":"https://api.github.com/repos/dotta/async-http-client/subscribers","subscription_url":"https://api.github.com/repos/dotta/async-http-client/subscription","commits_url":"https://api.github.com/repos/dotta/async-http-client/commits{/sha}","git_commits_url":"https://api.github.com/repos/dotta/async-http-client/git/commits{/sha}","comments_url":"https://api.github.com/repos/dotta/async-http-client/comments{/number}","issue_comment_url":"https://api.github.com/repos/dotta/async-http-client/issues/comments{/number}","contents_url":"https://api.github.com/repos/dotta/async-http-client/contents/{+path}","compare_url":"https://api.github.com/repos/dotta/async-http-client/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dotta/async-http-client/merges","archive_url":"https://api.github.com/repos/dotta/async-http-client/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dotta/async-http-client/downloads","issues_url":"https://api.github.com/repos/dotta/async-http-client/issues{/number}","pulls_url":"https://api.github.com/repos/dotta/async-http-client/pulls{/number}","milestones_url":"https://api.github.com/repos/dotta/async-http-client/milestones{/number}","notifications_url":"https://api.github.com/repos/dotta/async-http-client/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dotta/async-http-client/labels{/name}","releases_url":"https://api.github.com/repos/dotta/async-http-client/releases{/id}","created_at":"2015-08-18T13:23:35Z","updated_at":"2015-08-18T13:23:37Z","pushed_at":"2015-09-01T15:58:59Z","git_url":"git://github.com/dotta/async-http-client.git","ssh_url":"git@github.com:dotta/async-http-client.git","clone_url":"https://github.com/dotta/async-http-client.git","svn_url":"https://github.com/dotta/async-http-client","homepage":"","size":16677,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"AsyncHttpClient:master","ref":"master","sha":"6daf34a56f461c2f8da9e4f4a0a3787eb96bd010","user":{"login":"AsyncHttpClient","id":381412,"avatar_url":"https://avatars.githubusercontent.com/u/381412?v=3","gravatar_id":"","url":"https://api.github.com/users/AsyncHttpClient","html_url":"https://github.com/AsyncHttpClient","followers_url":"https://api.github.com/users/AsyncHttpClient/followers","following_url":"https://api.github.com/users/AsyncHttpClient/following{/other_user}","gists_url":"https://api.github.com/users/AsyncHttpClient/gists{/gist_id}","starred_url":"https://api.github.com/users/AsyncHttpClient/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AsyncHttpClient/subscriptions","organizations_url":"https://api.github.com/users/AsyncHttpClient/orgs","repos_url":"https://api.github.com/users/AsyncHttpClient/repos","events_url":"https://api.github.com/users/AsyncHttpClient/events{/privacy}","received_events_url":"https://api.github.com/users/AsyncHttpClient/received_events","type":"Organization","site_admin":false},"repo":{"id":1450115,"name":"async-http-client","full_name":"AsyncHttpClient/async-http-client","owner":{"login":"AsyncHttpClient","id":381412,"avatar_url":"https://avatars.githubusercontent.com/u/381412?v=3","gravatar_id":"","url":"https://api.github.com/users/AsyncHttpClient","html_url":"https://github.com/AsyncHttpClient","followers_url":"https://api.github.com/users/AsyncHttpClient/followers","following_url":"https://api.github.com/users/AsyncHttpClient/following{/other_user}","gists_url":"https://api.github.com/users/AsyncHttpClient/gists{/gist_id}","starred_url":"https://api.github.com/users/AsyncHttpClient/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AsyncHttpClient/subscriptions","organizations_url":"https://api.github.com/users/AsyncHttpClient/orgs","repos_url":"https://api.github.com/users/AsyncHttpClient/repos","events_url":"https://api.github.com/users/AsyncHttpClient/events{/privacy}","received_events_url":"https://api.github.com/users/AsyncHttpClient/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/AsyncHttpClient/async-http-client","description":"Asynchronous Http and WebSocket Client library for Java ","fork":false,"url":"https://api.github.com/repos/AsyncHttpClient/async-http-client","forks_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/forks","keys_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/keys{/key_id}","collaborators_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/teams","hooks_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/hooks","issue_events_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/issues/events{/number}","events_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/events","assignees_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/assignees{/user}","branches_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/branches{/branch}","tags_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/tags","blobs_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/git/refs{/sha}","trees_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/git/trees{/sha}","statuses_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/statuses/{sha}","languages_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/languages","stargazers_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/stargazers","contributors_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/contributors","subscribers_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/subscribers","subscription_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/subscription","commits_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/commits{/sha}","git_commits_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/git/commits{/sha}","comments_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/comments{/number}","issue_comment_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/issues/comments{/number}","contents_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/contents/{+path}","compare_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/compare/{base}...{head}","merges_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/merges","archive_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/downloads","issues_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/issues{/number}","pulls_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/pulls{/number}","milestones_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/milestones{/number}","notifications_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/labels{/name}","releases_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/releases{/id}","created_at":"2011-03-07T13:41:46Z","updated_at":"2015-09-01T16:09:35Z","pushed_at":"2015-09-01T18:38:31Z","git_url":"git://github.com/AsyncHttpClient/async-http-client.git","ssh_url":"git@github.com:AsyncHttpClient/async-http-client.git","clone_url":"https://github.com/AsyncHttpClient/async-http-client.git","svn_url":"https://github.com/AsyncHttpClient/async-http-client","homepage":"","size":30696,"stargazers_count":2293,"watchers_count":2293,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":675,"mirror_url":null,"open_issues_count":53,"forks":675,"open_issues":53,"watchers":2293,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/AsyncHttpClient/async-http-client/pulls/963"},"html":{"href":"https://github.com/AsyncHttpClient/async-http-client/pull/963"},"issue":{"href":"https://api.github.com/repos/AsyncHttpClient/async-http-client/issues/963"},"comments":{"href":"https://api.github.com/repos/AsyncHttpClient/async-http-client/issues/963/comments"},"review_comments":{"href":"https://api.github.com/repos/AsyncHttpClient/async-http-client/pulls/963/comments"},"review_comment":{"href":"https://api.github.com/repos/AsyncHttpClient/async-http-client/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/AsyncHttpClient/async-http-client/pulls/963/commits"},"statuses":{"href":"https://api.github.com/repos/AsyncHttpClient/async-http-client/statuses/c463b2a675d6831e3789361605da5a27a226b757"}}}}