tsukit
Best practice for doing fanout async calls
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.
Most Liked
entone
Registry also provides a nice way to do pub-sub without having to introduce GenStage or back pressure or anything like that, Registry — Elixir v1.20.2
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









