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
)
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
Use Registry and :via tuples for naming instead of atoms. The key can be arbitrary terms then.
akoutmos
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
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 ![]()
Popular in Discussions
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









