syn_event_handler.erl 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2019-2021 Roberto Ostinelli <roberto@ostinelli.net> and Neato Robotics, Inc.
  7. %%
  8. %% Portions of code from Ulf Wiger's unsplit server module:
  9. %% <https://github.com/uwiger/unsplit/blob/master/src/unsplit_server.erl>
  10. %%
  11. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  12. %% of this software and associated documentation files (the "Software"), to deal
  13. %% in the Software without restriction, including without limitation the rights
  14. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. %% copies of the Software, and to permit persons to whom the Software is
  16. %% furnished to do so, subject to the following conditions:
  17. %%
  18. %% The above copyright notice and this permission notice shall be included in
  19. %% all copies or substantial portions of the Software.
  20. %%
  21. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. %% THE SOFTWARE.
  28. %% ==========================================================================================================
  29. -module(syn_event_handler).
  30. %% API
  31. -export([ensure_event_handler_loaded/0]).
  32. -export([do_on_process_registered/4, do_on_process_unregistered/4]).
  33. -export([do_on_process_joined/4, do_on_process_left/4]).
  34. -export([do_resolve_registry_conflict/4]).
  35. -callback on_process_registered(
  36. Scope :: any(),
  37. Name :: any(),
  38. Pid :: pid(),
  39. Meta :: any()
  40. ) -> any().
  41. -callback on_process_unregistered(
  42. Scope :: any(),
  43. Name :: any(),
  44. Pid :: pid(),
  45. Meta :: any()
  46. ) -> any().
  47. -callback resolve_registry_conflict(
  48. Name :: any(),
  49. {Pid1 :: pid(), Meta1 :: any()},
  50. {Pid2 :: pid(), Meta2 :: any()}
  51. ) -> PidToKeep :: pid() | undefined.
  52. -optional_callbacks([on_process_registered/4, on_process_unregistered/4, resolve_registry_conflict/3]).
  53. %% ===================================================================
  54. %% API
  55. %% ===================================================================
  56. -spec ensure_event_handler_loaded() -> module().
  57. ensure_event_handler_loaded() ->
  58. %% get handler
  59. CustomEventHandler = get_custom_event_handler(),
  60. %% ensure that is it loaded (not using code:ensure_loaded/1 to support embedded mode)
  61. catch CustomEventHandler:module_info(exports).
  62. -spec do_on_process_registered(
  63. Scope :: atom(),
  64. Name :: any(),
  65. {TablePid :: pid() | undefined, TableMeta :: any()},
  66. {Pid :: pid(), Meta :: any()}
  67. ) -> any().
  68. do_on_process_registered(_Scope, _Name, {TablePid, TableMeta}, {Pid, Meta})
  69. when TablePid =:= Pid, TableMeta =:= Meta -> ok;
  70. do_on_process_registered(Scope, Name, {_TablePid, _TableMeta}, {Pid, Meta}) ->
  71. call_callback_event(on_process_registered, Scope, Name, Pid, Meta).
  72. -spec do_on_process_unregistered(
  73. Scope :: atom(),
  74. Name :: any(),
  75. TablePid :: pid(),
  76. TableMeta :: any()
  77. ) -> any().
  78. do_on_process_unregistered(Scope, Name, Pid, Meta) ->
  79. call_callback_event(on_process_unregistered, Scope, Name, Pid, Meta).
  80. -spec do_on_process_joined(
  81. Scope :: atom(),
  82. Name :: any(),
  83. {TablePid :: pid() | undefined, TableMeta :: any()},
  84. {Pid :: pid(), Meta :: any()}
  85. ) -> any().
  86. do_on_process_joined(_Scope, _GroupName, {TableMeta}, {_Pid, Meta})
  87. when TableMeta =:= Meta -> ok;
  88. do_on_process_joined(Scope, GroupName, {_TableMeta}, {Pid, Meta}) ->
  89. call_callback_event(on_process_joined, Scope, GroupName, Pid, Meta).
  90. -spec do_on_process_left(
  91. Scope :: atom(),
  92. Name :: any(),
  93. TablePid :: pid(),
  94. TableMeta :: any()
  95. ) -> any().
  96. do_on_process_left(Scope, GroupName, Pid, Meta) ->
  97. call_callback_event(on_process_left, Scope, GroupName, Pid, Meta).
  98. -spec do_resolve_registry_conflict(
  99. Scope :: atom(),
  100. Name :: any(),
  101. {Pid1 :: pid(), Meta1 :: any(), Time1 :: non_neg_integer()},
  102. {Pid2 :: pid(), Meta2 :: any(), Time2 :: non_neg_integer()}
  103. ) -> PidToKeep :: pid() | undefined.
  104. do_resolve_registry_conflict(Scope, Name, {Pid1, Meta1, Time1}, {Pid2, Meta2, Time2}) ->
  105. CustomEventHandler = get_custom_event_handler(),
  106. case erlang:function_exported(CustomEventHandler, resolve_registry_conflict, 4) of
  107. true ->
  108. try CustomEventHandler:resolve_registry_conflict(Scope, Name, {Pid1, Meta1, Time1}, {Pid2, Meta2, Time2}) of
  109. PidToKeep when is_pid(PidToKeep) -> PidToKeep;
  110. _ -> undefined
  111. catch Class:Reason ->
  112. error_logger:error_msg(
  113. "Syn(~p): Error ~p in custom handler resolve_registry_conflict: ~p",
  114. [node(), Class, Reason]
  115. ),
  116. undefined
  117. end;
  118. _ ->
  119. %% by default, keep pid registered more recently
  120. %% this is a simple mechanism that can be imprecise, as system clocks are not perfectly aligned in a cluster
  121. %% if something more elaborate is desired (such as vector clocks) use Meta to store data and a custom event handler
  122. PidToKeep = case Time1 > Time2 of
  123. true -> Pid1;
  124. _ -> Pid2
  125. end,
  126. PidToKeep
  127. end.
  128. %% ===================================================================
  129. %% Internal
  130. %% ===================================================================
  131. -spec get_custom_event_handler() -> undefined | {ok, CustomEventHandler :: atom()}.
  132. get_custom_event_handler() ->
  133. application:get_env(syn, event_handler, undefined).
  134. -spec call_callback_event(
  135. CallbackMethod :: atom(),
  136. Scope :: atom(),
  137. Name :: any(),
  138. Pid :: pid(),
  139. Meta :: any()
  140. ) -> any().
  141. call_callback_event(CallbackMethod, Scope, Name, Pid, Meta) ->
  142. CustomEventHandler = get_custom_event_handler(),
  143. case erlang:function_exported(CustomEventHandler, CallbackMethod, 4) of
  144. true ->
  145. try CustomEventHandler:CallbackMethod(Scope, Name, Pid, Meta)
  146. catch Class:Reason:Stacktrace ->
  147. error_logger:error_msg(
  148. "Syn(~p): Error ~p:~p in custom handler ~p: ~p",
  149. [node(), Class, Reason, CallbackMethod, Stacktrace]
  150. )
  151. end;
  152. _ ->
  153. ok
  154. end.