Skip to content

Commit

Permalink
Use :name in child spec as child id
Browse files Browse the repository at this point in the history
This is similar to sneako/finch@6b42c8e

While at it I made the following change which fixes what I think was
unintentional ommision of interpolation:

    -  defp to_name(name), do: :"__MODULE__:#{name}"
    +  defp to_name(name), do: :"#{inspect(__MODULE__)}:#{name}"

I also noticed we have:

    def start_link(opts) when is_list(opts) do
      with {:ok, name} <- Keyword.fetch(opts, :name) do
        GenServer.start_link(__MODULE__, opts, name: to_name(name))
      end
    end

And so if we do this, i.e. don't pass :name, we'll get :error

    iex> Meilisearch.start_link([])
    :error

That is not a common return value from start_link. Supervisor will crash
on it. So while the `child_spec/1` change that I am proposing is
technically a breaking change I think it is warranted as the current
behaviour under that circumstance, not passing name, was also crashing.

In this spirit, my suggestion is to change start_link, something like:

       def start_link(opts) when is_list(opts) do
    -    with {:ok, name} <- Keyword.fetch(opts, :name) do
    -      GenServer.start_link(__MODULE__, opts, name: to_name(name))
    -    end
    +    name = Keyword.fetch!(opts, :name)
    +    GenServer.start_link(__MODULE__, opts, name: to_name(name))
       end

Which I'm happy to do. let me know!
  • Loading branch information
wojtekmach committed Feb 14, 2025
1 parent 3163843 commit d27cebf
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/meilisearch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,16 @@ defmodule Meilisearch do

use GenServer

defp to_name(name), do: :"__MODULE__:#{name}"
def child_spec(opts) do
name = Keyword.fetch!(opts, :name)

%{
id: name,
start: {__MODULE__, :start_link, [opts]}
}
end

defp to_name(name), do: :"#{inspect(__MODULE__)}:#{name}"

@spec start_link(atom(),
endpoint: String.t(),
Expand Down

0 comments on commit d27cebf

Please sign in to comment.