larshei

larshei

Continuously spawn GenServers without running out of names/atoms over time

Background
I was inspired by the this tic tac toe in live view article and thought it would be cool to build something like this.

As games need state, the most obvious solution (to me, maybe there are better solutions) is to use a GenServer for each game, with its life-time being bound to the game itself.

Problem

  • Each game uses the same GenServer Module
  • All processes need different names. Names are atoms.
  • Randomly generating names will lead to runtime generated atoms, and I would like to avoid that (although I guess it would never really be a problem, because the number of available atoms is most likely way higher than the number games that will ever be played :smiley:)

Possible Solution?
I thought a way could maybe be to simply set a hard limit to the number of games (e.g. 10k).
Then generate all the atoms (:game_1 to :game_10000) on startup.

On spawning a new GenServer, a name within the allowed range is picked at random.
If a process with this name exists, pick another name. Once no process has been found for a name, use this name to start a new server.

Players could then connect to the games with unique links that are translated via a Registry. Once the original process/game has been stopped, the invite link will be removed and is no longer valid, even if another game/GenServer with the same name is created.

This way, processes could be spawned continuously, as long as the maximum number of processes is not hit, while players/invites can still be unique.

Question
Do you see any pitfalls on my suggested solution?
Is there a common or better solution to this problem?
What would be your solution?

Most Liked

NobbZ

NobbZ

Use Registry and :via tuples for naming instead of atoms. The key can be arbitrary terms then.

akoutmos

akoutmos

Author of Build a Weather Station with Elixir and Nerves

I would also leverage a Registry like @NobbZ mentioned as well as a dynamic supervisor to watch over these game processes. Usually named processes are for when you have a singleton process or just a handful of processes in a pool. Not for when the number of processes can be unbounded. With that said I would not lean on naming these processes using atoms. Use strings instead to denote the game instance and the Registry to look up the GenServer by that string identifier.

I actually have an article series on this exact architecture:

Hope that helps!

larshei

larshei

Great, thanks a lot!
Now that I read your answers, I think I actually read about this option somehow but guess I did not really understand what I was reading :sweat_smile:

Where Next?

Popular in Discussions Top

Qqwy
I would like to spark a discussion about the static access operator: .. For whom does not know: it is used in Elixir to access fields of...
New
pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
acrolink
How does the two languages compare when it comes to server side application development? Any experiences or ideas? Thank you.
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
pdgonzalez872
If this has been asked here before, please point me to where it was asked as I didn’t find it when I searched the forum. Maybe a mailing ...
New
chulkilee
Here are the list of HTTP client libraries/wrappers, and some thoughts on HTTP client in general. I’d like to hear from others how they w...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 14027 124
New
Ankhers
Just a little information upfront. Generally speaking, if I feel like I need to either break a pipe chain or use an anonymous function in...
New
100phlecs
Are there any downsides, like perf issues, to putting all functional components in CoreComponents as long as you prefix it with the conte...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement