Skip to content

Commit

Permalink
Allow any term to be a coordinator name
Browse files Browse the repository at this point in the history
This comes from when the name was the name of a registered process, and
hence the Erlang registry required it be an atom, but now it is an
opaque term stored in a persistent term, hence allowing the flexibility
for it to be any term allows us to create coordinators dynamically.

One example of when this might be desired is when coordinating members
of a groupchat: when the groupchat is created, a coordinator is created
with the name of the groupchat, and the members will add themselves to
this coordinator. This way, they can coordinate setting presences and
messages within the namespace of that room alone.
  • Loading branch information
NelsonVides committed May 7, 2024
1 parent 9279382 commit 96ab7d2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/coordinator/amoc_coordinator.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
-define(IS_N_OF_USERS(N), (?IS_POS_INT(N) orelse N =:= all)).
-define(IS_TIMEOUT(Timeout), (?IS_POS_INT(Timeout) orelse Timeout =:= infinity)).

-type name() :: atom().
-type name() :: term().

-type data() :: {pid(), Data :: any()}.

Expand Down Expand Up @@ -102,11 +102,11 @@ reset(Name) ->
notify(Name, reset_coordinator).

-spec notify(name(), coordinator_timeout | reset_coordinator | {coordinate, {pid(), term()}}) -> ok.
notify(Name, coordinator_timeout) when is_atom(Name) ->
notify(Name, coordinator_timeout) ->
do_notify(Name, coordinator_timeout);
notify(Name, reset_coordinator) when is_atom(Name) ->
notify(Name, reset_coordinator) ->
do_notify(Name, reset_coordinator);
notify(Name, {coordinate, _} = Event) when is_atom(Name) ->
notify(Name, {coordinate, _} = Event) ->
do_notify(Name, Event).

do_notify(Name, Event) ->
Expand Down
14 changes: 14 additions & 0 deletions test/amoc_coordinator_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ all() ->
plan_normalises_successfully,
ordering_plan_sets_all_at_the_end,
failing_action_does_not_kill_the_worker,
coordinator_name_can_be_dynamic_terms,
execute_with_range_without_timeout,
execute_plan_without_timeout,
reset_plan_without_timeout,
Expand Down Expand Up @@ -220,6 +221,19 @@ execute_plan_with_timeout(_Config) ->
assert_telemetry_events(Name, [start, {N1, add}, timeout,
{N2, add}, timeout, stop]).

coordinator_name_can_be_dynamic_terms(_) ->
BinName = <<(atom_to_binary(?FUNCTION_NAME))/binary, (crypto:strong_rand_bytes(8))/binary>>,
SomePlan = [{1000, fun(_Event) -> ok end}],
Names = [{some_composed_tuple},
[list, made_of, atoms],
base64:encode(BinName)],
[ begin
?assertEqual(ok, amoc_coordinator:start(Name, SomePlan)),
{ok, _, _Workers} = amoc_coordinator_sup:get_workers(Name),
[amoc_coordinator:add(Name, User) || User <- lists:seq(1, 10)],
amoc_coordinator:stop(Name)
end || Name <- Names ].

failing_action_does_not_kill_the_worker(_) ->
Name = ?FUNCTION_NAME,
Plan = {2, [mock_failing()]},
Expand Down

0 comments on commit 96ab7d2

Please sign in to comment.