↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

Highlander, there can be only one

The post explains a small way to run one globally unique process in an Erlang cluster. It uses :global and a Highlander wrapper around an existing child spec so a supervisor can start only one instance across the cluster.

Reading notes
#

  • The goal is to make one child in an existing supervision tree globally unique without changing the whole tree.
  • Highlander is inserted as {Highlander, child_spec} and uses child_spec.id as the global name.
  • :global.register_name(name, pid) is used to claim the name, and :global.whereis_name(name) is used to look up an existing process.
  • If registration succeeds, Highlander starts the wrapped child under its own supervisor.
  • If registration fails, Highlander monitors the already registered process and retries if that process goes down.
  • When the monitored process dies, Highlander handles :DOWN by trying to register again.
  • On termination, Highlander stops the child process so shutdown stays orderly.
  • The wrapper accepts any child spec, including another supervisor, because it uses Supervisor.child_spec/1.