sezaru
Fastest way to a GenServer communicate with all childrens of some supervisor?
Hello,
A part of my application currently have a supervisor hierarchy that looks something like this:
As you can see, I have a Manager Genserver and a Childs Supervisor that will contain a lot of children’s (this is a normal supervisor for now, but maybe in the future I would want to start the children on demand, so that can be changed to a Dynamic Supervisor in the future I guess).
My question is regarding the Manager Genserver communication with the children’s. Basically Manager Genserver needs to send broadcast messages to all the children’s, this can happen very frequently (like multiple times a second), so I have a performance concern.
So, what kind of communication strategy should I use in this case? I’m kinda new with OTP, so I’m not 100% sure and are contemplating the following solutions:
-
Create a function inside
Childs Supervisorthat will iterate and do aGenServer.callto all it’s children. Not sure if this is efficient or will bottleneck all the communication if one children blocks the call for some reason; -
Send a message from each
Childto theManager GenServerwith it’sPID, store it in a list and sendGenServer.calliterating through it; -
Use a
PubSubsystem likePhoenix.PubSub. This seems like a good solution, but as far as I know thePhoenix.PubSubis global and used more for a distributed case. I’m not sure if using it would be over-engineering and maybe be a bottleneck; -
Use the
Registry. this seems like another good solution too but I only saw examples of global (but local to the beam node) examples ofRegistry, in my case I only want to handle communication fromManager Genserverto all theChilds, is it possible to create maybe a local Registry forParent Supervisor?
Any help would be much appreciated.
Thanks a lot!
Most Liked
kokolegorille
Supervisor has the which children function (and more…)
You should try to benchmark if sending message to that list of children is within your refresh frequency.
lud
Your 1) timeouts because you implemented it inside a handle_call and call it from another process. But this problem is out of scope. You could add a :infinity timeout.
You could call all children concurrently:
reply = state.childs
|> Map.values()
|> Enum.map(fn pid -> Task.async(fn -> GenServer.call(pid, {:received_msg, "hello"}) end) end)
|> Enum.map(&Task.await/1)
Edit: but this is a poor man’s implementation of GenServer.multi_call.
Or use Task.async_stream to limit the number of concurrent calls.
Calling your children with GenServer.call provides another guarantee : all calls will be made before you start to call your children again. The other solutions do not give this guarantee. But you may not care about it.
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
- #performance










