abdelaz3r

abdelaz3r

Dynamic Supervision Trees

Hi, elixir programmers (and at first I hope you would excuse my English),

I’m developing a backend server for a video game project. It is basically a real-time multi-agent simulation. Agents are implemented as GenServer, and of course, I make intensive use of Supervisor, DynamicSupervisor, and Registry. That part is fine. I now struggle trying to make multiple simulations under the same supervision tree. The idea is to have a GameSupervisor that can spawn a new instance of the simulation. Each instance is a Supervisor that has some DynamicSupervisor that manage GenServer. Each simulation is totally closed to the outside (eventually, I’ll have to connect the simulations to a socket dispatcher).

The issue that I now encounter is for “dynamic” DynamicSupervisor, and “dynamic” Registry. These ones need to have a proper name, but it can only be atoms. So I’m trying to figure out how to deal with this problem.

My current solution (which is absolutely not optimal, you’ll see why) it create, for Supervisor and Registry only, on runtime atoms, from strings. Agents (GenServer) are registered using Registry mechanism. It could be fine (I won’t create thousands of instances at the same moment) but it will eventually reach the atom number limit.

Here is a picture of an example process diagram that I use now. Elixir.Game is an application, it starts and Instance.Registry and a Game.Supervisor which is a DynamicSupervisor. There, I can spawn an instance (<0.474.0> or <0.510.0>) which are supervisors. Every following node that begins with “Instance*_” is either a Supervisor or a Registry, that have a dynamic name (strong to atom conversion). And then DynamicSupervisors spawn Agents which are registered into the Registry.

(It appear that I don’t have rights to upload images, so here is the link DELIMAJITU | Situs Resmi Game Angka & Link Login Aman)

What would be the Elixir way of achieving that?

First 4 of 4 Posts! Switch mode

Phillipp

Phillipp

You can also use tuples for the name in the registry.

I am building a position tracking service and my Supervisor which is a child under a DynamicSupervisor uses {:position_tracker, user_id} as name.

Then one of its children uses {:position_tracker, user_id, :state} as name (e.g. name = {:via, Registry, {@registry, {:position_tracker, user_id, :state}}}).

That way, you don’t need to generate atoms on runtime.

abdelaz3r

abdelaz3r

Ok, thanks a lot for the hint.

I have tryied rewriting my code considering your advise and it seems to work fine.

However it appear that we can’t register Registry name via the via mechanism. So I ended up using only one “global” registry, which may be ok, but I worried about the bottleneck that this Registry would become. Since I’ll have a quite big number of agent for each instance, they all would try to reach this Registry. What about that?

Here is the previous node diagram :

And here is the new one :

Phillipp

Phillipp

One registry should be fine. As far as I know, it uses ETS so it’s quite fast. I wouldn’t worry about it until I see some real performance degradation related to it.

abdelaz3r

abdelaz3r

Ok fine, I’ll go with that so.

Thanks again a lot for that.

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement