fireproofsocks

fireproofsocks

Naming GenServer using :via does not work the same as naming Agent with :via

I’m working with a custom registry and I’ve noticed that it doesn’t seem to like GenServers. Whereas I can start up an Agent using the same :via clause and things work.

Given this GenServer:

defmodule Replay do
    @moduledoc false
    use GenServer

    @impl true
    def init(opts), do: {:ok, opts}

    @impl true
    def handle_call(request, _from, state), do: {:reply, request, state}
end

I try starting instances of it:

{:ok, _} = Registry.start_link(keys: :unique, name: MyRegistry)
{:ok, _pid} = GenServer.start_link(Replay, name: {:via, Registry, {MyRegistry, "aaa", "v1"}})
{:ok, _pid} = GenServer.start_link(Replay, name: {:via, Registry, {MyRegistry, "ccc", "v3"}})
{:ok, _pid} = GenServer.start_link(Replay, name: {:via, Registry, {MyRegistry, "bbb", "v2"}})
{:ok, _pid} = Agent.start_link(fn -> 0 end, name: {:via, Registry, {MyRegistry, "ddd", "v4"}})
Registry.select(MyRegistry, [{{:"$1", :"$2", :"$3"}, [], [{{:"$1", :"$2", :"$3"}}]}])

# Expected:
[{"aaa", #PID<0.131.0>, "v4"}, {"bbb", #PID<0.132.0>, "v4"}, {"ccc", #PID<0.133.0>, "v4"}, {"ddd", #PID<0.134.0>, "v4"}]

# Actual:
[{"ddd", #PID<0.134.0>, "v4"}]

Can someone explain this? And hopefully point at the error? Thanks in advance!

Marked As Solved

al2o3cr

al2o3cr

This is passing name: ... to Replay as opts, GenServer.start_link/3 always needs a second argument. You likely want:

{:ok, _pid} = GenServer.start_link(Replay, [], name: {:via, Registry, {MyRegistry, "aaa", "v1"}})

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement