You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2023/06/27 22:20:46 UTC

[couchdb] branch changes-websocket updated (a65ca9265 -> 2ab48cf3f)

This is an automated email from the ASF dual-hosted git repository.

rnewson pushed a change to branch changes-websocket
in repository https://gitbox.apache.org/repos/asf/couchdb.git


 discard a65ca9265 support Websocket protocol for continuous response
     new 2ab48cf3f support Websocket protocol for continuous response

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (a65ca9265)
            \
             N -- N -- N   refs/heads/changes-websocket (2ab48cf3f)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/chttpd/src/chttpd_db.erl | 41 +++++++++++++++++++++++++++++++++++------
 1 file changed, 35 insertions(+), 6 deletions(-)


[couchdb] 01/01: support Websocket protocol for continuous response

Posted by rn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rnewson pushed a commit to branch changes-websocket
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 2ab48cf3f8869be333c03c4119d649082b0bac29
Author: Robert Newson <rn...@apache.org>
AuthorDate: Mon Jun 26 21:29:08 2023 +0100

    support Websocket protocol for continuous response
    
    This isn't full Websocket protocol as mochiweb doesn't have
    the plumbing but it's still useful.
    
    The endpoint will not return a pong if the client sends a ping
    but if the heartbeat parameter is used the server will send pongs
    at that right as a keep-alive.
---
 src/chttpd/src/chttpd_db.erl | 59 ++++++++++++++++++++++++++++++++++++++------
 1 file changed, 51 insertions(+), 8 deletions(-)

diff --git a/src/chttpd/src/chttpd_db.erl b/src/chttpd/src/chttpd_db.erl
index 791ee86f3..8beb94cb6 100644
--- a/src/chttpd/src/chttpd_db.erl
+++ b/src/chttpd/src/chttpd_db.erl
@@ -163,18 +163,29 @@ handle_changes_req1(#httpd{} = Req, Db) ->
             throw({bad_request, Msg})
     end.
 
-% websocket goop
+% websocket callbacks
 changes_callback(start, #cacc{feed = continuous, websocket = true} = Acc) ->
     {ReentryWs, ReplyChannel} = mochiweb_websocket:upgrade_connection(
-        Acc#cacc.mochi, fun client_ws_loop/3),
-    {ok, Acc#cacc{mochi = undefined, websocket = {ws, ReentryWs, ReplyChannel}}};
+        Acc#cacc.mochi#httpd.mochi_req, fun client_ws_loop/3),
+    {ok, Acc#cacc{websocket = {ws, ReentryWs, ReplyChannel}}};
 changes_callback({change, Change}, #cacc{websocket = {ws, _ReentryWs, ReplyChannel}} = Acc) ->
     chttpd_stats:incr_rows(),
     ReplyChannel(?JSON_ENCODE(Change)),
     {ok, Acc};
-changes_callback(timeout, #cacc{websocket = {ws, _ReentryWs, ReplyChannel}} = Acc) ->
-    ReplyChannel("\n"),
+changes_callback(timeout, #cacc{websocket = {ws, _ReentryWs, _ReplyChannel}} = Acc) ->
+    ws_pong(Acc),
     {ok, Acc};
+changes_callback(waiting_for_updates, #cacc{websocket = {ws, _ReentryWs, _ReplyChannel}} = Acc) ->
+    {ok, Acc};
+changes_callback({error, Reason}, #cacc{websocket = {ws, _ReentryWs, ReplyChannel}} = Acc) ->
+    {_Code, ErrorStr, ReasonStr} = chttpd:error_info(Reason),
+    Row =
+        {[
+            {<<"error">>, ErrorStr},
+            {<<"reason">>, ReasonStr}
+         ]},
+    ReplyChannel(?JSON_ENCODE(Row)),
+    ws_close(Acc);
 changes_callback({stop, EndSeq, Pending}, #cacc{websocket = {ws, _ReentryWs, ReplyChannel}} = Acc) ->
     Row =
         {[
@@ -182,7 +193,7 @@ changes_callback({stop, EndSeq, Pending}, #cacc{websocket = {ws, _ReentryWs, Rep
             {<<"pending">>, Pending}
         ]},
     ReplyChannel(?JSON_ENCODE(Row)),
-    {stop, Acc};
+    ws_close(Acc);
 
 % callbacks for continuous feed (newline-delimited JSON Objects)
 changes_callback(start, #cacc{feed = continuous} = Acc) ->
@@ -2584,8 +2595,40 @@ websocket_upgrade_requested(#httpd{} = Req) ->
     Upgrade = chttpd:header_value(Req, "Upgrade"),
     Upgrade =/= undefined andalso string:to_lower(Upgrade) =:= "websocket".
 
-client_ws_loop(_Payload, Broadcaster, _ReplyChannel) ->
-    Broadcaster.
+client_ws_loop(_Payload, State, _ReplyChannel) ->
+    State.
+
+ws_pong(Req) ->
+    Socket = ws_socket(Req),
+    mochiweb_socket:send(Socket, <<1:1, 0:3, 10:4, 0:8>>).
+
+ws_close(Req) ->
+    Socket = ws_socket(Req),
+    mochiweb_socket:send(Socket, <<1:1, 0:3, 10:4, 0:8>>),
+    ws_shutdown(Socket, write),
+    ws_drain(Socket),
+    mochiweb_socket:close(Socket),
+    exit({shutdown, websocket_close}).
+
+ws_drain(Socket) ->
+    Result = gen_tcp:recv(Socket, 8192),
+    case Result of
+        {ok, _Data} ->
+            ws_drain(socket);
+        {error, closed} ->
+            ok
+    end.
+
+ws_socket(#cacc{} = Acc) ->
+    ws_socket(Acc#cacc.mochi#httpd.mochi_req);
+ws_socket({ReqM, _} = Req) ->
+    ReqM:get(socket, Req).
+
+ws_shutdown({ssl, Socket}, How) ->
+    gen_tcp:shutdown(Socket, How);
+ws_shutdown(Socket, How) ->
+    gen_tcp:shutdown(Socket, How).
+
 
 -ifdef(TEST).
 -include_lib("eunit/include/eunit.hrl").