syn.erl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2015-2021 Roberto Ostinelli <roberto@ostinelli.net> and Neato Robotics, Inc.
  7. %%
  8. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  9. %% of this software and associated documentation files (the "Software"), to deal
  10. %% in the Software without restriction, including without limitation the rights
  11. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. %% copies of the Software, and to permit persons to whom the Software is
  13. %% furnished to do so, subject to the following conditions:
  14. %%
  15. %% The above copyright notice and this permission notice shall be included in
  16. %% all copies or substantial portions of the Software.
  17. %%
  18. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. %% THE SOFTWARE.
  25. %% ==========================================================================================================
  26. %% ===================================================================
  27. %% @doc `syn' exposes all of the global Process Registry and Process Group APIs.
  28. %%
  29. %% Syn implement Scopes. You may think of Scopes such as database tables, so a set of data elements,
  30. %% but that's where the analogy ends.
  31. %%
  32. %% A Scope is a way to create a namespaced, logical overlay network running on top of the Erlang distribution cluster.
  33. %% Nodes that belong to the same Scope will form a "sub-cluster": they will synchronize data between themselves,
  34. %% and themselves only.
  35. %%
  36. %% For instance, you may have nodes in your Erlang cluster that need to handle connections to users, and other nodes
  37. %% that need to handle connections to physical devices. One approach is to create two Scopes: `users' and `devices',
  38. %% where you can register your different types of connections.
  39. %%
  40. %% Scopes are therefore a way to properly namespace your logic, but they also allow to build considerably larger
  41. %% scalable architectures, as it is possible to divide an Erlang cluster into sub-clusters which hold specific portions
  42. %% of data.
  43. %%
  44. %% Please note any of the methods documented here will raise:
  45. %% <ul>
  46. %% <li>An `error({invalid_scope, Scope})' if the local node has not been added to the specified Scope.</li>
  47. %% <li>An `error({invalid_remote_scope, Scope})' if the Pid passed in as variable is running on a node that has not
  48. %% been added to the specified Scope.</li>
  49. %% </ul>
  50. %%
  51. %% <h2>Quickstart</h2>
  52. %% <h3>Registry</h3>
  53. %% <h4>Elixir</h4>
  54. %% ```
  55. %% iex> :syn.add_node_to_scopes([:users])
  56. %% :ok
  57. %% iex> pid = self().
  58. %% #PID<0.105.0>
  59. %% iex> :syn.register(:users, "hedy", pid)
  60. %% :ok
  61. %% iex> :syn.lookup(:users, "hedy")
  62. %% {#PID<0.105.0>,:undefined}
  63. %% iex> :syn.register(:users, "hedy", pid, [city: "Milan"])
  64. %% :ok
  65. %% iex> :syn.lookup(:users, "hedy")
  66. %% {#PID<0.105.0>,[city: "Milan"]}
  67. %% iex> :syn.registry_count(:users)
  68. %% 1
  69. %% '''
  70. %% <h4>Erlang</h4>
  71. %% ```
  72. %% 1> syn:add_node_to_scopes([users]).
  73. %% ok
  74. %% 2> Pid = self().
  75. %% <0.93.0>
  76. %% 3> syn:register(users, "hedy", Pid).
  77. %% ok
  78. %% 4> syn:lookup(users, "hedy").
  79. %% {<0.93.0>,undefined}
  80. %% 5> syn:register(users, "hedy", Pid, [{city, "Milan"}]).
  81. %% ok
  82. %% 6> syn:lookup(users, "hedy").
  83. %% {<0.93.0>,[{city, "Milan"}]}
  84. %% 7> syn:registry_count(users).
  85. %% 1
  86. %% '''
  87. %% <h3>Process Groups</h3>
  88. %% <h4>Elixir</h4>
  89. %% ```
  90. %% iex> :syn.add_node_to_scopes([:users])
  91. %% :ok
  92. %% iex> pid = self().
  93. %% #PID<0.88.0>
  94. %% iex> :syn.join(:users, {:italy, :lombardy}, pid)
  95. %% :ok
  96. %% iex> :syn.members(:users, {:italy, :lombardy}).
  97. %% [#PID<0.88.0>,:undefined}]
  98. %% iex> :syn.is_member(:users, {:italy, :lombardy}, pid)
  99. %% true
  100. %% iex> :syn.publish(:users, {:italy, :lombardy}, "hello lombardy!")
  101. %% {:ok,1}
  102. %% iex> flush()
  103. %% Shell got "hello lombardy!"
  104. %% ok
  105. %% '''
  106. %% <h4>Erlang</h4>
  107. %% ```
  108. %% 1> syn:add_node_to_scopes([users]).
  109. %% ok
  110. %% 2> Pid = self().
  111. %% <0.88.0>
  112. %% 3> syn:join(users, {italy, lombardy}, Pid).
  113. %% ok
  114. %% 4> syn:members(users, {italy, lombardy}).
  115. %% [{<0.88.0>,undefined}]
  116. %% 5> syn:is_member(users, {italy, lombardy}, Pid).
  117. %% true
  118. %% 6> syn:publish(users, {italy, lombardy}, "hello lombardy!").
  119. %% {ok,1}
  120. %% 7> flush().
  121. %% Shell got "hello lombardy!"
  122. %% ok
  123. %% '''
  124. %% @end
  125. %% ===================================================================
  126. -module(syn).
  127. %% API
  128. -export([start/0, stop/0]).
  129. %% scopes
  130. -export([node_scopes/0, add_node_to_scopes/1]).
  131. -export([set_event_handler/1]).
  132. %% registry
  133. -export([lookup/2]).
  134. -export([register/3, register/4]).
  135. -export([unregister/2]).
  136. -export([registry_count/1, registry_count/2]).
  137. -export([local_registry_count/1]).
  138. %% gen_server via interface
  139. -export([register_name/2, unregister_name/1, whereis_name/1, send/2]).
  140. %% groups
  141. -export([members/2, is_member/3]).
  142. -export([local_members/2, is_local_member/3]).
  143. -export([join/3, join/4]).
  144. -export([leave/3]).
  145. -export([group_count/1, group_count/2]).
  146. -export([local_group_count/1]).
  147. -export([group_names/1, group_names/2]).
  148. -export([local_group_names/1]).
  149. -export([publish/3]).
  150. -export([local_publish/3]).
  151. -export([multi_call/3, multi_call/4, multi_call_reply/2]).
  152. %% macros
  153. -define(DEFAULT_MULTI_CALL_TIMEOUT_MS, 5000).
  154. %% API
  155. %% ===================================================================
  156. %% @doc Starts Syn manually.
  157. %%
  158. %% In most cases Syn will be started as one of your application's dependencies,
  159. %% however you may use this helper method to start it manually.
  160. -spec start() -> ok.
  161. start() ->
  162. {ok, _} = application:ensure_all_started(syn),
  163. ok.
  164. %% @doc Stops Syn manually.
  165. -spec stop() -> ok | {error, Reason :: any()}.
  166. stop() ->
  167. application:stop(syn).
  168. %% ----- \/ scopes ---------------------------------------------------
  169. %% @doc Retrieves the Scopes that the node has been added to.
  170. -spec node_scopes() -> [atom()].
  171. node_scopes() ->
  172. syn_sup:node_scopes().
  173. %% @doc Add the local node to the specified `Scopes'.
  174. %%
  175. %% There are 2 ways to add a node to Scopes. One is by using this method, the other is to set the environment variable `syn'
  176. %% with the key `scopes'. In this latter case, you're probably best off using an application configuration file:
  177. %%
  178. %% You only need to add a node to a scope once.
  179. %% <h3>Elixir</h3>
  180. %% ```
  181. %% config :syn,
  182. %% scopes: [:devices, :users]
  183. %% '''
  184. %% <h3>Erlang</h3>
  185. %% ```
  186. %% {syn, [
  187. %% {scopes, [:devices, :users]}
  188. %% ]}
  189. %% '''
  190. %%
  191. %% <h2>Examples</h2>
  192. %% <h3>Elixir</h3>
  193. %% ```
  194. %% iex> :syn.add_node_to_scopes([:devices]).
  195. %% :ok
  196. %% '''
  197. %% <h3>Erlang</h3>
  198. %% ```
  199. %% 1> syn:add_node_to_scopes([devices]).
  200. %% ok
  201. %% '''
  202. -spec add_node_to_scopes(Scopes :: [atom()]) -> ok.
  203. add_node_to_scopes(Scopes) when is_list(Scopes) ->
  204. lists:foreach(fun(Scope) ->
  205. syn_sup:add_node_to_scope(Scope)
  206. end, Scopes).
  207. %% @doc Sets the handler module.
  208. %%
  209. %% Please see {@link syn_event_handler} for information on callbacks.
  210. %%
  211. %% There are 2 ways to set a handler module. One is by using this method, the other is to set the environment variable `syn'
  212. %% with the key `scopes'. In this latter case, you're probably best off using an application configuration file:
  213. %%
  214. %% <h3>Elixir</h3>
  215. %% ```
  216. %% config :syn,
  217. %% event_handler: MyCustomEventHandler
  218. %% '''
  219. %% <h3>Erlang</h3>
  220. %% ```
  221. %% {syn, [
  222. %% {event_handler, my_custom_event_handler}
  223. %% ]}
  224. %% '''
  225. %%
  226. %% <h2>Examples</h2>
  227. %% <h3>Elixir</h3>
  228. %% ```
  229. %% iex> :syn.set_event_handler(MyCustomEventHandler)
  230. %% ok
  231. %% '''
  232. %% <h3>Erlang</h3>
  233. %% ```
  234. %% 1> syn:set_event_handler(my_custom_event_handler).
  235. %% ok
  236. %% '''
  237. -spec set_event_handler(module()) -> ok.
  238. set_event_handler(Module) ->
  239. application:set_env(syn, event_handler, Module).
  240. %% ----- \/ registry -------------------------------------------------
  241. %% @doc Looks up a registry entry in the specified `Scope'.
  242. %%
  243. %% <h2>Examples</h2>
  244. %% <h3>Elixir</h3>
  245. %% ```
  246. %% iex> :syn.register(:devices, "SN-123-456789", self())
  247. %% :ok
  248. %% iex> :syn.lookup(:devices, "SN-123-456789")
  249. %% {#PID<0.105.0>, undefined}
  250. %% '''
  251. %% <h3>Erlang</h3>
  252. %% ```
  253. %% 1> syn:register(devices, "SN-123-456789", self()).
  254. %% ok
  255. %% 2> syn:lookup(devices, "SN-123-456789").
  256. %% {<0.79.0>, undefined}
  257. %% '''
  258. -spec lookup(Scope :: atom(), Name :: any()) -> {pid(), Meta :: any()} | undefined.
  259. lookup(Scope, Name) ->
  260. syn_registry:lookup(Scope, Name).
  261. %% @equiv register(Scope, Name, Pid, undefined)
  262. %% @end
  263. -spec register(Scope :: atom(), Name :: any(), Pid :: any()) -> ok | {error, Reason :: any()}.
  264. register(Scope, Name, Pid) ->
  265. register(Scope, Name, Pid, undefined).
  266. %% @doc Registers a process with metadata in the specified `Scope'.
  267. %%
  268. %% Possible error reasons:
  269. %% <ul>
  270. %% <li>`taken': name is already registered with another `pid()'.</li>
  271. %% <li>`not_alive': The `pid()' being registered is not alive.</li>
  272. %% </ul>
  273. %%
  274. %% You may re-register a process multiple times, for example if you need to update its metadata.
  275. %% When a process gets registered, Syn will automatically monitor it. You may also register the same process with different names.
  276. %%
  277. %% <h2>Examples</h2>
  278. %% <h3>Elixir</h3>
  279. %% ```
  280. %% iex> :syn.register(:devices, "SN-123-456789", self(), [meta: :one])
  281. %% :ok
  282. %% iex> :syn.lookup(:devices, "SN-123-456789")
  283. %% {#PID<0.105.0>, [meta: :one]}
  284. %% '''
  285. %%
  286. %% Processes can also be registered as `gen_server' names, by usage of via-tuples. This way, you can use the `gen_server'
  287. %% API with these tuples without referring to the Pid directly. If you do so, you MUST use a `gen_server' name
  288. %% in format `{Scope, Name}', i.e. your via tuple will look like `{via, syn, {my_scope, <<"process name">>}}'.
  289. %% See here below for examples.
  290. %% <h2>Examples</h2>
  291. %% <h3>Elixir</h3>
  292. %% ```
  293. %% iex> tuple = {:via, :syn, {:devices, "SN-123-456789"}}.
  294. %% {:via, :syn, {:devices, "SN-123-456789"}}
  295. %% iex> GenServer.start_link(__MODULE__, [], name: tuple)
  296. %% {ok, #PID<0.105.0>}
  297. %% iex> GenServer.call(tuple, :your_message).
  298. %% :your_message
  299. %% '''
  300. %% <h3>Erlang</h3>
  301. %% ```
  302. %% 1> syn:register(devices, "SN-123-456789", self(), [{meta, one}]).
  303. %% ok
  304. %% 2> syn:lookup(devices, "SN-123-456789").
  305. %% {<0.79.0>, [{meta, one}]}
  306. %% '''
  307. %% ```
  308. %% 1> Tuple = {via, syn, {devices, "SN-123-456789"}}.
  309. %% {via, syn, {devices, "SN-123-456789"}}
  310. %% 2> gen_server:start_link(Tuple, your_module, []).
  311. %% {ok, <0.79.0>}
  312. %% 3> gen_server:call(Tuple, your_message).
  313. %% your_message
  314. %% '''
  315. -spec register(Scope :: atom(), Name :: any(), Pid :: pid(), Meta :: any()) -> ok | {error, Reason :: any()}.
  316. register(Scope, Name, Pid, Meta) ->
  317. syn_registry:register(Scope, Name, Pid, Meta).
  318. %% @doc Unregisters a process from specified `Scope'.
  319. %%
  320. %% Possible error reasons:
  321. %% <ul>
  322. %% <li>`undefined': name is not registered.</li>
  323. %% <li>`race_condition': the local `pid()' does not correspond to the cluster value, so Syn will not succeed
  324. %% unregistering the value and will wait for the cluster to synchronize. This is a rare occasion.</li>
  325. %% </ul>
  326. %%
  327. %% You don't need to unregister names of processes that are about to die, since they are monitored by Syn
  328. %% and they will be removed automatically.
  329. -spec unregister(Scope :: atom(), Name :: any()) -> ok | {error, Reason :: any()}.
  330. unregister(Scope, Name) ->
  331. syn_registry:unregister(Scope, Name).
  332. %% @doc Returns the count of all registered processes for the specified `Scope'.
  333. %%
  334. %% <h2>Examples</h2>
  335. %% <h3>Elixir</h3>
  336. %% ```
  337. %% iex> :syn.registry_count(:devices)
  338. %% 512473
  339. %% '''
  340. %% <h3>Erlang</h3>
  341. %% ```
  342. %% 1> syn:registry_count(devices).
  343. %% 512473
  344. %% '''
  345. -spec registry_count(Scope :: atom()) -> non_neg_integer().
  346. registry_count(Scope) ->
  347. syn_registry:count(Scope).
  348. %% @doc Returns the count of all registered processes for the specified `Scope' running on a node.
  349. -spec registry_count(Scope :: atom(), Node :: node()) -> non_neg_integer().
  350. registry_count(Scope, Node) ->
  351. syn_registry:count(Scope, Node).
  352. %% @equiv registry_count(Scope, node())
  353. %% @end
  354. -spec local_registry_count(Scope :: atom()) -> non_neg_integer().
  355. local_registry_count(Scope) ->
  356. registry_count(Scope, node()).
  357. %% ----- \/ gen_server via module interface --------------------------
  358. -spec register_name(Name :: any(), Pid :: pid()) -> yes | no.
  359. register_name({Scope, Name}, Pid) ->
  360. case register(Scope, Name, Pid) of
  361. ok -> yes;
  362. _ -> no
  363. end.
  364. -spec unregister_name(Name :: any()) -> any().
  365. unregister_name({Scope, Name}) ->
  366. case unregister(Scope, Name) of
  367. ok -> Name;
  368. _ -> nil
  369. end.
  370. -spec whereis_name(Name :: any()) -> pid() | undefined.
  371. whereis_name({Scope, Name}) ->
  372. case lookup(Scope, Name) of
  373. {Pid, _Meta} -> Pid;
  374. undefined -> undefined
  375. end.
  376. -spec send(Name :: any(), Message :: any()) -> pid().
  377. send({Scope, Name}, Message) ->
  378. case whereis_name({Scope, Name}) of
  379. undefined ->
  380. {badarg, {{Scope, Name}, Message}};
  381. Pid ->
  382. Pid ! Message,
  383. Pid
  384. end.
  385. %% ----- \/ groups ---------------------------------------------------
  386. %% @doc Returns the list of all members for GroupName in the specified `Scope'.
  387. %%
  388. %% <h2>Examples</h2>
  389. %% <h3>Elixir</h3>
  390. %% ```
  391. %% iex> :syn.join(:devices, "area-1").
  392. %% :ok
  393. %% iex> :syn.members(:devices, "area-1").
  394. %% [{#PID<0.105.0>, :undefined}]
  395. %% '''
  396. %% <h3>Erlang</h3>
  397. %% ```
  398. %% 1> syn:join(devices, "area-1", self()).
  399. %% ok
  400. %% 2> syn:members(devices, "area-1").
  401. %% [{<0.69.0>, undefined}]
  402. %% '''
  403. -spec members(Scope :: atom(), GroupName :: term()) -> [{Pid :: pid(), Meta :: term()}].
  404. members(Scope, GroupName) ->
  405. syn_pg:members(Scope, GroupName).
  406. %% @doc Returns whether a `pid()' is a member of GroupName in the specified `Scope'.
  407. -spec is_member(Scope :: atom(), GroupName :: any(), Pid :: pid()) -> boolean().
  408. is_member(Scope, GroupName, Pid) ->
  409. syn_pg:is_member(Scope, GroupName, Pid).
  410. %% @doc Returns the list of all members for GroupName in the specified `Scope' running on the local node.
  411. -spec local_members(Scope :: atom(), GroupName :: term()) -> [{Pid :: pid(), Meta :: term()}].
  412. local_members(Scope, GroupName) ->
  413. syn_pg:local_members(Scope, GroupName).
  414. %% @doc Returns whether a `pid()' is a member of GroupName in the specified `Scope' running on the local node.
  415. -spec is_local_member(Scope :: atom(), GroupName :: any(), Pid :: pid()) -> boolean().
  416. is_local_member(Scope, GroupName, Pid) ->
  417. syn_pg:is_local_member(Scope, GroupName, Pid).
  418. %% @equiv join(Scope, GroupName, Pid, undefined)
  419. %% @end
  420. -spec join(Scope :: any(), Name :: any(), Pid :: any()) -> ok | {error, Reason :: any()}.
  421. join(Scope, GroupName, Pid) ->
  422. join(Scope, GroupName, Pid, undefined).
  423. %% @doc Adds a `pid()' with metadata to GroupName in the specified `Scope'.
  424. %%
  425. %% Possible error reasons:
  426. %% <ul>
  427. %% <li>`not_alive': The `pid()' being added is not alive.</li>
  428. %% </ul>
  429. %%
  430. %% A process can join multiple groups. When a process joins a group, Syn will automatically monitor it.
  431. %% A process may join the same group multiple times, for example if you need to update its metadata,
  432. %% though it will still be listed only once in it.
  433. %%
  434. %% <h2>Examples</h2>
  435. %% <h3>Elixir</h3>
  436. %% ```
  437. %% iex> :syn.join(:devices, "area-1", self(), [meta: :one]).
  438. %% :ok
  439. %% '''
  440. %% <h3>Erlang</h3>
  441. %% ```
  442. %% 1> syn:join(devices, "area-1", self(), [{meta, one}]).
  443. %% ok
  444. %% '''
  445. -spec join(Scope :: atom(), GroupName :: any(), Pid :: pid(), Meta :: any()) -> ok | {error, Reason :: any()}.
  446. join(Scope, GroupName, Pid, Meta) ->
  447. syn_pg:join(Scope, GroupName, Pid, Meta).
  448. %% @doc Removes a `pid()' from GroupName in the specified `Scope'.
  449. %%
  450. %% Possible error reasons:
  451. %% <ul>
  452. %% <li>`not_in_group': The `pid()' is not in GroupName for the specified `Scope'.</li>
  453. %% </ul>
  454. %%
  455. %% You don't need to remove processes that are about to die, since they are monitored by Syn and they will be removed
  456. %% automatically from their groups.
  457. -spec leave(Scope :: atom(), GroupName :: any(), Pid :: pid()) -> ok | {error, Reason :: any()}.
  458. leave(Scope, GroupName, Pid) ->
  459. syn_pg:leave(Scope, GroupName, Pid).
  460. %% @doc Returns the count of all the groups for the specified `Scope'.
  461. %%
  462. %% <h2>Examples</h2>
  463. %% <h3>Elixir</h3>
  464. %% ```
  465. %% iex> :syn.group_count("area-1")
  466. %% 321778
  467. %% '''
  468. %% <h3>Erlang</h3>
  469. %% ```
  470. %% 1> syn:group_count("area-1").
  471. %% 321778
  472. %% '''
  473. -spec group_count(Scope :: atom()) -> non_neg_integer().
  474. group_count(Scope) ->
  475. syn_pg:count(Scope).
  476. %% @doc Returns the count of all the groups for the specified `Scope' which have at least 1 process running on `Node'.
  477. -spec group_count(Scope :: atom(), Node :: node()) -> non_neg_integer().
  478. group_count(Scope, Node) ->
  479. syn_pg:count(Scope, Node).
  480. %% @equiv group_count(Scope, node())
  481. %% @end
  482. -spec local_group_count(Scope :: atom()) -> non_neg_integer().
  483. local_group_count(Scope) ->
  484. group_count(Scope, node()).
  485. %% @doc Returns the group names for the specified `Scope'.
  486. %%
  487. %% The order of the group names is not guaranteed to be the same on all calls.
  488. %%
  489. %% <h2>Examples</h2>
  490. %% <h3>Elixir</h3>
  491. %% ```
  492. %% iex> :syn.group_names(:users)
  493. %% ["area-1", "area-2"]
  494. %% '''
  495. %% <h3>Erlang</h3>
  496. %% ```
  497. %% 1> syn:group_names(users).
  498. %% ["area-1", "area-2"]
  499. %% '''
  500. -spec group_names(Scope :: atom()) -> [GroupName :: term()].
  501. group_names(Scope) ->
  502. syn_pg:group_names(Scope).
  503. %% @doc Returns the group names for the specified `Scope' which have at least 1 process running on `Node'.
  504. %%
  505. %% The order of the group names is not guaranteed to be the same on all calls.
  506. -spec group_names(Scope :: atom(), Node :: node()) -> [GroupName :: term()].
  507. group_names(Scope, Node) ->
  508. syn_pg:group_names(Scope, Node).
  509. %% @equiv group_names(Scope, node())
  510. %% @end
  511. -spec local_group_names(Scope :: atom()) -> [GroupName :: term()].
  512. local_group_names(Scope) ->
  513. group_names(Scope, node()).
  514. %% @doc Publish a message to all group members in the specified `Scope'.
  515. %%
  516. %% `RecipientCount' is the count of the intended recipients.
  517. %%
  518. %% <h2>Examples</h2>
  519. %% <h3>Elixir</h3>
  520. %% ```
  521. %% iex> :syn.join(:users, "area-1", self())
  522. %% :ok
  523. %% iex> :syn.publish(:users, "area-1", :my_message)
  524. %% {:ok,1}
  525. %% iex> flush().
  526. %% Shell got :my_message
  527. %% :ok
  528. %% '''
  529. %% <h3>Erlang</h3>
  530. %% ```
  531. %% 1> syn:join(users, "area-1", self()).
  532. %% ok
  533. %% 2> syn:publish(users, "area-1", my_message).
  534. %% {ok,1}
  535. %% 3> flush().
  536. %% Shell got my_message
  537. %% ok
  538. %% '''
  539. -spec publish(Scope :: atom(), GroupName :: any(), Message :: any()) -> {ok, RecipientCount :: non_neg_integer()}.
  540. publish(Scope, GroupName, Message) ->
  541. syn_pg:publish(Scope, GroupName, Message).
  542. %% @doc Publish a message to all group members running on the local node in the specified `Scope'.
  543. %%
  544. %% Works similarly to {@link publish/3} for local processes.
  545. -spec local_publish(Scope :: atom(), GroupName :: any(), Message :: any()) -> {ok, RecipientCount :: non_neg_integer()}.
  546. local_publish(Scope, GroupName, Message) ->
  547. syn_pg:local_publish(Scope, GroupName, Message).
  548. %% @equiv multi_call(Scope, GroupName, Message, 5000)
  549. %% @end
  550. -spec multi_call(Scope :: atom(), GroupName :: any(), Message :: any()) ->
  551. {
  552. Replies :: [{{pid(), Meta :: term()}, Reply :: term()}],
  553. BadReplies :: [{pid(), Meta :: term()}]
  554. }.
  555. multi_call(Scope, GroupName, Message) ->
  556. multi_call(Scope, GroupName, Message, ?DEFAULT_MULTI_CALL_TIMEOUT_MS).
  557. %% @doc Calls all group members in the specified `Scope' and collects their replies.
  558. %%
  559. %% When this call is issued, all members will receive a tuple in the format:
  560. %%
  561. %% `{syn_multi_call, TestMessage, Caller, Meta}'
  562. %%
  563. %% To reply, every member MUST use the method {@link multi_call_reply/2}.
  564. %%
  565. %% Syn will wait up to the value specified in `Timeout' to receive all replies from the members.
  566. %% The responses will be added to the `Replies' list, while the members that do not reply in time or that crash
  567. %% before sending a reply will be added to the `BadReplies' list.
  568. -spec multi_call(Scope :: atom(), GroupName :: any(), Message :: any(), Timeout :: non_neg_integer()) ->
  569. {
  570. Replies :: [{{pid(), Meta :: term()}, Reply :: term()}],
  571. BadReplies :: [{pid(), Meta :: term()}]
  572. }.
  573. multi_call(Scope, GroupName, Message, Timeout) ->
  574. syn_pg:multi_call(Scope, GroupName, Message, Timeout).
  575. %% @doc Allows a group member to reply to a multi call.
  576. %%
  577. %% See {@link multi_call/4} for info.
  578. -spec multi_call_reply(Caller :: term(), Reply :: any()) -> {syn_multi_call_reply, pid(), Reply :: any()}.
  579. multi_call_reply(Caller, Reply) ->
  580. syn_pg:multi_call_reply(Caller, Reply).