You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2018/07/14 14:55:11 UTC

[GitHub] janl closed pull request #389: Emit heartbeats until feed timeout

janl closed pull request #389: Emit heartbeats until feed timeout
URL: https://github.com/apache/couchdb/pull/389
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/couchdb/couch_changes.erl b/src/couchdb/couch_changes.erl
index 6edde32dba..2cb63a5510 100644
--- a/src/couchdb/couch_changes.erl
+++ b/src/couchdb/couch_changes.erl
@@ -238,29 +238,43 @@ builtin_results(Style, [#rev_info{rev=Rev}|_]=Revs) ->
 get_changes_timeout(Args, Callback) ->
     #changes_args{
         heartbeat = Heartbeat,
-        timeout = Timeout,
+        timeout = UserTimeout,
         feed = ResponseType
     } = Args,
     DefaultTimeout = list_to_integer(
         couch_config:get("httpd", "changes_timeout", "60000")
     ),
-    case Heartbeat of
-    undefined ->
-        case Timeout of
+    Timeout = case UserTimeout of
+        undefined -> DefaultTimeout;
+        infiity -> infinity;
+        _ -> lists:min([DefaultTimeout, UserTimeout])
+    end,
+    HeartbeatInterval = case Heartbeat of
+        undefined -> undefined;
+        true -> DefaultTimeout;
+        _ -> lists:min([DefaultTimeout, Heartbeat])
+    end,
+    case HeartbeatInterval of
         undefined ->
-            {DefaultTimeout, fun(UserAcc) -> {stop, UserAcc} end};
-        infinity ->
-            {infinity, fun(UserAcc) -> {stop, UserAcc} end};
+            {Timeout, HeartbeatInterval,
+             fun(UserAcc) -> {stop, UserAcc} end};
         _ ->
-            {lists:min([DefaultTimeout, Timeout]),
-                fun(UserAcc) -> {stop, UserAcc} end}
-        end;
-    true ->
-        {DefaultTimeout,
-            fun(UserAcc) -> {ok, Callback(timeout, ResponseType, UserAcc)} end};
-    _ ->
-        {lists:min([DefaultTimeout, Heartbeat]),
-            fun(UserAcc) -> {ok, Callback(timeout, ResponseType, UserAcc)} end}
+            {Timeout, HeartbeatInterval,
+             fun(UserAcc) ->
+                Before = get(last_changes_heartbeat),
+                case Before of
+                    undefined ->
+                        {ok, UserAcc};
+                    _ ->
+                        Now = now(),
+                        case timer:now_diff(Now, Before) div 1000 >= Timeout of
+                            true ->
+                                {stop, UserAcc};
+                            false ->
+                                {ok, Callback(timeout, ResponseType, UserAcc)}
+                        end
+                end
+             end}
     end.
 
 start_sending_changes(_Callback, UserAcc, ResponseType)
@@ -446,7 +460,7 @@ changes_enumerator(DocInfo, #changes_acc{resp_type = ResponseType} = Acc)
     #changes_acc{
         filter = FilterFun, callback = Callback,
         user_acc = UserAcc, limit = Limit, db = Db,
-        timeout = Timeout, timeout_fun = TimeoutFun
+        timeout_fun = TimeoutFun
     } = Acc,
     #doc_info{high_seq = Seq} = DocInfo,
     Results0 = FilterFun(Db, DocInfo),
@@ -455,7 +469,7 @@ changes_enumerator(DocInfo, #changes_acc{resp_type = ResponseType} = Acc)
     Go = if Limit =< 1 -> stop; true -> ok end,
     case Results of
     [] ->
-        {Done, UserAcc2} = maybe_heartbeat(Timeout, TimeoutFun, UserAcc),
+        {Done, UserAcc2} = TimeoutFun(UserAcc),
         case Done of
         stop ->
             {stop, Acc#changes_acc{seq = Seq, user_acc = UserAcc2}};
@@ -472,7 +486,7 @@ changes_enumerator(DocInfo, Acc) ->
     #changes_acc{
         filter = FilterFun, callback = Callback, prepend = Prepend,
         user_acc = UserAcc, limit = Limit, resp_type = ResponseType, db = Db,
-        timeout = Timeout, timeout_fun = TimeoutFun
+        timeout_fun = TimeoutFun
     } = Acc,
     #doc_info{high_seq = Seq} = DocInfo,
     Results0 = FilterFun(Db, DocInfo),
@@ -480,7 +494,7 @@ changes_enumerator(DocInfo, Acc) ->
     Go = if (Limit =< 1) andalso Results =/= [] -> stop; true -> ok end,
     case Results of
     [] ->
-        {Done, UserAcc2} = maybe_heartbeat(Timeout, TimeoutFun, UserAcc),
+        {Done, UserAcc2} = TimeoutFun(UserAcc),
         case Done of
         stop ->
             {stop, Acc#changes_acc{seq = Seq, user_acc = UserAcc2}};
@@ -558,20 +572,3 @@ reset_heartbeat() ->
     _ ->
         put(last_changes_heartbeat, now())
     end.
-
-maybe_heartbeat(Timeout, TimeoutFun, Acc) ->
-    Before = get(last_changes_heartbeat),
-    case Before of
-    undefined ->
-        {ok, Acc};
-    _ ->
-        Now = now(),
-        case timer:now_diff(Now, Before) div 1000 >= Timeout of
-        true ->
-            Acc2 = TimeoutFun(Acc),
-            put(last_changes_heartbeat, Now),
-            Acc2;
-        false ->
-            {ok, Acc}
-        end
-    end.
diff --git a/test/couchdb/couch_changes_tests.erl b/test/couchdb/couch_changes_tests.erl
index a129ba2fda..98cd202823 100644
--- a/test/couchdb/couch_changes_tests.erl
+++ b/test/couchdb/couch_changes_tests.erl
@@ -539,7 +539,7 @@ spawn_consumer(DbName, ChangesArgs0, Req) ->
         ChangesArgs = case (ChangesArgs0#changes_args.timeout =:= undefined)
             andalso (ChangesArgs0#changes_args.heartbeat =:= undefined) of
             true ->
-                ChangesArgs0#changes_args{timeout = 10, heartbeat = 10};
+                ChangesArgs0#changes_args{heartbeat = 10};
             false ->
                 ChangesArgs0
         end,


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services