📎 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.
Highlanderis inserted as{Highlander, child_spec}and useschild_spec.idas 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
:DOWNby 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.
