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 https://asylamba.com/public/media/files/sources/asylamba-build-diagram.png)

What would be the Elixir way of achieving that?

Marked As Solved

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.

Also Liked

abdelaz3r

abdelaz3r

Ok fine, I’ll go with that so.

Thanks again a lot for that.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement