bitwalker / swarm

Easy clustering, registration, and distribution of worker processes for Erlang/Elixir
MIT License
1.2k stars 103 forks source link

Swarm stops to respond when try to find name #145

Open wesleimp opened 3 years ago

wesleimp commented 3 years ago

If you try to run Swarm.whereis_name(name) while Swarm.register_name(name, Sup....) didn't finish yet, the application stops to respond.

defmodule MyDynamicSupervisor do
  use DynamicSupervisor

  # Initialization

  def register(name) do
    child_spec = %{
      id: MyActor,
      start: {MyActor, :start_link, [name]},
      restart: :transient
    }

    case DynamicSupervisor.start_child(__MODULE__, child_spec) do
      {:error, {:already_started, pid}} -> {:ok, pid}
      {:ok, pid} -> {:ok, pid}
    end
  end
end
defmodule MyActor do
  use GenServer

  def start_actor(name) do
    {:ok, pid} = Swarm.register_name(name, MyDynamicSupervisor, :register, [name])
  end

  def start_link(name) do
    GenServer.start_link(__MODULE__, name)
  end

  def init(name) do
    pid = Swarm.whereis_name(name) # At this moment, the application stops to respond
    {:ok, name}
  end
end