ProcessHub - process distribution library

Hi, we are currently running on a single node and due to scaling challenges (good problem to have!) we are looking at ProcessHub to move to a multi-node setup.

Here is the meat of our code that uses DynamicSupevisor and Registry:

  @doc """
  Start a child process or return an existing one by the given key
  - If the process is already exists for the key, it is returned
  - If the process doesn't exist for the key, it will be created with config in `{mod, opts}`
  """
  @spec start_child(
          supervisor(),
          term(),
          {module(), keyword()}
        ) :: pid()
  def start_child(supervisor, key, {mod, opts}) do
    # the registry is actually the name of the supervisor
    registry = supervisor

    case lookup_child(registry, key) do
      # Process already exists, just return it
      pid when is_pid(pid) ->
        pid

      nil ->
        {:ok, dynamic_supervisor} = Registry.meta(registry, :dynamic_supervisor)
        via = {:via, Registry, {registry, key}}

        # The child will need to be registered with a via tuple so that we
        # have a stable process name to reference
        opts = Keyword.merge(opts, name: via, restart: :temporary)

        case DynamicSupervisor.start_child(dynamic_supervisor, {mod, opts}) do
          {:ok, pid} -> pid
          {:error, {:already_started, pid}} -> pid
        end
    end
  end

If I wanted to swap to using ProcessHub, what things would I use to replace DynamicSupervisor and Registry?

2 Likes