Adworse Codes Here

Rubyist Elixir Journey

Menu
  • About me
  • Privacy Policy
Menu

Category: Learned the Hard Way

You’ll like it if you, like me, prefer to learn by try and error and read too few docs. (Unlike me, you should read more because Elixirs docs are terrific). All this is in docs; it just happened I learned this the hard way instead.

If you prefer to read docs first, I guess you may wonder what pitfalls you’ve avoided. Here they are!

Singleton GenServer: Short track to the orphanage

Posted on July 16, 2022July 18, 2022 by Dima Ermilov

While writing a GenServer as a singleton on a multi-node system, it’s easy to come up with something like this:

def start_link(_number, _opts \\ []) do
  case GenServer.start_link(__MODULE__, :ok, name: {:global, __MODULE__}) do
    {:ok, pid} ->
      {:ok, pid}

    {:error, {:already_started, pid}} ->
      {:ok, pid}
  end
end

It works perfectly until it doesn’t: when our singleton process goes down, the only supervision tree aware of this would be the one that started it. Every other node won’t get any message, so while the process is restarting, the caller will fail with Process is not alive.…

Read more
Category: Learned the Hard Way

Recent Posts

  • The Curious Case of String.slice/3
  • Singleton GenServer: Short track to the orphanage

Recent Comments

No comments to show.

Archives

  • July 2022

Categories

  • Learned the Hard Way
  • We Need To Go Deeper
©2023 Adworse Codes Here