Unexpected Registry behavior

I can not update value when use new :via syntax.

iex(1)> {:ok, _} = Registry.start_link(keys: :unique, name: Registry.RegistryTest)
{:ok, #PID<0.103.0>}
iex(2)> name = {:via, Registry, {Registry.RegistryTest, "key1", 0}}
{:via, Registry, {Registry.RegistryTest, "key1", 0}}
iex(3)> {:ok, _} = Agent.start_link(fn -> "some_value" end, name: name)
{:ok, #PID<0.107.0>}
iex(4)> Registry.register(Registry.RegistryTest, "key2", "value2")
{:ok, #PID<0.104.0>}
iex(5)> Registry.lookup(Registry.RegistryTest, "key1")
[{#PID<0.107.0>, 0}]
iex(6)> Registry.lookup(Registry.RegistryTest, "key2")
[{#PID<0.101.0>, "value2"}]
iex(7)> Registry.update_value(Registry.RegistryTest, "key2", fn _ -> "value333" end)
{"value333", "value2"}
iex(8)> Registry.update_value(Registry.RegistryTest, "key1", &(&1 + 1))             

I expect to receive {0, 1} but got :error instead.

1 Like

You can only update a key in the registry within the process itself. In this case, the Agent. To quote the docs:

Updates the value for key for the current process in the unique registry.


Thank you very much.

1 Like