Browse Source

Immediately update local registry when calling from different nodes.

Roberto Ostinelli 3 years ago
parent
commit
7cd8563c85
2 changed files with 21 additions and 6 deletions
  1. 20 6
      src/syn_registry.erl
  2. 1 0
      test/syn_registry_SUITE.erl

+ 20 - 6
src/syn_registry.erl

@@ -95,7 +95,13 @@ register(Scope, Name, Pid, Meta) ->
     ProcessName = get_process_name_for_scope(Scope),
     ProcessName = get_process_name_for_scope(Scope),
     Node = node(Pid),
     Node = node(Pid),
     try gen_server:call({ProcessName, Node}, {register_on_node, Name, Pid, Meta}) of
     try gen_server:call({ProcessName, Node}, {register_on_node, Name, Pid, Meta}) of
-        Value -> Value
+        {ok, Time} when Node =/= node() ->
+            %% update table on caller node immediately so that subsequent calls have an updated registry
+            add_to_local_table(Scope, Name, Pid, Meta, Time, undefined),
+            ok;
+
+        {Response, _} ->
+            Response
     catch
     catch
         exit:{noproc, {gen_server, call, _}} -> error({invalid_scope, Scope})
         exit:{noproc, {gen_server, call, _}} -> error({invalid_scope, Scope})
     end.
     end.
@@ -114,7 +120,15 @@ unregister(Scope, Name) ->
         {{Name, Pid}, _, _, _, _} ->
         {{Name, Pid}, _, _, _, _} ->
             ProcessName = get_process_name_for_scope(Scope),
             ProcessName = get_process_name_for_scope(Scope),
             Node = node(Pid),
             Node = node(Pid),
-            gen_server:call({ProcessName, Node}, {unregister_on_node, Name, Pid})
+            case gen_server:call({ProcessName, Node}, {unregister_on_node, Name, Pid}) of
+                ok when Node =/= node() ->
+                    %% remove table on caller node immediately so that subsequent calls have an updated registry
+                    remove_from_local_table(Scope, Name, Pid),
+                    ok;
+
+                Response ->
+                    Response
+            end
     catch
     catch
         exit:{noproc, {gen_server, call, _}} -> error({invalid_scope, Scope});
         exit:{noproc, {gen_server, call, _}} -> error({invalid_scope, Scope});
         error:badarg -> error({invalid_scope, Scope})
         error:badarg -> error({invalid_scope, Scope})
@@ -196,7 +210,7 @@ handle_call({register_on_node, Name, Pid, Meta}, _From, #state{
                     %% broadcast
                     %% broadcast
                     broadcast({'3.0', sync_register, Scope, Name, Pid, Meta, Time}, State),
                     broadcast({'3.0', sync_register, Scope, Name, Pid, Meta, Time}, State),
                     %% return
                     %% return
-                    {reply, ok, State};
+                    {reply, {ok, Time}, State};
 
 
                 {{Name, Pid}, _TableMeta, _TableTime, MRef, _TableNode} ->
                 {{Name, Pid}, _TableMeta, _TableTime, MRef, _TableNode} ->
                     %% same pid, possibly new meta or time, overwrite
                     %% same pid, possibly new meta or time, overwrite
@@ -205,14 +219,14 @@ handle_call({register_on_node, Name, Pid, Meta}, _From, #state{
                     %% broadcast
                     %% broadcast
                     broadcast({'3.0', sync_register, Scope, Name, Pid, Meta, Time}, State),
                     broadcast({'3.0', sync_register, Scope, Name, Pid, Meta, Time}, State),
                     %% return
                     %% return
-                    {reply, ok, State};
+                    {reply, {ok, Time}, State};
 
 
                 _ ->
                 _ ->
-                    {reply, {error, taken}, State}
+                    {reply, {{error, taken}, undefined}, State}
             end;
             end;
 
 
         false ->
         false ->
-            {reply, {error, not_alive}, State}
+            {reply, {{error, not_alive}, undefined}, State}
     end;
     end;
 
 
 handle_call({unregister_on_node, Name, Pid}, _From, #state{scope = Scope} = State) ->
 handle_call({unregister_on_node, Name, Pid}, _From, #state{scope = Scope} = State) ->

+ 1 - 0
test/syn_registry_SUITE.erl

@@ -510,6 +510,7 @@ three_nodes_register_unregister_and_monitor_custom_scope(Config) ->
     {error, taken} = syn:register(custom_scope_ab, "scope_a", PidWithMeta),
     {error, taken} = syn:register(custom_scope_ab, "scope_a", PidWithMeta),
     {error, not_alive} = syn:register(custom_scope_ab, {"pid not alive"}, list_to_pid("<0.9999.0>")),
     {error, not_alive} = syn:register(custom_scope_ab, {"pid not alive"}, list_to_pid("<0.9999.0>")),
     {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:register(custom_scope_bc, "scope_a_noscope", Pid),
     {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:register(custom_scope_bc, "scope_a_noscope", Pid),
+    {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:unregister(custom_scope_bc, "scope_a_noscope"),
 
 
     %% retrieve
     %% retrieve
     undefined = syn:lookup("scope_a"),
     undefined = syn:lookup("scope_a"),