Can this be used with a via_tuple?
Trevoke opened this issue · comments
I am using Elixir's Registry (though I don't think this matters) to keep track of multiple processes born from the same module all with a different id
. I'm not sure I can do this cleanly with this package. Is that true, or am I missing the option that allows it? All I saw was use ExActor.GenServer, export: {:global, :global_registered_name}
but it doesn't look like it will work if I need to spawn two of this kind of process and give them different names?
You can pass a gen_server_opts: :runtime
to defstart
:
def MyServer do
defstart start_link(arg1, arg2), gen_server_opts: :runtime do
# ...
end
# ...
end
Now, your start_link
accepts three arguments: arg1
, arg2
, and GenServer options, so you can do the following:
MyServer.start_link(arg1, arg2, name: {:via, Registry, {MyRegistry, some_name}})
Normally, defcast
and defcall
take the server as the first argument, so you can pass the same via tuple as in start_link
. That's of course clumsy, because the clients must be aware of the exact shape of the via tuple.
If you want to wrap that knowledge inside the GenServer module, you can use private versions of defcastp
and defcallp
and wrap them with a plain function:
def MyServer do
def some_op(name, arg1, arg2), do: some_op_cast(via_tuple(name), arg1, arg2)
defcastp some_op_cast(arg1, arg2, ...) do
# ...
end
defp via_tuple(name), do:
{:via, Registry, {MyRegistry, some_name}}
end
Unfortunately, at this point ExActor doesn't really give you much compared to plain GenServer. ExActor only helps for the simplest cases where all arguments are passed as is. As soon as you somehow need to transform those arguments, it will be pretty much the same amount of code as with GenServer. This is the reason why I mostly stopped using ExActor myself. It sometimes help, but not always, so it seems to me that using a plain GenServer is generally a better way, because it's less magical and standard part of Elixir.
nod I understand, thank you.