Просмотр исходного кода

Refactor rebuilding monitors after scope process crash.

Roberto Ostinelli 3 лет назад
Родитель
Сommit
5663cd243d
2 измененных файлов с 52 добавлено и 42 удалено
  1. 26 21
      src/syn_groups.erl
  2. 26 21
      src/syn_registry.erl

+ 26 - 21
src/syn_groups.erl

@@ -329,29 +329,34 @@ purge_local_data_for_node(Node, #state{
 %% ===================================================================
 -spec rebuild_monitors(#state{}) -> ok.
 rebuild_monitors(#state{
+    table_by_name = TableByName
+} = State) ->
+    GroupsTuples = get_groups_tuples_for_node(node(), TableByName),
+    do_rebuild_monitors(GroupsTuples, #{}, State).
+
+-spec do_rebuild_monitors([syn_groups_tuple()], [reference()], #state{}) -> ok.
+do_rebuild_monitors([], _, _) -> ok;
+do_rebuild_monitors([{GroupName, Pid, Meta, Time} | T], NewMonitorRefs, #state{
     table_by_name = TableByName,
     table_by_pid = TableByPid
-}) ->
-    GroupsTuples = get_groups_tuples_for_node(node(), TableByName),
-    lists:foldl(fun({GroupName, Pid, Meta, Time}, NewMonitorRefs) ->
-        remove_from_local_table(GroupName, Pid, TableByName, TableByPid),
-        case is_process_alive(Pid) of
-            true ->
-                case maps:find(Pid, NewMonitorRefs) of
-                    error ->
-                        MRef = erlang:monitor(process, Pid),
-                        add_to_local_table(GroupName, Pid, Meta, Time, MRef, TableByName, TableByPid),
-                        maps:put(Pid, MRef, NewMonitorRefs);
-
-                    {ok, MRef} ->
-                        add_to_local_table(GroupName, Pid, Meta, Time, MRef, TableByName, TableByPid),
-                        NewMonitorRefs
-                end;
-
-            _ ->
-                NewMonitorRefs
-        end
-    end, #{}, GroupsTuples).
+} = State) ->
+    remove_from_local_table(GroupName, Pid, TableByName, TableByPid),
+    case is_process_alive(Pid) of
+        true ->
+            case maps:find(Pid, NewMonitorRefs) of
+                error ->
+                    MRef = erlang:monitor(process, Pid),
+                    add_to_local_table(GroupName, Pid, Meta, Time, MRef, TableByName, TableByPid),
+                    do_rebuild_monitors(T, maps:put(Pid, MRef, NewMonitorRefs), State);
+
+                {ok, MRef} ->
+                    add_to_local_table(GroupName, Pid, Meta, Time, MRef, TableByName, TableByPid),
+                    do_rebuild_monitors(T, NewMonitorRefs, State)
+            end;
+
+        _ ->
+            do_rebuild_monitors(T, NewMonitorRefs, State)
+    end.
 
 -spec get_groups_tuples_for_node(Node :: node(), TableByName :: atom()) -> [syn_groups_tuple()].
 get_groups_tuples_for_node(Node, TableByName) ->

+ 26 - 21
src/syn_registry.erl

@@ -339,29 +339,34 @@ purge_local_data_for_node(Node, #state{
 %% ===================================================================
 -spec rebuild_monitors(#state{}) -> ok.
 rebuild_monitors(#state{
+    table_by_name = TableByName
+} = State) ->
+    RegistryTuples = get_registry_tuples_for_node(node(), TableByName),
+    do_rebuild_monitors(RegistryTuples, #{}, State).
+
+-spec do_rebuild_monitors([syn_registry_tuple()], [reference()], #state{}) -> ok.
+do_rebuild_monitors([], _, _) -> ok;
+do_rebuild_monitors([{Name, Pid, Meta, Time} | T], NewMonitorRefs, #state{
     table_by_name = TableByName,
     table_by_pid = TableByPid
-}) ->
-    RegistryTuples = get_registry_tuples_for_node(node(), TableByName),
-    lists:foldl(fun({Name, Pid, Meta, Time}, NewMonitorRefs) ->
-        remove_from_local_table(Name, Pid, TableByName, TableByPid),
-        case is_process_alive(Pid) of
-            true ->
-                case maps:find(Pid, NewMonitorRefs) of
-                    error ->
-                        MRef = erlang:monitor(process, Pid),
-                        add_to_local_table(Name, Pid, Meta, Time, MRef, TableByName, TableByPid),
-                        maps:put(Pid, MRef, NewMonitorRefs);
-
-                    {ok, MRef} ->
-                        add_to_local_table(Name, Pid, Meta, Time, MRef, TableByName, TableByPid),
-                        NewMonitorRefs
-                end;
-
-            _ ->
-                NewMonitorRefs
-        end
-    end, #{}, RegistryTuples).
+} = State) ->
+    remove_from_local_table(Name, Pid, TableByName, TableByPid),
+    case is_process_alive(Pid) of
+        true ->
+            case maps:find(Pid, NewMonitorRefs) of
+                error ->
+                    MRef = erlang:monitor(process, Pid),
+                    add_to_local_table(Name, Pid, Meta, Time, MRef, TableByName, TableByPid),
+                    do_rebuild_monitors(T, maps:put(Pid, MRef, NewMonitorRefs), State);
+
+                {ok, MRef} ->
+                    add_to_local_table(Name, Pid, Meta, Time, MRef, TableByName, TableByPid),
+                    do_rebuild_monitors(T, NewMonitorRefs, State)
+            end;
+
+        _ ->
+            do_rebuild_monitors(T, NewMonitorRefs, State)
+    end.
 
 -spec get_registry_tuples_for_node(Node :: node(), TableByName :: atom()) -> [syn_registry_tuple()].
 get_registry_tuples_for_node(Node, TableByName) ->