↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

Supervising multiple GenServer processes

The article explains a refactoring in the Trello tribute series: instead of keeping all connected board members in one GenServer, it creates one process per board and puts them under a supervisor. That way, if one process dies or restarts, the state of the other boards is not lost.

Reading notes
#

  • A single PhoenixTrello.BoardChannel.Monitor kept a map of connected users by board, but a restart would lose that state.
  • The proposed fix is to split the state into multiple processes, one for each board.
  • A module-based Supervisor starts and restarts the monitor processes.
  • The supervisor uses restart: :transient so a child restarts only after an abnormal termination.
  • The setup uses :simple_one_for_one so only the child process that fails is restarted.
  • PhoenixTrello.BoardChannel.Monitor gets a dynamic name from board_id through ref(board_id).
  • create(board_id) checks whether a process already exists for that board before calling Supervisor.start_child/2.
  • user_joined/2, users_in_board/1, and user_left/2 call the correct process through try_call/2.
  • The application supervision tree is updated to include the new board-channel supervisor instead of the old monitor worker.