I have a GenServer defined as a module X whose each instance can be instantiated with a different parameter. My app creates multiple instances of this GenServer. When serving an coming request, the main flow needs to send a message to each instance of X asynchronously, so each instance can process the call in parallel. The main flow waits to get all the responses before moving on.
I am not sure what’s the best way to do this. Currently this is what I am doing.
defmodule X do
...
def handle_cast({:doit, caller, p1, ..}, state) do
..
send(caller, ...)
{:noreply, state}
end
..
def do_it_async(server, caller, p1, ...) do
GenServer.cast(server, {:doit, caller, p1, ..})
end
end
defmodule Other do
..
def handleRequest(...) do
servers_list = ..
servers_list |> Enum.each(&X.do_it_async(&1, self(), ..)
results = collect_results([], length(servers_list), 0)
..
end
defp collect_results(results, count, received) when count == received, do: {:ok, results}
defp collect_results(results, count, received) do
receive do
{:x_result, ..} ->
collect_results([{..} | results], count, received + 1)
after
5000 -> :timeout
end
end
This is obviously very tedious and error-prone. What is the recommended / common way to do this kind of thing in Elixir?
I am aware of Task.async_stream
and I could provide a sync version of the API. However, unless I misunderstood it, I don’t think that’s the right way to do it because it would spawn so many processes just to call the API and wait for the result. Each X instance is already a process.