syn_event_handler.erl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. -export([do_resolve_registry_conflict/5]).
  31. -callback resolve_registry_conflict(
  32. Name :: any(),
  33. {Pid1 :: pid(), Meta1 :: any()},
  34. {Pid2 :: pid(), Meta2 :: any()}
  35. ) -> PidToKeep :: pid() | undefined.
  36. -optional_callbacks([resolve_registry_conflict/3]).
  37. -spec do_resolve_registry_conflict(
  38. Scope :: atom(),
  39. Name :: any(),
  40. {Pid1 :: pid(), Meta1 :: any(), Time1 :: non_neg_integer()},
  41. {Pid2 :: pid(), Meta2 :: any(), Time2 :: non_neg_integer()},
  42. CustomEventHandler :: module() | undefined
  43. ) -> PidToKeep :: pid() | undefined.
  44. do_resolve_registry_conflict(Scope, Name, {Pid1, Meta1, Time1}, {Pid2, Meta2, Time2}, CustomEventHandler) ->
  45. case erlang:function_exported(CustomEventHandler, resolve_registry_conflict, 4) of
  46. true ->
  47. try CustomEventHandler:resolve_registry_conflict(Scope, Name, {Pid1, Meta1}, {Pid2, Meta2}) of
  48. PidToKeep when is_pid(PidToKeep) -> PidToKeep;
  49. _ -> undefined
  50. catch Exception:Reason ->
  51. error_logger:error_msg(
  52. "Syn(~p): Error ~p in custom handler resolve_registry_conflict: ~p~n",
  53. [node(), Exception, Reason]
  54. ),
  55. undefined
  56. end;
  57. _ ->
  58. %% by default, keep pid registered more recently
  59. %% this is a simple mechanism that can be imprecise, as system clocks are not perfectly aligned in a cluster
  60. %% if something more elaborate is desired (such as vector clocks) use Meta to store data and a custom event handler
  61. PidToKeep = case Time1 > Time2 of
  62. true -> Pid1;
  63. _ -> Pid2
  64. end,
  65. PidToKeep
  66. end.