derekkraan / horde

Horde is a distributed Supervisor and Registry backed by DeltaCrdt
MIT License
1.3k stars 102 forks source link

Is it possible to start a Horde dynamic supervisor with a registered name? #234

Open iacobson opened 3 years ago

iacobson commented 3 years ago

I try something like (simplified)

  def start_link(name) do
    Horde.DynamicSupervisor.start_link(__MODULE__, [], name: name)
  end

  def via(name), do: {:via, Horde.Registry, {MyApp.DistributedRegistry, name}}

and trying to start it with

    Horde.DynamicSupervisor.start_child(MyApp.DistributedSupervisor, {__MODULE__, name})

getting the error

{:error,
 {%Protocol.UndefinedError{
    description: "",
    protocol: String.Chars,
    value: {:via, Horde.Registry, {Distributed.DistributedRegistry, "hello"}}
  },
  [
    {String.Chars, :impl_for!, 1, [file: 'lib/string/chars.ex', line: 3]},
    {String.Chars, :to_string, 1, [file: 'lib/string/chars.ex', line: 22]},
    {Horde.DynamicSupervisor, :start_link, 3,
     [file: 'lib/horde/dynamic_supervisor.ex', line: 131]},
    {Horde.ProcessesSupervisor, :start_child, 3,
     [file: 'lib/horde/processes_supervisor.ex', line: 766]},
    {Horde.ProcessesSupervisor, :handle_start_child, 2,
     [file: 'lib/horde/processes_supervisor.ex', line: 752]},
    {:gen_server, :try_handle_call, 4, [file: 'gen_server.erl', line: 661]},
    {:gen_server, :handle_msg, 6, [file: 'gen_server.erl', line: 690]},
    {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 249]}
  ]}}

When tried to start with a simple string name (by mistake), I got the following error, which made me think the :via option should be possible


{:error,
 {:shutdown,
  {:failed_to_start_child, Horde.DynamicSupervisorImpl,
   {:EXIT,
    {%ArgumentError{
       message: "expected :name option to be one of the following:\n\n  * nil\n  * atom\n  * {:global, term}\n  * {:via, module, term}\n\nGot: \"hello\"\n"
     },
     [
       {GenServer, :do_start, 4, [file: 'lib/gen_server.ex', line: 943]},
       {:supervisor, :do_start_child_i, 3, [file: 'supervisor.erl', line: 379]},
       {:supervisor, :do_start_child, 2, [file: 'supervisor.erl', line: 365]},
       {:supervisor, :"-start_children/2-fun-0-", 3,
        [file: 'supervisor.erl', line: 349]},
       {:supervisor, :children_map, 4, [file: 'supervisor.erl', line: 1157]},
       {:supervisor, :init_children, 2, [file: 'supervisor.erl', line: 315]},
       {:gen_server, :init_it, 2, [file: 'gen_server.erl', line: 374]},
       {:gen_server, :init_it, 6, [file: 'gen_server.erl', line: 342]}
     ]}}}}}
ccapndave commented 2 years ago

I've also run into this problem. It seems to be because of https://github.com/derekkraan/horde/blob/master/lib/horde/dynamic_supervisor.ex#L131 which assumes that name is a binary.

derekkraan commented 2 years ago

Hey, thanks for the issue. If someone wants to submit a PR that makes Horde work with via tuple semantics then I would be happy to merge that.