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?

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

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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 43622 214
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement