Cleaning Up After a Game
Explore how to efficiently clean up in-memory game state data using :ets.delete/2 within a supervised GenServer setup. Understand the terminate callback to handle process timeouts gracefully and ensure system reliability by removing stale game entries when games end or timeout.
We'll cover the following...
We'll cover the following...
Cleaning up the tables
We’re saving the state of each game in the :game_state table, but so far, we haven’t removed that state when the game is over. The :game_state table continues to grow and use memory unless we delete a game’s key from the :game_state table when the supervisor terminates the child process or when the child process times out.
To clean that data up when a game ends normally, we add a call to :ets.delete/2 in the GameSupervisor.stop_game/1 function:
def stop_game(name) do
:ets.delete(:game_state, name)
Supervisor.terminate_child(__MODULE__, pid_from_name(name))
end
Cleaning up ...