couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kocol...@apache.org
Subject [2/3] couch-mrview commit: updated refs/heads/2724-chunked-buffering to 2f489b9
Date Mon, 20 Jul 2015 20:27:03 GMT
Buffer rows to reduce number of chunks

This patch reduces the number of chunks in an HTTP chunked response body
by coalescing multiple rows into a single transmission. The default
value is chosen to fill a standard Ethernet frame but will be made
configurable.

COUCHDB-2724


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/commit/bb94b2a5
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/tree/bb94b2a5
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/diff/bb94b2a5

Branch: refs/heads/2724-chunked-buffering
Commit: bb94b2a528ae9410517d57de1d3fb553556a4ce1
Parents: 4038d9e
Author: Adam Kocoloski <adam@cloudant.com>
Authored: Mon Jul 20 16:10:24 2015 -0400
Committer: Adam Kocoloski <adam@cloudant.com>
Committed: Mon Jul 20 16:20:16 2015 -0400

----------------------------------------------------------------------
 include/couch_mrview.hrl  |  5 ++++-
 src/couch_mrview_http.erl | 28 ++++++++++++++++++++--------
 2 files changed, 24 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/blob/bb94b2a5/include/couch_mrview.hrl
----------------------------------------------------------------------
diff --git a/include/couch_mrview.hrl b/include/couch_mrview.hrl
index 1a0cdea..c176839 100644
--- a/include/couch_mrview.hrl
+++ b/include/couch_mrview.hrl
@@ -94,7 +94,10 @@
     resp,
     prepend,
     etag,
-    should_close = false
+    should_close = false,
+    buffer = [],
+    bufsize = 0,
+    threshold = 1490
 }).
 
 -record(lacc, {

http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/blob/bb94b2a5/src/couch_mrview_http.erl
----------------------------------------------------------------------
diff --git a/src/couch_mrview_http.erl b/src/couch_mrview_http.erl
index d0da69e..9149689 100644
--- a/src/couch_mrview_http.erl
+++ b/src/couch_mrview_http.erl
@@ -331,7 +331,7 @@ view_cb({meta, Meta}, #vacc{resp=undefined}=Acc) ->
     Headers = [{"ETag", Acc#vacc.etag}],
     {ok, Resp} = chttpd:start_delayed_json_response(Acc#vacc.req, 200, Headers),
     view_cb({meta, Meta}, Acc#vacc{resp=Resp, should_close=true});
-view_cb({meta, Meta}, #vacc{resp=Resp}=Acc) ->
+view_cb({meta, Meta}, #vacc{}=Acc) ->
     % Sending metadata
     Parts = case couch_util:get_value(total, Meta) of
         undefined -> [];
@@ -344,26 +344,25 @@ view_cb({meta, Meta}, #vacc{resp=Resp}=Acc) ->
         UpdateSeq -> [io_lib:format("\"update_seq\":~p", [UpdateSeq])]
     end ++ ["\"rows\":["],
     Chunk = [prepend_val(Acc), "{", string:join(Parts, ","), "\r\n"],
-    {ok, Resp1} = chttpd:send_delayed_chunk(Resp, Chunk),
-    {ok, Acc#vacc{resp=Resp1, prepend=""}};
+    {ok, AccOut} = maybe_flush_response(Acc, Chunk, iolist_size(Chunk)),
+    {ok, AccOut#vacc{prepend=""}};
 view_cb({row, Row}, Acc) ->
     % Adding another row
     Chunk = [prepend_val(Acc), row_to_json(Row)],
-    {ok, Resp1} = chttpd:send_delayed_chunk(Acc#vacc.resp, Chunk),
-    {ok, Acc#vacc{prepend=",\r\n", resp=Resp1}};
+    maybe_flush_response(Acc, Chunk, iolist_size(Chunk));
 view_cb(complete, #vacc{resp=undefined}=Acc) ->
     % Nothing in view
     {ok, Resp} = chttpd:send_json(Acc#vacc.req, 200, {[{rows, []}]}),
     {ok, Acc#vacc{resp=Resp}};
-view_cb(complete, #vacc{resp=Resp}=Acc) ->
+view_cb(complete, #vacc{resp=Resp, buffer=Buf}=Acc) ->
     % Finish view output and possibly end the response
-    {ok, Resp1} = chttpd:send_delayed_chunk(Resp, "\r\n]}"),
+    {ok, Resp1} = chttpd:send_delayed_chunk(Resp, [Buf | "\r\n]}"]),
     case Acc#vacc.should_close of
         true ->
             {ok, Resp2} = chttpd:end_delayed_json_response(Resp1),
             {ok, Acc#vacc{resp=Resp2}};
         _ ->
-            {ok, Acc#vacc{resp=Resp1, prepend=",\r\n"}}
+            {ok, Acc#vacc{resp=Resp1, prepend=",\r\n", buffer=[], bufsize=0}}
     end;
 view_cb({error, Reason}, #vacc{resp=undefined}=Acc) ->
     {ok, Resp} = chttpd:send_error(Acc#vacc.req, Reason),
@@ -372,6 +371,19 @@ view_cb({error, Reason}, #vacc{resp=Resp}=Acc) ->
     {ok, Resp1} = chttpd:send_delayed_error(Resp, Reason),
     {ok, Acc#vacc{resp=Resp1}}.
 
+maybe_flush_response(#vacc{bufsize=Size, threshold=Max} = Acc, Data, Len)
+        when (Size + Len) > Max ->
+    #vacc{buffer = Buffer, resp = Resp} = Acc,
+    {ok, R1} = chttpd:send_delayed_chunk(Resp, Buffer),
+    {ok, Acc#vacc{prepend = ",\r\n", buffer = Data, bufsize = Len, resp = R1}};
+maybe_flush_response(Acc0, Data, Len) ->
+    #vacc{buffer = Buf, bufsize = Size} = Acc0,
+    Acc = Acc0#vacc{
+        prepend = ",\r\n",
+        buffer = [Buf | Data],
+        bufsize = Size + Len
+    },
+    {ok, Acc}.
 
 prepend_val(#vacc{prepend=Prepend}) ->
     case Prepend of


Mime
View raw message